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.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 () {

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

35
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' );

Loading…
Cancel
Save