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 ); const exportMode = getExportMode( this, options.exports );
// Assign names to external modules // Assign names to external modules
this.externalModules.forEach( module => { // this.externalModules.forEach( module => {
const override = module.declarations['*'] || module.declarations.default; // const override = module.declarations['*'] || module.declarations.default;
if ( override ) module.name = override.name; // if ( override ) module.name = override.name;
}); // });
let magicString = new MagicString.Bundle({ separator: '\n\n' }); let magicString = new MagicString.Bundle({ separator: '\n\n' });

30
src/ExternalModule.js

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

9
src/Module.js

@ -522,8 +522,8 @@ 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 = declaration.isExternal ?
declaration.getName() : declaration.getName( es6 ) :
declaration.name; declaration.name;
magicString.overwrite( start, start + reference.name.length, 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 ); 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` // remove `export` from `export class Foo {...}` or `export default Foo`
// TODO default exports need different treatment // TODO default exports need different treatment
else if ( statement.node.declaration.id ) { else if ( statement.node.declaration.id ) {

Loading…
Cancel
Save