Browse Source

Do scope lookups when exporting and importing.

gh-109
Oskar Segersvärd 10 years ago
parent
commit
3a512f09cd
  1. 15
      src/finalisers/es6.js
  2. 4
      src/finalisers/shared/getExportBlock.js
  3. 12
      src/finalisers/shared/getInteropBlock.js

15
src/finalisers/es6.js

@ -19,9 +19,8 @@ export default function es6 ( bundle, magicString ) {
.map( module => {
const specifiers = [];
if ( module.needsDefault ) {
specifiers.push( module.importedByBundle.filter( declaration =>
declaration.name === 'default' )[0].localName );
if ( module.exports.inScope( 'default' ) ) {
specifiers.push( module.exports.lookup( 'default' ).name );
}
if ( module.needsAll ) {
@ -47,18 +46,18 @@ export default function es6 ( bundle, magicString ) {
const module = bundle.entryModule;
const specifiers = bundle.toExport.filter( notDefault ).map( name => {
const canonicalName = bundle.traceExport( module, name );
const id = bundle.exports.lookup( name );
return canonicalName === name ?
return id.name === name ?
name :
`${canonicalName} as ${name}`;
`${id.name} as ${name}`;
});
let exportBlock = specifiers.length ? `export { ${specifiers.join(', ')} };` : '';
const defaultExport = module.exports.default || module.reexports.default;
const defaultExport = module.exports.lookup( 'default' );
if ( defaultExport ) {
exportBlock += `export default ${bundle.traceExport(module,'default')};`;
exportBlock += `export default ${ defaultExport.name };`;
}
if ( exportBlock ) {

4
src/finalisers/shared/getExportBlock.js

@ -11,8 +11,8 @@ export default function getExportBlock ( bundle, exportMode, mechanism = 'return
return bundle.toExport
.map( name => {
const prop = name === 'default' ? `['default']` : `.${name}`;
name = bundle.traceExport( bundle.entryModule, name );
return `exports${prop} = ${name};`;
const id = bundle.exports.lookup( name );
return `exports${prop} = ${id.name};`;
})
.join( '\n' );
}

12
src/finalisers/shared/getInteropBlock.js

@ -1,11 +1,13 @@
export default function getInteropBlock ( bundle ) {
return bundle.externalModules
.map( module => {
return module.needsDefault ?
( module.needsNamed ?
`var ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` :
`${module.name} = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` ) :
null;
if ( !module.needsDefault ) return;
if ( module.needsNamed ) {
return `var ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};`;
} else {
return `${module.name} = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};`;
}
})
.filter( Boolean )
.join( '\n' );

Loading…
Cancel
Save