diff --git a/src/Bundle.js b/src/Bundle.js index 46c8fff..070eabe 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -310,10 +310,10 @@ export default class Bundle { const reexportDeclaration = this.entryModule.reexports[ key ]; if ( reexportDeclaration.module.isExternal ) return; - const originalDeclaration = reexportDeclaration.module.findDeclaration( reexportDeclaration.importedName ); + const originalDeclaration = reexportDeclaration.module.findDeclaration( reexportDeclaration.localName ); if ( originalDeclaration && originalDeclaration.type === 'VariableDeclaration' ) { - const canonicalName = this.trace( reexportDeclaration.module, reexportDeclaration.importedName, false ); + const canonicalName = this.trace( reexportDeclaration.module, reexportDeclaration.localName, false ); allBundleExports[ canonicalName ] = `exports.${key}`; varExports[ key ] = true; @@ -356,7 +356,7 @@ export default class Bundle { while ( exportingModule.reexports[ key ] ) { reexport = exportingModule.reexports[ key ]; exportingModule = reexport.module; - key = reexport.importedName; + key = reexport.localName; } key = exportingModule.replacements[ key ] || key; @@ -547,7 +547,7 @@ export default class Bundle { `${reexportDeclaration.module.name}.${name}`; } - return this.traceExport( reexportDeclaration.module, reexportDeclaration.importedName ); + return this.traceExport( reexportDeclaration.module, reexportDeclaration.localName ); } const exportDeclaration = module.exports[ name ]; diff --git a/src/Module.js b/src/Module.js index a004182..909acf3 100644 --- a/src/Module.js +++ b/src/Module.js @@ -90,7 +90,7 @@ export default class Module { node.specifiers.forEach( specifier => { this.reexports[ specifier.exported.name ] = { source, - importedName: specifier.local.name, + localName: specifier.local.name, module: null // filled in later }; }); @@ -208,6 +208,8 @@ export default class Module { // if names are referenced that are neither defined nor imported // in this module, we assume that they're globals this.statements.forEach( statement => { + if ( statement.isExportDeclaration ) return; + keys( statement.dependsOn ).forEach( name => { if ( !this.definitions[ name ] && !this.imports[ name ] ) { this.bundle.assumedGlobals[ name ] = true; @@ -242,7 +244,7 @@ export default class Module { while ( !module.isExternal && module.reexports[ name ] && module.reexports[ name ].isUsed ) { reexport = module.reexports[ name ]; module = reexport.module; - name = reexport.importedName; + name = reexport.localName; } addDependency( strongDependencies, reexport ); @@ -452,7 +454,7 @@ export default class Module { return this.bundle.fetchModule( reexportDeclaration.source, this.id ) .then( otherModule => { reexportDeclaration.module = otherModule; - return otherModule.markExport( reexportDeclaration.importedName, suggestedName, this ); + return otherModule.markExport( reexportDeclaration.localName, suggestedName, this ); }); } diff --git a/src/finalisers/es6.js b/src/finalisers/es6.js index ba6a528..c612d87 100644 --- a/src/finalisers/es6.js +++ b/src/finalisers/es6.js @@ -10,6 +10,22 @@ function uniqueNames ( declarations ) { return keys( uniques ); } +function notDefault ( name ) { + return name !== 'default'; +} + +function getSpecifiers ( exports, replacements ) { + return keys( exports ).filter( notDefault ).map( name => { + const specifier = exports[ name ]; + console.log( 'specifier', specifier ) + const canonicalName = replacements[ specifier.localName ] || specifier.localName; + + return canonicalName === name ? + name : + `${canonicalName} as ${name}`; + }); +} + export default function es6 ( bundle, magicString ) { const importBlock = bundle.externalModules .map( module => { @@ -40,23 +56,20 @@ export default function es6 ( bundle, magicString ) { magicString.prepend( importBlock + '\n\n' ); } - const exports = bundle.entryModule.exports; - const exportBlock = keys( exports ).map( exportedName => { - const specifier = exports[ exportedName ]; + const module = bundle.entryModule; - const canonicalName = bundle.entryModule.replacements[ specifier.localName ] || specifier.localName; + const specifiers = getSpecifiers( module.exports, module.replacements ) + .concat( getSpecifiers( module.reexports, module.replacements ) ); - if ( exportedName === 'default' ) { - return `export default ${canonicalName};`; - } + let exportBlock = specifiers.length ? `export { ${specifiers.join(', ')} };` : ''; - return exportedName === canonicalName ? - `export { ${exportedName} };` : - `export { ${canonicalName} as ${exportedName} };`; - }).join( '\n' ); + const defaultExport = module.exports.default || module.reexports.default; + if ( defaultExport ) { + exportBlock += `export default ${bundle.traceExport(module,'default')};`; + } if ( exportBlock ) { - magicString.append( '\n\n' + exportBlock ); + magicString.append( '\n\n' + exportBlock.trim() ); } return magicString.trim(); diff --git a/test/form/exported-empty-vars/_expected/es6.js b/test/form/exported-empty-vars/_expected/es6.js index 3be564f..4b76753 100644 --- a/test/form/exported-empty-vars/_expected/es6.js +++ b/test/form/exported-empty-vars/_expected/es6.js @@ -6,6 +6,4 @@ var baz; bar = 43; baz = 44; -export { foo }; -export { bar }; -export { baz }; +export { foo, bar, baz }; diff --git a/test/form/multiple-exports/_expected/es6.js b/test/form/multiple-exports/_expected/es6.js index a46872c..2822603 100644 --- a/test/form/multiple-exports/_expected/es6.js +++ b/test/form/multiple-exports/_expected/es6.js @@ -1,5 +1,4 @@ var foo = 1; var bar = 2; -export { foo }; -export { bar }; +export { foo, bar };