From 979f61baf15eff01ce0faec50cd05d71c6e3d89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Wed, 26 Aug 2015 16:55:55 +0200 Subject: [PATCH] Use ExternalModules as their own default value. --- src/ExternalModule.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/ExternalModule.js b/src/ExternalModule.js index 2af7b9b..eb35dfb 100644 --- a/src/ExternalModule.js +++ b/src/ExternalModule.js @@ -2,7 +2,11 @@ export default class ExternalModule { constructor ( { id, bundle } ) { this.id = id; + + // Implement `Identifier` interface. + this.originalName = id; this.name = id; + this.module = this; this.isExternal = true; this.importedByBundle = []; @@ -17,32 +21,39 @@ export default class ExternalModule { this.needsNamed = false; this.needsAll = false; - bundle.scope.define( this.name, this ); + bundle.scope.define( this.originalName, this ); this.exports = bundle.scope.virtual(); - const ref = this.exports.reference; + const { lookup, reference } = this.exports; // Override reference. this.exports.reference = name => { - if ( !this.exports.defines( name ) ) { - let idName = name; + if ( name === 'default' ) { + this.needsDefault = true; + return bundle.scope.reference( this.originalName ); + } - if ( name === 'default' ) { - idName = this.name; - this.needsDefault = true; - } else { - this.needsNamed = true; - } + if ( !this.exports.defines( name ) ) { + this.needsNamed = true; this.exports.define( name, { - originalName: idName, - name: idName, + originalName: name, + name, module: this }); } - return ref.call( this.exports, name ); + return reference.call( this.exports, name ); + }; + + // Override lookup. + this.exports.lookup = name => { + if ( name === 'default' ) { + return this; + } + + return lookup.call( this.exports, name ); }; } }