Browse Source

Named and * imports must be treated separately.

contingency-plan
Oskar Segersvärd 10 years ago
parent
commit
9a1ae23919
  1. 7
      src/ExternalModule.js
  2. 2
      src/Module.js
  3. 35
      src/finalisers/es6.js

7
src/ExternalModule.js

@ -12,7 +12,14 @@ export default class ExternalModule {
this.suggestedNames = blank(); this.suggestedNames = blank();
this.needsDefault = false; 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.needsNamed = false;
this.needsAll = false;
} }
findDefiningStatement () { findDefiningStatement () {

2
src/Module.js

@ -369,6 +369,8 @@ export default class Module {
if ( module.isExternal ) { if ( module.isExternal ) {
if ( importDeclaration.name === 'default' ) { if ( importDeclaration.name === 'default' ) {
module.needsDefault = true; module.needsDefault = true;
} else if ( importDeclaration.name === '*' ) {
module.needsAll = true;
} else { } else {
module.needsNamed = true; module.needsNamed = true;
} }

35
src/finalisers/es6.js

@ -3,33 +3,28 @@ import { keys } from '../utils/object';
export default function es6 ( bundle, magicString, { exportMode }, options ) { export default function es6 ( bundle, magicString, { exportMode }, options ) {
const importBlock = bundle.externalModules const importBlock = bundle.externalModules
.map( module => { .map( module => {
let defaultSpecifier = null; const specifiers = [];
let namedSpecifiers = null;
if ( module.needsDefault ) { if ( module.needsDefault ) {
const defaultImportDeclaration = module.importedByBundle.filter( declaration => declaration.name === 'default' )[0]; specifiers.push( module.importedByBundle.filter( declaration =>
defaultSpecifier = defaultImportDeclaration.localName; declaration.name === 'default' )[0].localName );
} }
if ( module.needsNamed ) { if ( module.needsAll ) {
namedSpecifiers = '{ ' + module.importedByBundle specifiers.push( '* as ' + module.importedByBundle.filter( declaration =>
.filter( declaration => declaration.name !== 'default' ) declaration.name === '*' )[0].localName );
.map( declaration => {
const { name, localName } = declaration;
return name === localName ?
name :
`${name} as ${localName}`;
})
.join( ', ' ) + ' }';
} }
const specifiers = module.needsDefault && module.needsNamed ? if ( module.needsNamed ) {
`${defaultSpecifier}, ${namedSpecifiers}` : specifiers.push( '{ ' + module.importedByBundle
( defaultSpecifier || namedSpecifiers ); .filter( declaration => !/^(default|\*)$/.test( declaration.name ) )
.map( ({ name, localName }) =>
name === localName ? name : `${name} as ${localName}` )
.join( ', ' ) + ' }' );
}
return specifiers ? return specifiers.length ?
`import ${specifiers} from '${module.id}';` : `import ${specifiers.join( ', ' )} from '${module.id}';` :
`import '${module.id}';`; `import '${module.id}';`;
}) })
.join( '\n' ); .join( '\n' );

Loading…
Cancel
Save