Browse Source

rename external namespace imports correctly

contingency-plan
Rich Harris 10 years ago
parent
commit
0a021c799b
  1. 3
      src/Bundle.js
  2. 5
      src/ExternalModule.js
  3. 9
      src/Module.js
  4. 3
      test/function/import-namespace-from-external-module-renamed/_config.js
  5. 6
      test/function/import-namespace-from-external-module-renamed/main.js

3
src/Bundle.js

@ -105,7 +105,8 @@ export default class Bundle {
// Assign names to external modules
this.externalModules.forEach( module => {
let name = makeLegalIdentifier( module.id );
// TODO is this right?
let name = makeLegalIdentifier( module.suggestedNames['*'] || module.suggestedNames.default || module.id );
if ( has( definers, name ) ) {
conflicts[ name ] = true;

5
src/ExternalModule.js

@ -2,7 +2,6 @@ export default class ExternalModule {
constructor ( id ) {
this.id = id;
this.name = null;
this.module = null;
this.isExternal = true;
this.importedByBundle = [];
@ -10,8 +9,8 @@ export default class ExternalModule {
this.canonicalNames = {};
this.suggestedNames = {};
this.needsDefault = null;
this.needsNamed = null;
this.needsDefault = false;
this.needsNamed = false;
}
getCanonicalName ( name ) {

9
src/Module.js

@ -223,11 +223,17 @@ export default class Module {
.then( module => {
importDeclaration.module = module;
// suggest names. TODO should this apply to non default/* imports?
if ( importDeclaration.name === 'default' ) {
// TODO this seems ropey
const localName = importDeclaration.localName;
const suggestion = has( this.suggestedNames, localName ) ? this.suggestedNames[ localName ] : localName;
module.suggestName( 'default', suggestion );
} else if ( importDeclaration.name === '*' ) {
const localName = importDeclaration.localName;
const suggestion = has( this.suggestedNames, localName ) ? this.suggestedNames[ localName ] : localName;
module.suggestName( '*', suggestion );
module.suggestName( 'default', `${suggestion}__default` );
}
if ( module.isExternal ) {
@ -242,9 +248,6 @@ export default class Module {
}
if ( importDeclaration.name === '*' ) {
module.suggestName( '*', importDeclaration.localName );
module.suggestName( 'default', `${importDeclaration.localName}__default` );
// we need to create an internal namespace
if ( !~this.bundle.internalNamespaceModules.indexOf( module ) ) {
this.bundle.internalNamespaceModules.push( module );

3
test/function/import-namespace-from-external-module-renamed/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'imports a namespace from an external module and renames it'
};

6
test/function/import-namespace-from-external-module-renamed/main.js

@ -0,0 +1,6 @@
import * as node_path from 'path';
var path1 = 'foo/bar/baz';
var path2 = 'foo/baz/bar';
assert.equal( node_path.relative( path1, path2 ), '../../baz/bar' );
Loading…
Cancel
Save