Browse Source

more fixes

declarations-and-references
Rich-Harris 9 years ago
parent
commit
9c3409f593
  1. 6
      src/Bundle.js
  2. 42
      src/ExternalModule.js
  3. 4
      src/Module.js
  4. 9
      src/ast/Scope.js
  5. 17
      src/finalisers/es6.js

6
src/Bundle.js

@ -155,6 +155,12 @@ export default class Bundle {
// Determine export mode - 'default', 'named', 'none' // Determine export mode - 'default', 'named', 'none'
const exportMode = getExportMode( this, options.exports ); const exportMode = getExportMode( this, options.exports );
// Assign names to external modules
this.externalModules.forEach( module => {
const override = module.declarations['*'] || module.declarations.default;
if ( override ) module.name = override.name;
});
let magicString = new MagicString.Bundle({ separator: '\n\n' }); let magicString = new MagicString.Bundle({ separator: '\n\n' });
this.orderedModules.forEach( module => { this.orderedModules.forEach( module => {

42
src/ExternalModule.js

@ -4,11 +4,32 @@ import makeLegalIdentifier from './utils/makeLegalIdentifier';
class ExternalDeclaration { class ExternalDeclaration {
constructor ( module, name ) { constructor ( module, name ) {
this.module = module; this.module = module;
this.name = name; this.importedAs = name;
this.name = null;
this.isExternal = true; this.isExternal = true;
this.references = []; this.references = [];
} }
addReference ( reference ) {
reference.declaration = this;
this.name = reference.name;
}
getName () {
if ( this.importedAs === '*' ) {
return this.module.name;
}
if ( this.importedAs === 'default' ) {
return this.module.needsNamed ?
`${this.module.name}__default` :
this.module.name;
}
return `${this.module.name}.${this.name}`;
}
} }
export default class ExternalModule { export default class ExternalModule {
@ -19,8 +40,6 @@ export default class ExternalModule {
this.isExternal = true; this.isExternal = true;
this.declarations = blank(); this.declarations = blank();
this.suggestedNames = blank();
this.needsDefault = false; this.needsDefault = false;
// Invariant: needsNamed and needsAll are never both true at once. // Invariant: needsNamed and needsAll are never both true at once.
@ -32,23 +51,12 @@ export default class ExternalModule {
this.needsAll = false; this.needsAll = false;
} }
findDefiningStatement () {
return null;
}
rename () {
// noop
}
suggestName ( exportName, suggestion ) {
if ( !this.suggestedNames[ exportName ] ) {
this.suggestedNames[ exportName ] = suggestion;
}
}
traceExport ( name ) { traceExport ( name ) {
// TODO is this necessary? where is it used?
if ( name === 'default' ) { if ( name === 'default' ) {
this.needsDefault = true; this.needsDefault = true;
} else if ( name === '*' ) {
this.needsAll = true;
} else { } else {
this.needsNamed = true; this.needsNamed = true;
} }

4
src/Module.js

@ -215,7 +215,7 @@ export default class Module {
if ( declaration ) { if ( declaration ) {
reference.declaration = declaration; reference.declaration = declaration;
declaration.references.push( reference ); declaration.addReference( reference );
} else { } else {
// TODO handle globals // TODO handle globals
this.bundle.assumedGlobals[ reference.name ] = true; this.bundle.assumedGlobals[ reference.name ] = true;
@ -523,7 +523,7 @@ export default class Module {
if ( reference.declaration ) { if ( reference.declaration ) {
const { start } = reference.node; const { start } = reference.node;
const name = ( !es6 && declaration.isExternal ) ? const name = ( !es6 && declaration.isExternal ) ?
`${declaration.module.name}.${declaration.name}` : declaration.getName() :
declaration.name; declaration.name;
magicString.overwrite( start, start + reference.name.length, name ); magicString.overwrite( start, start + reference.name.length, name );

9
src/ast/Scope.js

@ -34,10 +34,15 @@ function extractNames ( param ) {
} }
class Declaration { class Declaration {
constructor ( name ) { constructor () {
this.references = []; this.references = [];
this.statement = null; this.statement = null;
this.name = name; this.name = null;
}
addReference ( reference ) {
reference.declaration = this;
this.name = reference.name; // TODO handle differences of opinion
} }
} }

17
src/finalisers/es6.js

@ -18,20 +18,19 @@ export default function es6 ( bundle, magicString ) {
const importBlock = bundle.externalModules const importBlock = bundle.externalModules
.map( module => { .map( module => {
const specifiers = []; const specifiers = [];
const importedNames = keys( module.declarations )
.filter( name => name !== '*' && name !== 'default' );
if ( module.needsDefault ) { if ( module.declarations.default ) {
specifiers.push( module.importedByBundle.filter( declaration => specifiers.push( module.name );
declaration.name === 'default' )[0].localName );
} }
if ( module.needsAll ) { if ( module.declarations['*'] ) {
specifiers.push( '* as ' + module.importedByBundle.filter( declaration => specifiers.push( `* as ${module.name}` );
declaration.name === '*' )[0].localName );
} }
if ( module.needsNamed ) { if ( importedNames.length ) {
specifiers.push( '{ ' + keys( module.declarations ) specifiers.push( `{ ${importedNames.join( ', ' )} }` );
.join( ', ' ) + ' }' );
} }
return specifiers.length ? return specifiers.length ?

Loading…
Cancel
Save