Browse Source

tidy up

contingency-plan
Rich Harris 10 years ago
parent
commit
259fb1b876
  1. 57
      src/Bundle.js
  2. 8
      src/Module.js

57
src/Bundle.js

@ -14,20 +14,11 @@ export default class Bundle {
constructor ( options ) { constructor ( options ) {
this.base = options.base || process.cwd(); this.base = options.base || process.cwd();
this.entryPath = resolve( this.base, options.entry ).replace( /\.js$/, '' ) + '.js'; this.entryPath = resolve( this.base, options.entry ).replace( /\.js$/, '' ) + '.js';
this.entryModule = null;
this.resolvePath = options.resolvePath || defaultResolver; this.resolvePath = options.resolvePath || defaultResolver;
this.entryModule = null;
this.modulePromises = {}; this.modulePromises = {};
this.modules = {}; this.statements = [];
// this will store the top-level AST nodes we import
this.body = [];
// this will store per-module names, and enable deconflicting
this.bindingNames = {};
this.usedNames = {};
this.externalModules = []; this.externalModules = [];
} }
@ -54,7 +45,6 @@ export default class Bundle {
bundle: this bundle: this
}); });
this.modules[ path ] = module;
return module; return module;
}); });
} }
@ -70,17 +60,11 @@ export default class Bundle {
const importedNames = keys( entryModule.imports ); const importedNames = keys( entryModule.imports );
entryModule.definedNames
.concat( importedNames )
.forEach( name => {
this.usedNames[ name ] = true;
});
// pull in imports // pull in imports
return sequence( importedNames, name => { return sequence( importedNames, name => {
return entryModule.define( name ) return entryModule.define( name )
.then( nodes => { .then( nodes => {
this.body.push.apply( this.body, nodes ); this.statements.push.apply( this.statements, nodes );
}); });
}) })
.then( () => { .then( () => {
@ -97,7 +81,7 @@ export default class Bundle {
} }
// Include everything else // Include everything else
this.body.push( node ); this.statements.push( node );
}); });
}); });
}) })
@ -108,23 +92,11 @@ export default class Bundle {
} }
deconflict () { deconflict () {
// TODO this probably needs to happen at generate time, since
// treatment of external modules differs between formats
// e.g. this...
//
// import { relative } from 'path'`;
// console.log( relative( 'foo', 'bar' ) );
//
// ...would look very similar when bundled as ES6, but in
// a CommonJS bundle would become this:
//
// var path = require( 'path' );
// console.log( path.relative( 'foo', 'bar' ) );
let definers = {}; let definers = {};
let conflicts = {}; let conflicts = {};
// Discover conflicts (i.e. two statements in separate modules both define `foo`) // Discover conflicts (i.e. two statements in separate modules both define `foo`)
this.body.forEach( statement => { this.statements.forEach( statement => {
keys( statement._defines ).forEach( name => { keys( statement._defines ).forEach( name => {
if ( has( definers, name ) ) { if ( has( definers, name ) ) {
conflicts[ name ] = true; conflicts[ name ] = true;
@ -164,25 +136,8 @@ export default class Bundle {
generate ( options = {} ) { generate ( options = {} ) {
let magicString = new MagicString.Bundle({ separator: '' }); let magicString = new MagicString.Bundle({ separator: '' });
// TODO we shouldn't be adding export statements back into the entry
// module, they shouldn't be removed in the first place
/*this.entryModule.exportStatements.forEach( statement => {
if ( statement.specifiers.length ) {
// we don't need to include `export { foo }`, it's already handled
return;
}
if ( statement.declaration.type === 'VariableDeclaration' ) {
statement._source.remove( statement.start, statement.declaration.start );
} else {
// TODO function, class declarations
}
this.body.push( statement );
});*/
// Apply new names and add to the output bundle // Apply new names and add to the output bundle
this.body.forEach( statement => { this.statements.forEach( statement => {
let replacements = {}; let replacements = {};
keys( statement._dependsOn ) keys( statement._dependsOn )

8
src/Module.js

@ -10,12 +10,13 @@ const emptyArrayPromise = Promise.resolve([]);
export default class Module { export default class Module {
constructor ({ path, code, bundle }) { constructor ({ path, code, bundle }) {
this.bundle = bundle;
this.path = path; this.path = path;
this.relativePath = relative( bundle.base, path ).slice( 0, -3 ); // remove .js this.relativePath = relative( bundle.base, path ).slice( 0, -3 ); // remove .js
this.code = new MagicString( code, { this.code = new MagicString( code, {
filename: path filename: path
}); });
this.bundle = bundle;
this.ast = parse( code, { this.ast = parse( code, {
ecmaVersion: 6, ecmaVersion: 6,
@ -54,9 +55,6 @@ export default class Module {
this.imports = {}; this.imports = {};
this.exports = {}; this.exports = {};
// an array of export statements, used for the entry module
this.exportStatements = [];
this.ast.body.forEach( node => { this.ast.body.forEach( node => {
let source; let source;
@ -81,8 +79,6 @@ export default class Module {
} }
else if ( /^Export/.test( node.type ) ) { else if ( /^Export/.test( node.type ) ) {
this.exportStatements.push( node );
// export default function foo () {} // export default function foo () {}
// export default foo; // export default foo;
// export default 42; // export default 42;

Loading…
Cancel
Save