From 9a1ae239199676fc3e5a49bbd65ed01d0d17adbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Thu, 23 Jul 2015 16:32:57 +0200 Subject: [PATCH] Named and * imports must be treated separately. --- src/ExternalModule.js | 7 +++++++ src/Module.js | 2 ++ src/finalisers/es6.js | 35 +++++++++++++++-------------------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/ExternalModule.js b/src/ExternalModule.js index d8c944d..a4f0968 100644 --- a/src/ExternalModule.js +++ b/src/ExternalModule.js @@ -12,7 +12,14 @@ export default class ExternalModule { this.suggestedNames = blank(); this.needsDefault = false; + + // Invariant: needsNamed and needsAll are never both true at once. + // Because an import with both a namespace and named import is invalid: + // + // import * as ns, { a } from '...' + // this.needsNamed = false; + this.needsAll = false; } findDefiningStatement () { diff --git a/src/Module.js b/src/Module.js index 17d35e0..2cc9bb7 100644 --- a/src/Module.js +++ b/src/Module.js @@ -369,6 +369,8 @@ export default class Module { if ( module.isExternal ) { if ( importDeclaration.name === 'default' ) { module.needsDefault = true; + } else if ( importDeclaration.name === '*' ) { + module.needsAll = true; } else { module.needsNamed = true; } diff --git a/src/finalisers/es6.js b/src/finalisers/es6.js index f785e9e..d70283f 100644 --- a/src/finalisers/es6.js +++ b/src/finalisers/es6.js @@ -3,33 +3,28 @@ import { keys } from '../utils/object'; export default function es6 ( bundle, magicString, { exportMode }, options ) { const importBlock = bundle.externalModules .map( module => { - let defaultSpecifier = null; - let namedSpecifiers = null; + const specifiers = []; if ( module.needsDefault ) { - const defaultImportDeclaration = module.importedByBundle.filter( declaration => declaration.name === 'default' )[0]; - defaultSpecifier = defaultImportDeclaration.localName; + specifiers.push( module.importedByBundle.filter( declaration => + declaration.name === 'default' )[0].localName ); } - if ( module.needsNamed ) { - namedSpecifiers = '{ ' + module.importedByBundle - .filter( declaration => declaration.name !== 'default' ) - .map( declaration => { - const { name, localName } = declaration; - - return name === localName ? - name : - `${name} as ${localName}`; - }) - .join( ', ' ) + ' }'; + if ( module.needsAll ) { + specifiers.push( '* as ' + module.importedByBundle.filter( declaration => + declaration.name === '*' )[0].localName ); } - const specifiers = module.needsDefault && module.needsNamed ? - `${defaultSpecifier}, ${namedSpecifiers}` : - ( defaultSpecifier || namedSpecifiers ); + if ( module.needsNamed ) { + specifiers.push( '{ ' + module.importedByBundle + .filter( declaration => !/^(default|\*)$/.test( declaration.name ) ) + .map( ({ name, localName }) => + name === localName ? name : `${name} as ${localName}` ) + .join( ', ' ) + ' }' ); + } - return specifiers ? - `import ${specifiers} from '${module.id}';` : + return specifiers.length ? + `import ${specifiers.join( ', ' )} from '${module.id}';` : `import '${module.id}';`; }) .join( '\n' );