From 3a512f09cd6443efe1c70cfa30cff6fbe765b8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Wed, 26 Aug 2015 15:08:40 +0200 Subject: [PATCH] Do scope lookups when exporting and importing. --- src/finalisers/es6.js | 15 +++++++-------- src/finalisers/shared/getExportBlock.js | 4 ++-- src/finalisers/shared/getInteropBlock.js | 12 +++++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/finalisers/es6.js b/src/finalisers/es6.js index 0ed804c..ac928ed 100644 --- a/src/finalisers/es6.js +++ b/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 ) { diff --git a/src/finalisers/shared/getExportBlock.js b/src/finalisers/shared/getExportBlock.js index 24eeb61..f9d4ea7 100644 --- a/src/finalisers/shared/getExportBlock.js +++ b/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' ); } diff --git a/src/finalisers/shared/getInteropBlock.js b/src/finalisers/shared/getInteropBlock.js index 8060e00..b913519 100644 --- a/src/finalisers/shared/getInteropBlock.js +++ b/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' );