Browse Source

handle export *

contingency-plan
Rich-Harris 9 years ago
parent
commit
6646f156de
  1. 10
      src/Bundle.js
  2. 39
      src/Module.js
  3. 2
      test/function/export-all/_config.js

10
src/Bundle.js

@ -516,14 +516,8 @@ export default class Bundle {
const exportDeclaration = module.exports[ name ]; const exportDeclaration = module.exports[ name ];
if ( exportDeclaration ) return this.trace( module, exportDeclaration.localName ); if ( exportDeclaration ) return this.trace( module, exportDeclaration.localName );
for ( let i = 0; i < module.exportDelegates.length; i += 1 ) { const exportDelegate = module.exportDelegates[ name ];
const delegate = module.exportDelegates[i]; if ( exportDelegate ) return this.traceExport( exportDelegate.module, name, es6 );
const delegateExportDeclaration = delegate.module.exports[ name ];
if ( delegateExportDeclaration ) {
return this.trace( delegate.module, delegateExportDeclaration.localName, es6 );
}
}
throw new Error( `Could not trace binding '${name}' from ${module.id}` ); throw new Error( `Could not trace binding '${name}' from ${module.id}` );
} }

39
src/Module.js

@ -61,11 +61,9 @@ export default class Module {
this.imports = blank(); this.imports = blank();
this.exports = blank(); this.exports = blank();
this.reexports = blank(); this.reexports = blank();
this.exportDelegates = blank();
this.exportAlls = blank(); this.exportAlls = [];
// array of all-export sources
this.exportDelegates = [];
this.replacements = blank(); this.replacements = blank();
@ -90,7 +88,7 @@ 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.
this.exportDelegates.push({ this.exportAlls.push({
statement, statement,
source source
}); });
@ -249,6 +247,11 @@ export default class Module {
}); });
}); });
this.exportAlls.forEach( delegate => {
const id = this.resolvedIds[ delegate.source ];
delegate.module = this.bundle.moduleById[ id ];
});
this.dependencies.forEach( source => { this.dependencies.forEach( source => {
const id = this.resolvedIds[ source ]; const id = this.resolvedIds[ source ];
const module = this.bundle.moduleById[ id ]; const module = this.bundle.moduleById[ id ];
@ -298,7 +301,7 @@ export default class Module {
keys( statement.stronglyDependsOn ).forEach( name => { keys( statement.stronglyDependsOn ).forEach( name => {
if ( statement.defines[ name ] ) return; if ( statement.defines[ name ] ) return;
addDependency( strongDependencies, this.exportAlls[ name ] ) || addDependency( strongDependencies, this.exportDelegates[ name ] ) ||
addDependency( strongDependencies, this.imports[ name ] ); addDependency( strongDependencies, this.imports[ name ] );
}); });
} }
@ -310,7 +313,7 @@ export default class Module {
keys( statement.dependsOn ).forEach( name => { keys( statement.dependsOn ).forEach( name => {
if ( statement.defines[ name ] ) return; if ( statement.defines[ name ] ) return;
addDependency( weakDependencies, this.exportAlls[ name ] ) || addDependency( weakDependencies, this.exportDelegates[ name ] ) ||
addDependency( weakDependencies, this.imports[ name ] ); addDependency( weakDependencies, this.imports[ name ] );
}); });
}); });
@ -458,27 +461,27 @@ export default class Module {
return this.mark( exportDeclaration.localName ); return this.mark( exportDeclaration.localName );
} }
const noExport = new Error( `Module ${this.id} does not export ${name} (imported by ${importer.id})` );
// See if there exists an export delegate that defines `name`. // See if there exists an export delegate that defines `name`.
for ( i = 0; i < this.exportDelegates.length; i += 1 ) { let i;
const declaration = this.exportDelegates[i]; for ( i = 0; i < this.exportAlls.length; i += 1 ) {
const declaration = this.exportAlls[i];
const result = declaration.module.mark( name );
if ( !result.length ) throw noExport;
if ( declaration.module.exports[ name ] ) {
// It's found! This module exports `name` through declaration. // It's found! This module exports `name` through declaration.
// It is however not imported into this scope. // It is however not imported into this scope.
this.exportAlls[ name ] = declaration; this.exportDelegates[ name ] = declaration;
declaration.module.markExport( name );
declaration.statement.dependsOn[ name ] = declaration.statement.dependsOn[ name ] =
declaration.statement.stronglyDependsOn[ name ] = result; declaration.statement.stronglyDependsOn[ name ] = true;
return result; return;
} }
} }
throw new Error( `Module ${this.id} does not export ${name} (imported by ${importer.id})` );
}
parse ( ast ) { parse ( ast ) {
// The ast can be supplied programmatically (but usually won't be) // The ast can be supplied programmatically (but usually won't be)
if ( !ast ) { if ( !ast ) {

2
test/function/export-all/_config.js

@ -1,5 +1,3 @@
var assert = require( 'assert' );
module.exports = { module.exports = {
description: 'allows export *' description: 'allows export *'
}; };

Loading…
Cancel
Save