Browse Source

Implemented 'export *' for internal modules

* exporting * from external modules isn't supported yet, and raises a warning.
gh-109
Oskar Segersvärd 10 years ago
parent
commit
e5801ae400
  1. 3
      src/ExternalModule.js
  2. 36
      src/Module.js

3
src/ExternalModule.js

@ -8,6 +8,9 @@ export default class ExternalModule {
this.name = id; this.name = id;
this.module = this; this.module = this;
// Define the external module's name in the bundle scope.
bundle.scope.define( id, this );
this.isExternal = true; this.isExternal = true;
this.importedByBundle = []; this.importedByBundle = [];

36
src/Module.js

@ -54,6 +54,37 @@ export default class Module {
this.locals = bundle.scope.virtual(); this.locals = bundle.scope.virtual();
this.exports = bundle.scope.virtual(); this.exports = bundle.scope.virtual();
const { reference, inScope } = this.exports;
this.exports.reference = name => {
// If we have it, grab it.
if ( inScope.call( this.exports, name ) ) return reference.call( this.exports, name );
// ... otherwise search exportAlls.
for ( const module of this.exportAlls ) {
if ( module.exports.inScope( name ) ) {
return module.exports.reference( name );
}
}
throw new Error( `The name ${name} is never exported!` );
};
this.exports.inScope = name => {
if ( inScope.call( this.exports, name ) ) return true;
for ( const module of this.exportAlls ) {
if ( module.exports.inScope( name ) ) return true;
}
return false;
};
// Create a unique virtual scope for references to the module.
// const unique = bundle.scope.virtual();
// unique.define( this.name, this );
// this.reference = unique.reference( this.name );
this.exportAlls = []; this.exportAlls = [];
this.reassignments = []; this.reassignments = [];
@ -77,6 +108,11 @@ export default class Module {
if ( node.type === 'ExportAllDeclaration' ) { if ( node.type === 'ExportAllDeclaration' ) {
// Store `export * from '...'` statements in an array of delegates. // Store `export * from '...'` statements in an array of delegates.
// When an unknown import is encountered, we see if one of them can satisfy it. // When an unknown import is encountered, we see if one of them can satisfy it.
if ( module.isExternal ) {
throw new Error( `Cannot trace 'export *' references through external modules.` );
}
this.exportAlls.push( module ); this.exportAlls.push( module );
} }

Loading…
Cancel
Save