Browse Source

make getCanonicalName es6-aware

contingency-plan
Rich-Harris 10 years ago
parent
commit
069f83bec8
  1. 28
      src/Bundle.js
  2. 9
      src/ExternalModule.js
  3. 15
      src/Module.js

28
src/Bundle.js

@ -13,11 +13,11 @@ import getExportMode from './utils/getExportMode';
import getIndentString from './utils/getIndentString';
import { unixizePath } from './utils/normalizePlatform.js';
function isEmptyExportedVarDeclaration ( node, module, allBundleExports ) {
function isEmptyExportedVarDeclaration ( node, module, allBundleExports, es6 ) {
if ( node.type !== 'VariableDeclaration' || node.declarations[0].init ) return false;
const name = node.declarations[0].id.name;
const canonicalName = module.getCanonicalName( name );
const canonicalName = module.getCanonicalName( name, es6 );
return canonicalName in allBundleExports;
}
@ -91,11 +91,10 @@ export default class Bundle {
})
.then( () => {
this.statements = this.sort();
this.deconflict();
});
}
deconflict () {
deconflict ( es6 ) {
let definers = blank();
let conflicts = blank();
@ -124,10 +123,10 @@ export default class Bundle {
// we need to ensure that the name chosen for the expression does
// not conflict
if ( statement.node.type === 'ExportDefaultDeclaration' ) {
const name = module.getCanonicalName( 'default' );
const name = module.getCanonicalName( 'default', es6 );
const isProxy = statement.node.declaration && statement.node.declaration.type === 'Identifier';
const shouldDeconflict = !isProxy || ( module.getCanonicalName( statement.node.declaration.name ) !== name );
const shouldDeconflict = !isProxy || ( module.getCanonicalName( statement.node.declaration.name, es6 ) !== name );
if ( shouldDeconflict && !~names.indexOf( name ) ) {
names.push( name );
@ -218,6 +217,7 @@ export default class Bundle {
let magicString = new MagicString.Bundle({ separator: '' });
const format = options.format || 'es6';
this.deconflict( format === 'es6' );
// If we have named exports from the bundle, and those exports
// are assigned to *within* the bundle, we may need to rewrite e.g.
@ -243,7 +243,7 @@ export default class Bundle {
const originalDeclaration = this.entryModule.findDeclaration( exportDeclaration.localName );
if ( originalDeclaration && originalDeclaration.type === 'VariableDeclaration' ) {
const canonicalName = this.entryModule.getCanonicalName( exportDeclaration.localName );
const canonicalName = this.entryModule.getCanonicalName( exportDeclaration.localName, false );
allBundleExports[ canonicalName ] = `exports.${key}`;
this.varExports[ key ] = true;
@ -268,12 +268,12 @@ export default class Bundle {
if ( statement.node.specifiers.length ) return;
// skip `export var foo;` if foo is exported
if ( isEmptyExportedVarDeclaration( statement.node.declaration, statement.module, allBundleExports ) ) return;
if ( isEmptyExportedVarDeclaration( statement.node.declaration, statement.module, allBundleExports, format === 'es6' ) ) return;
}
// skip empty var declarations for exported bindings
// (otherwise we're left with `exports.foo;`, which is useless)
if ( isEmptyExportedVarDeclaration( statement.node, statement.module, allBundleExports ) ) return;
if ( isEmptyExportedVarDeclaration( statement.node, statement.module, allBundleExports, format === 'es6' ) ) return;
let replacements = blank();
let bundleExports = blank();
@ -281,7 +281,7 @@ export default class Bundle {
keys( statement.dependsOn )
.concat( keys( statement.defines ) )
.forEach( name => {
const canonicalName = statement.module.getCanonicalName( name );
const canonicalName = statement.module.getCanonicalName( name, format === 'es6' );
if ( allBundleExports[ canonicalName ] ) {
bundleExports[ name ] = replacements[ name ] = allBundleExports[ canonicalName ];
@ -307,9 +307,9 @@ export default class Bundle {
else if ( statement.node.type === 'ExportDefaultDeclaration' ) {
const module = statement.module;
const canonicalName = module.getCanonicalName( 'default' );
const canonicalName = module.getCanonicalName( 'default', format === 'es6' );
if ( statement.node.declaration.type === 'Identifier' && canonicalName === module.getCanonicalName( statement.node.declaration.name ) ) {
if ( statement.node.declaration.type === 'Identifier' && canonicalName === module.getCanonicalName( statement.node.declaration.name, format === 'es6' ) ) {
return;
}
@ -370,8 +370,8 @@ export default class Bundle {
const namespaceBlock = this.internalNamespaceModules.map( module => {
const exportKeys = keys( module.exports );
return `var ${module.getCanonicalName('*')} = {\n` +
exportKeys.map( key => `${indentString}get ${key} () { return ${module.getCanonicalName(key)}; }` ).join( ',\n' ) +
return `var ${module.getCanonicalName('*', format === 'es6')} = {\n` +
exportKeys.map( key => `${indentString}get ${key} () { return ${module.getCanonicalName(key, format === 'es6')}; }` ).join( ',\n' ) +
`\n};\n\n`;
}).join( '' );

9
src/ExternalModule.js

@ -26,17 +26,16 @@ export default class ExternalModule {
return null;
}
getCanonicalName ( name ) {
getCanonicalName ( name, es6 ) {
if ( name === 'default' ) {
return this.needsNamed ? `${this.name}__default` : this.name;
return this.needsNamed && !es6 ? `${this.name}__default` : this.name;
}
if ( name === '*' ) {
return this.name;
return this.name; // TODO is this correct in ES6?
}
// TODO this depends on the output format... works for CJS etc but not ES6
return `${this.name}.${name}`;
return es6 ? ( this.canonicalNames[ name ] || name ) : `${this.name}.${name}`;
}
rename ( name, replacement ) {

15
src/Module.js

@ -281,7 +281,7 @@ export default class Module {
return null;
}
getCanonicalName ( localName ) {
getCanonicalName ( localName, es6 ) {
// Special case
if ( localName === 'default' && ( this.exports.default.isModified || !this.suggestedNames.default ) ) {
let canonicalName = makeLegalIdentifier( this.id.replace( dirname( this.bundle.entryModule.id ) + '/', '' ).replace( /\.js$/, '' ) );
@ -292,7 +292,9 @@ export default class Module {
localName = this.suggestedNames[ localName ];
}
if ( !this.canonicalNames[ localName ] ) {
const id = localName + ( es6 ? '-es6' : '' ); // TODO ugh this seems like a terrible hack
if ( !this.canonicalNames[ id ] ) {
let canonicalName;
if ( this.imports[ localName ] ) {
@ -317,7 +319,7 @@ export default class Module {
}
}
canonicalName = module.getCanonicalName( exporterLocalName );
canonicalName = module.getCanonicalName( exporterLocalName, es6 );
}
}
@ -325,10 +327,10 @@ export default class Module {
canonicalName = localName;
}
this.canonicalNames[ localName ] = canonicalName;
this.canonicalNames[ id ] = canonicalName;
}
return this.canonicalNames[ localName ];
return this.canonicalNames[ id ];
}
mark ( name ) {
@ -563,7 +565,8 @@ export default class Module {
}
rename ( name, replacement ) {
this.canonicalNames[ name ] = replacement;
// TODO again, hacky...
this.canonicalNames[ name ] = this.canonicalNames[ name + '-es6' ] = replacement;
}
suggestName ( defaultOrBatch, suggestion ) {

Loading…
Cancel
Save