Browse Source

fix es6 export

contingency-plan
Rich-Harris 9 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 ];
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 ];

8
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 );
});
}

37
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();

4
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 };

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

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

Loading…
Cancel
Save