Browse Source

fix es6 export

contingency-plan
Rich-Harris 10 years ago
parent
commit
13521adec8
  1. 8
      src/Bundle.js
  2. 8
      src/Module.js
  3. 37
      src/finalisers/es6.js
  4. 4
      test/form/exported-empty-vars/_expected/es6.js
  5. 3
      test/form/multiple-exports/_expected/es6.js

8
src/Bundle.js

@ -310,10 +310,10 @@ export default class Bundle {
const reexportDeclaration = this.entryModule.reexports[ key ]; const reexportDeclaration = this.entryModule.reexports[ key ];
if ( reexportDeclaration.module.isExternal ) return; if ( reexportDeclaration.module.isExternal ) return;
const originalDeclaration = reexportDeclaration.module.findDeclaration( reexportDeclaration.importedName ); const originalDeclaration = reexportDeclaration.module.findDeclaration( reexportDeclaration.localName );
if ( originalDeclaration && originalDeclaration.type === 'VariableDeclaration' ) { 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}`; allBundleExports[ canonicalName ] = `exports.${key}`;
varExports[ key ] = true; varExports[ key ] = true;
@ -356,7 +356,7 @@ export default class Bundle {
while ( exportingModule.reexports[ key ] ) { while ( exportingModule.reexports[ key ] ) {
reexport = exportingModule.reexports[ key ]; reexport = exportingModule.reexports[ key ];
exportingModule = reexport.module; exportingModule = reexport.module;
key = reexport.importedName; key = reexport.localName;
} }
key = exportingModule.replacements[ key ] || key; key = exportingModule.replacements[ key ] || key;
@ -547,7 +547,7 @@ export default class Bundle {
`${reexportDeclaration.module.name}.${name}`; `${reexportDeclaration.module.name}.${name}`;
} }
return this.traceExport( reexportDeclaration.module, reexportDeclaration.importedName ); return this.traceExport( reexportDeclaration.module, reexportDeclaration.localName );
} }
const exportDeclaration = module.exports[ name ]; const exportDeclaration = module.exports[ name ];

8
src/Module.js

@ -90,7 +90,7 @@ export default class Module {
node.specifiers.forEach( specifier => { node.specifiers.forEach( specifier => {
this.reexports[ specifier.exported.name ] = { this.reexports[ specifier.exported.name ] = {
source, source,
importedName: specifier.local.name, localName: specifier.local.name,
module: null // filled in later module: null // filled in later
}; };
}); });
@ -208,6 +208,8 @@ export default class Module {
// if names are referenced that are neither defined nor imported // if names are referenced that are neither defined nor imported
// in this module, we assume that they're globals // in this module, we assume that they're globals
this.statements.forEach( statement => { this.statements.forEach( statement => {
if ( statement.isExportDeclaration ) return;
keys( statement.dependsOn ).forEach( name => { keys( statement.dependsOn ).forEach( name => {
if ( !this.definitions[ name ] && !this.imports[ name ] ) { if ( !this.definitions[ name ] && !this.imports[ name ] ) {
this.bundle.assumedGlobals[ name ] = true; this.bundle.assumedGlobals[ name ] = true;
@ -242,7 +244,7 @@ export default class Module {
while ( !module.isExternal && module.reexports[ name ] && module.reexports[ name ].isUsed ) { while ( !module.isExternal && module.reexports[ name ] && module.reexports[ name ].isUsed ) {
reexport = module.reexports[ name ]; reexport = module.reexports[ name ];
module = reexport.module; module = reexport.module;
name = reexport.importedName; name = reexport.localName;
} }
addDependency( strongDependencies, reexport ); addDependency( strongDependencies, reexport );
@ -452,7 +454,7 @@ export default class Module {
return this.bundle.fetchModule( reexportDeclaration.source, this.id ) return this.bundle.fetchModule( reexportDeclaration.source, this.id )
.then( otherModule => { .then( otherModule => {
reexportDeclaration.module = otherModule; reexportDeclaration.module = otherModule;
return otherModule.markExport( reexportDeclaration.importedName, suggestedName, this ); return otherModule.markExport( reexportDeclaration.localName, suggestedName, this );
}); });
} }

37
src/finalisers/es6.js

@ -10,6 +10,22 @@ function uniqueNames ( declarations ) {
return keys( uniques ); 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 ) { export default function es6 ( bundle, magicString ) {
const importBlock = bundle.externalModules const importBlock = bundle.externalModules
.map( module => { .map( module => {
@ -40,23 +56,20 @@ export default function es6 ( bundle, magicString ) {
magicString.prepend( importBlock + '\n\n' ); magicString.prepend( importBlock + '\n\n' );
} }
const exports = bundle.entryModule.exports; const module = bundle.entryModule;
const exportBlock = keys( exports ).map( exportedName => {
const specifier = exports[ exportedName ];
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' ) { let exportBlock = specifiers.length ? `export { ${specifiers.join(', ')} };` : '';
return `export default ${canonicalName};`;
}
return exportedName === canonicalName ? const defaultExport = module.exports.default || module.reexports.default;
`export { ${exportedName} };` : if ( defaultExport ) {
`export { ${canonicalName} as ${exportedName} };`; exportBlock += `export default ${bundle.traceExport(module,'default')};`;
}).join( '\n' ); }
if ( exportBlock ) { if ( exportBlock ) {
magicString.append( '\n\n' + exportBlock ); magicString.append( '\n\n' + exportBlock.trim() );
} }
return magicString.trim(); return magicString.trim();

4
test/form/exported-empty-vars/_expected/es6.js

@ -6,6 +6,4 @@ var baz;
bar = 43; bar = 43;
baz = 44; baz = 44;
export { foo }; export { foo, bar, baz };
export { bar };
export { baz };

3
test/form/multiple-exports/_expected/es6.js

@ -1,5 +1,4 @@
var foo = 1; var foo = 1;
var bar = 2; var bar = 2;
export { foo }; export { foo, bar };
export { bar };

Loading…
Cancel
Save