Browse Source

more fixes

declarations-and-references
Rich-Harris 9 years ago
parent
commit
20638dbe48
  1. 5
      src/Bundle.js
  2. 27
      src/ExternalModule.js
  3. 15
      src/Module.js
  4. 7
      src/ast/Scope.js
  5. 2
      src/finalisers/es6.js

5
src/Bundle.js

@ -40,7 +40,7 @@ export default class Bundle {
this.internalNamespaceModules = [];
this.assumedGlobals = blank();
// TODO strictly speaking, this only applies with non-ES6, non-default-only bundles
[ 'module', 'exports' ].forEach( global => this.assumedGlobals[ global ] = true );
}
@ -180,7 +180,6 @@ export default class Bundle {
render ( options = {} ) {
const format = options.format || 'es6';
const allReplacements = this.deconflict( format === 'es6' );
// Determine export mode - 'default', 'named', 'none'
const exportMode = getExportMode( this, options.exports );
@ -188,7 +187,7 @@ export default class Bundle {
let magicString = new MagicString.Bundle({ separator: '\n\n' });
this.orderedModules.forEach( module => {
const source = module.render();
const source = module.render( format === 'es6' );
if ( source.toString().length ) {
magicString.addSource( source );
}

27
src/ExternalModule.js

@ -1,12 +1,23 @@
import { blank } from './utils/object';
import makeLegalIdentifier from './utils/makeLegalIdentifier';
class ExternalDeclaration {
constructor ( module, name ) {
this.module = module;
this.name = name;
this.isExternal = true;
this.references = [];
}
}
export default class ExternalModule {
constructor ( id ) {
this.id = id;
this.name = null;
this.name = makeLegalIdentifier( id );
this.isExternal = true;
this.importedByBundle = [];
this.declarations = blank();
this.suggestedNames = blank();
@ -34,4 +45,16 @@ export default class ExternalModule {
this.suggestedNames[ exportName ] = suggestion;
}
}
traceExport ( name ) {
if ( name === 'default' ) {
this.needsDefault = true;
} else {
this.needsNamed = true;
}
return this.declarations[ name ] || (
this.declarations[ name ] = new ExternalDeclaration( this, name )
);
}
}

15
src/Module.js

@ -508,7 +508,7 @@ export default class Module {
return statements;
}
render () {
render ( es6 ) {
let magicString = this.magicString.clone();
this.statements.forEach( statement => {
@ -516,6 +516,19 @@ export default class Module {
magicString.remove( statement.start, statement.next );
}
statement.references.forEach( reference => {
const declaration = reference.declaration;
if ( reference.declaration ) {
const { start, end } = reference.node;
const name = ( !es6 && declaration.isExternal ) ?
`${declaration.module.name}.${declaration.name}` :
declaration.name;
magicString.overwrite( start, end, name );
}
});
// modify exports as necessary
if ( statement.isExportDeclaration ) {
// remove `export` from `export class Foo {...}` or `export default Foo`

7
src/ast/Scope.js

@ -34,9 +34,10 @@ function extractNames ( param ) {
}
class Declaration {
constructor () {
constructor ( name ) {
this.references = [];
this.statement = null;
this.name = name;
}
}
@ -52,7 +53,7 @@ export default class Scope {
if ( options.params ) {
options.params.forEach( param => {
extractNames( param ).forEach( name => {
this.declarations[ name ] = new Declaration();
this.declarations[ name ] = new Declaration( name );
});
});
}
@ -65,7 +66,7 @@ export default class Scope {
this.parent.addDeclaration( node, isBlockDeclaration, isVar );
} else {
extractNames( node.id ).forEach( name => {
this.declarations[ name ] = new Declaration();
this.declarations[ name ] = new Declaration( name );
});
}
}

2
src/finalisers/es6.js

@ -30,7 +30,7 @@ export default function es6 ( bundle, magicString ) {
}
if ( module.needsNamed ) {
specifiers.push( '{ ' + uniqueNames( module.importedByBundle )
specifiers.push( '{ ' + keys( module.declarations )
.join( ', ' ) + ' }' );
}

Loading…
Cancel
Save