Browse Source

more fixes

declarations-and-references
Rich-Harris 9 years ago
parent
commit
f49c5d3fda
  1. 8
      src/Bundle.js
  2. 30
      src/ExternalModule.js
  3. 9
      src/Module.js

8
src/Bundle.js

@ -156,10 +156,10 @@ export default class Bundle {
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;
});
// 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' });

30
src/ExternalModule.js

@ -4,9 +4,7 @@ import makeLegalIdentifier from './utils/makeLegalIdentifier';
class ExternalDeclaration {
constructor ( module, name ) {
this.module = module;
this.importedAs = name;
this.name = null;
this.name = name;
this.isExternal = true;
this.references = [];
@ -14,21 +12,24 @@ class ExternalDeclaration {
addReference ( reference ) {
reference.declaration = this;
this.name = reference.name;
if ( this.name === 'default' || this.name === '*' ) {
this.module.suggestName( reference.name );
}
}
getName () {
if ( this.importedAs === '*' ) {
getName ( es6 ) {
if ( this.name === '*' ) {
return this.module.name;
}
if ( this.importedAs === 'default' ) {
if ( this.name === 'default' ) {
return this.module.needsNamed ?
`${this.module.name}__default` :
this.module.name;
}
return `${this.module.name}.${this.name}`;
return es6 ? this.name : `${this.module.name}.${this.name}`;
}
}
@ -37,6 +38,9 @@ export default class ExternalModule {
this.id = id;
this.name = makeLegalIdentifier( id );
this.nameSuggestions = blank();
this.mostCommonSuggestion = 0;
this.isExternal = true;
this.declarations = blank();
@ -51,6 +55,16 @@ export default class ExternalModule {
this.needsAll = false;
}
suggestName ( name ) {
if ( !this.nameSuggestions[ name ] ) this.nameSuggestions[ name ] = 0;
this.nameSuggestions[ name ] += 1;
if ( this.nameSuggestions[ name ] > this.mostCommonSuggestion ) {
this.mostCommonSuggestion = this.nameSuggestions[ name ];
this.name = name;
}
}
traceExport ( name ) {
// TODO is this necessary? where is it used?
if ( name === 'default' ) {

9
src/Module.js

@ -522,8 +522,8 @@ export default class Module {
if ( reference.declaration ) {
const { start } = reference.node;
const name = ( !es6 && declaration.isExternal ) ?
declaration.getName() :
const name = declaration.isExternal ?
declaration.getName( es6 ) :
declaration.name;
magicString.overwrite( start, start + reference.name.length, name );
@ -537,6 +537,11 @@ export default class Module {
magicString.remove( statement.node.start, statement.node.declaration.start );
}
else if ( statement.node.type === 'ExportAllDeclaration' ) {
// TODO: remove once `export * from 'external'` is supported.
magicString.remove( statement.start, statement.next );
}
// remove `export` from `export class Foo {...}` or `export default Foo`
// TODO default exports need different treatment
else if ( statement.node.declaration.id ) {

Loading…
Cancel
Save