From 20638dbe483005d357a7abb69af14a5b9881bd84 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 3 Oct 2015 12:42:02 -0400 Subject: [PATCH] more fixes --- src/Bundle.js | 5 ++--- src/ExternalModule.js | 27 +++++++++++++++++++++++++-- src/Module.js | 15 ++++++++++++++- src/ast/Scope.js | 7 ++++--- src/finalisers/es6.js | 2 +- 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/Bundle.js b/src/Bundle.js index 6f87104..9ee74c9 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -40,7 +40,7 @@ export default class Bundle { this.internalNamespaceModules = []; this.assumedGlobals = blank(); - + // TODO strictly speaking, this only applies with non-ES6, non-default-only bundles [ 'module', 'exports' ].forEach( global => this.assumedGlobals[ global ] = true ); } @@ -180,7 +180,6 @@ export default class Bundle { render ( options = {} ) { const format = options.format || 'es6'; - const allReplacements = this.deconflict( format === 'es6' ); // Determine export mode - 'default', 'named', 'none' const exportMode = getExportMode( this, options.exports ); @@ -188,7 +187,7 @@ export default class Bundle { let magicString = new MagicString.Bundle({ separator: '\n\n' }); this.orderedModules.forEach( module => { - const source = module.render(); + const source = module.render( format === 'es6' ); if ( source.toString().length ) { magicString.addSource( source ); } diff --git a/src/ExternalModule.js b/src/ExternalModule.js index aabf9df..c981c5e 100644 --- a/src/ExternalModule.js +++ b/src/ExternalModule.js @@ -1,12 +1,23 @@ import { blank } from './utils/object'; +import makeLegalIdentifier from './utils/makeLegalIdentifier'; + +class ExternalDeclaration { + constructor ( module, name ) { + this.module = module; + this.name = name; + this.isExternal = true; + + this.references = []; + } +} export default class ExternalModule { constructor ( id ) { this.id = id; - this.name = null; + this.name = makeLegalIdentifier( id ); this.isExternal = true; - this.importedByBundle = []; + this.declarations = blank(); this.suggestedNames = blank(); @@ -34,4 +45,16 @@ export default class ExternalModule { this.suggestedNames[ exportName ] = suggestion; } } + + traceExport ( name ) { + if ( name === 'default' ) { + this.needsDefault = true; + } else { + this.needsNamed = true; + } + + return this.declarations[ name ] || ( + this.declarations[ name ] = new ExternalDeclaration( this, name ) + ); + } } diff --git a/src/Module.js b/src/Module.js index 9cb012a..45e2592 100644 --- a/src/Module.js +++ b/src/Module.js @@ -508,7 +508,7 @@ export default class Module { return statements; } - render () { + render ( es6 ) { let magicString = this.magicString.clone(); this.statements.forEach( statement => { @@ -516,6 +516,19 @@ export default class Module { magicString.remove( statement.start, statement.next ); } + statement.references.forEach( reference => { + const declaration = reference.declaration; + + if ( reference.declaration ) { + const { start, end } = reference.node; + const name = ( !es6 && declaration.isExternal ) ? + `${declaration.module.name}.${declaration.name}` : + declaration.name; + + magicString.overwrite( start, end, name ); + } + }); + // modify exports as necessary if ( statement.isExportDeclaration ) { // remove `export` from `export class Foo {...}` or `export default Foo` diff --git a/src/ast/Scope.js b/src/ast/Scope.js index f64133b..c5ef05c 100644 --- a/src/ast/Scope.js +++ b/src/ast/Scope.js @@ -34,9 +34,10 @@ function extractNames ( param ) { } class Declaration { - constructor () { + constructor ( name ) { this.references = []; this.statement = null; + this.name = name; } } @@ -52,7 +53,7 @@ export default class Scope { if ( options.params ) { options.params.forEach( param => { extractNames( param ).forEach( name => { - this.declarations[ name ] = new Declaration(); + this.declarations[ name ] = new Declaration( name ); }); }); } @@ -65,7 +66,7 @@ export default class Scope { this.parent.addDeclaration( node, isBlockDeclaration, isVar ); } else { extractNames( node.id ).forEach( name => { - this.declarations[ name ] = new Declaration(); + this.declarations[ name ] = new Declaration( name ); }); } } diff --git a/src/finalisers/es6.js b/src/finalisers/es6.js index 0ed804c..a005e91 100644 --- a/src/finalisers/es6.js +++ b/src/finalisers/es6.js @@ -30,7 +30,7 @@ export default function es6 ( bundle, magicString ) { } if ( module.needsNamed ) { - specifiers.push( '{ ' + uniqueNames( module.importedByBundle ) + specifiers.push( '{ ' + keys( module.declarations ) .join( ', ' ) + ' }' ); }