Browse Source

Merge pull request #41 from rollup/browser

Browser-friendly version
contingency-plan
Rich Harris 10 years ago
parent
commit
4cfb38a116
  1. 3
      .babelrc
  2. 13
      browser/sander.js
  3. 40
      gobblefile.js
  4. 4
      package.json
  5. 11
      src/Bundle.js
  6. 2
      src/Module.js
  7. 2
      src/rollup.js
  8. 5
      src/utils/normalizePlatform.js
  9. 73
      src/utils/path.js
  10. 8
      src/utils/resolveId.js

3
.babelrc

@ -5,8 +5,7 @@
"es6.classes", "es6.classes",
"es6.constants", "es6.constants",
"es6.destructuring", "es6.destructuring",
"es6.parameters.default", "es6.parameters",
"es6.parameters.rest",
"es6.properties.shorthand", "es6.properties.shorthand",
"es6.spread", "es6.spread",
"es6.templateLiterals" "es6.templateLiterals"

13
browser/sander.js

@ -0,0 +1,13 @@
export function readFile () {
throw new Error( 'Cannot use sander.readFile inside browser' );
}
export function readFileSync () {
throw new Error( 'Cannot use sander.readFileSync inside browser' );
}
export function writeFile () {
throw new Error( 'Cannot use sander.writeFile inside browser' );
}
export const Promise = window.Promise;

40
gobblefile.js

@ -1,20 +1,38 @@
var gobble = require( 'gobble' ); var gobble = require( 'gobble' );
var fs = require( 'fs' );
var selfhost = 1; var src = gobble( 'src' );
module.exports = selfhost ? var node = src
gobble( 'src' )
.transform( 'babel' )
.transform( 'rollup', { .transform( 'rollup', {
entry: 'rollup.js', entry: 'rollup.js',
dest: 'rollup.js',
format: 'cjs', format: 'cjs',
external: [ 'sander', 'path', 'acorn', 'magic-string' ] external: [ 'sander', 'path', 'acorn', 'magic-string' ]
}) : })
.transform( 'babel' );
gobble( 'src' ) var absolutePath = /^(?:\/|(?:[A-Za-z]:)?\\)/;
.transform( 'babel' )
.transform( 'esperanto-bundle', { var browserPlaceholders = {
entry: 'rollup', sander: fs.readFileSync( 'browser/sander.js' ).toString()
type: 'cjs', };
strict: true
var browser = src
.transform( 'rollup-babel', {
entry: 'rollup.js',
dest: 'rollup.browser.js',
format: 'cjs',
load: function ( id ) {
if ( ~id.indexOf( 'sander.js' ) ) return browserPlaceholders.sander;
return fs.readFileSync( id ).toString();
},
external: [ 'acorn', 'magic-string' ]
})
.transform( 'browserify', {
entries: [ './rollup.browser' ],
dest: 'rollup.browser.js',
standalone: 'rollup'
}); });
module.exports = gobble([ node, browser ]);

4
package.json

@ -35,9 +35,11 @@
"console-group": "^0.1.2", "console-group": "^0.1.2",
"gobble": "^0.10.1", "gobble": "^0.10.1",
"gobble-babel": "^5.5.8", "gobble-babel": "^5.5.8",
"gobble-browserify": "^0.6.1",
"gobble-cli": "^0.4.2", "gobble-cli": "^0.4.2",
"gobble-esperanto-bundle": "^0.2.0", "gobble-esperanto-bundle": "^0.2.0",
"gobble-rollup": "^0.3.1", "gobble-rollup": "^0.5.0",
"gobble-rollup-babel": "^0.1.0",
"mocha": "^2.2.4", "mocha": "^2.2.4",
"source-map": "^0.1.40" "source-map": "^0.1.40"
}, },

11
src/Bundle.js

@ -1,4 +1,4 @@
import { basename, dirname, extname, relative } from 'path'; import { basename, extname } from './utils/path';
import { Promise } from 'sander'; import { Promise } from 'sander';
import MagicString from 'magic-string'; import MagicString from 'magic-string';
import { blank, keys } from './utils/object'; import { blank, keys } from './utils/object';
@ -486,17 +486,14 @@ export default class Bundle {
let map = null; let map = null;
if ( options.sourceMap ) { if ( options.sourceMap ) {
const file = options.sourceMapFile || options.dest;
map = magicString.generateMap({ map = magicString.generateMap({
includeContent: true, includeContent: true,
file: options.sourceMapFile || options.dest file
// TODO // TODO
}); });
// make sources relative. TODO fix this upstream? map.sources = map.sources.map( unixizePath );
const dir = dirname( map.file );
map.sources = map.sources.map( source => {
return source ? unixizePath( relative( dir, source ) ) : null;
});
} }
return { code, map }; return { code, map };

2
src/Module.js

@ -1,4 +1,4 @@
import { dirname } from 'path'; import { dirname } from './utils/path';
import { Promise } from 'sander'; import { Promise } from 'sander';
import { parse } from 'acorn'; import { parse } from 'acorn';
import MagicString from 'magic-string'; import MagicString from 'magic-string';

2
src/rollup.js

@ -1,4 +1,4 @@
import { basename } from 'path'; import { basename } from './utils/path';
import { writeFile } from 'sander'; import { writeFile } from 'sander';
import Bundle from './Bundle'; import Bundle from './Bundle';

5
src/utils/normalizePlatform.js

@ -1,6 +1,3 @@
import { sep } from "path";
export function unixizePath( path ) { export function unixizePath( path ) {
if (sep==="/") return path; return path.split( /[\/\\]/ ).join( '/' );
return path.split(sep).join("/");
} }

73
src/utils/path.js

@ -0,0 +1,73 @@
// TODO does this all work on windows?
export const absolutePath = /^(?:\/|(?:[A-Za-z]:)?\\)/;
export function isAbsolute ( path ) {
return absolutePath.test( path );
}
export function basename ( path ) {
return path.split( /(\/|\\)/ ).pop();
}
export function dirname ( path ) {
const match = /(\/|\\)[^\/\\]+$/.exec( path );
if ( !match ) return '.';
return path.slice( 0, -match[0].length );
}
export function extname ( path ) {
const match = /\.[^\.]+$/.exec( path );
if ( !match ) return '';
return match[0]
}
export function relative ( from, to ) {
const fromParts = from.split( /[\/\\]/ ).filter( Boolean );
const toParts = to.split( /[\/\\]/ ).filter( Boolean );
while ( fromParts[0] && toParts[0] && fromParts[0] === toParts[0] ) {
fromParts.shift();
toParts.shift();
}
while ( toParts[0] && toParts[0][0] === '.' ) {
const toPart = toParts.shift();
if ( toPart === '..' ) {
fromParts.pop();
} else if ( toPart !== '.' ) {
throw new Error( `Unexpected path part (${toPart})` );
}
}
while ( fromParts.pop() ) {
toParts.unshift( '..' );
}
return toParts.join( '/' );
}
export function resolve ( ...paths ) {
let resolvedParts = paths.shift().split( /[\/\\]/ );
paths.forEach( path => {
if ( isAbsolute( path ) ) {
resolvedParts = path.split( /[\/\\]/ );
} else {
const parts = path.split( /[\/\\]/ );
while ( parts[0] && parts[0][0] === '.' ) {
const part = parts.shift();
if ( part === '..' ) {
resolvedParts.pop();
} else if ( part !== '.' ) {
throw new Error( `Unexpected path part (${part})` );
}
}
resolvedParts.push.apply( resolvedParts, parts );
}
});
return resolvedParts.join( '/' ); // TODO windows...
}

8
src/utils/resolveId.js

@ -1,14 +1,12 @@
import { dirname, resolve } from 'path'; import { absolutePath, dirname, isAbsolute, resolve } from './path';
import { readFileSync } from 'sander'; import { readFileSync } from 'sander';
const absolutePath = /^(?:\/|(?:[A-Za-z]:)?\\)/;
export function defaultResolver ( importee, importer, options ) { export function defaultResolver ( importee, importer, options ) {
// absolute paths are left untouched // absolute paths are left untouched
if ( absolutePath.test( importee ) ) return importee; if ( isAbsolute( importee ) ) return importee;
// if this is the entry point, resolve against cwd // if this is the entry point, resolve against cwd
if ( importer === undefined ) return resolve( importee ); if ( importer === undefined ) return resolve( process.cwd(), importee );
// we try to resolve external modules // we try to resolve external modules
if ( importee[0] !== '.' ) { if ( importee[0] !== '.' ) {

Loading…
Cancel
Save