From 001901a6956a7d225ee37d6b7d3287c0e60c530b Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Mon, 6 Jun 2016 23:51:34 -0400 Subject: [PATCH] undo non-string ID change (#692) --- src/Bundle.js | 76 +++++++++---------- src/Module.js | 10 +-- .../custom-resolver-non-string/_config.js | 15 ---- .../custom-resolver-non-string/main.js | 2 - 4 files changed, 38 insertions(+), 65 deletions(-) delete mode 100644 test/function/custom-resolver-non-string/_config.js delete mode 100644 test/function/custom-resolver-non-string/main.js diff --git a/src/Bundle.js b/src/Bundle.js index 95248b1..0253c33 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -198,53 +198,47 @@ export default class Bundle { return mapSequence( module.sources, source => { return this.resolveId( source, module.id ) .then( resolvedId => { - module.resolvedIds[ source ] = resolvedId; - - if ( !resolvedId || typeof resolvedId === 'string' ) { - let externalName; - if ( resolvedId ) { - // If the `resolvedId` is supposed to be external, make it so. - externalName = resolvedId.replace( /[\/\\]/g, '/' ); - } else if ( isRelative( source ) ) { - // This could be an external, relative dependency, based on the current module's parent dir. - externalName = resolve( module.id, '..', source ); - } - - const forcedExternal = externalName && this.isExternal( externalName ); - - if ( !resolvedId || forcedExternal ) { - let normalizedExternal = source; - - if ( !forcedExternal ) { - if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` ); - if ( !this.isExternal( source ) ) this.onwarn( `Treating '${source}' as external dependency` ); - } else if ( resolvedId ) { - if ( isRelative(resolvedId) || isAbsolute(resolvedId) ) { - // Try to deduce relative path from entry dir if resolvedId is defined as a relative path. - normalizedExternal = this.getPathRelativeToEntryDirname( resolvedId ); - } else { - normalizedExternal = resolvedId; - } - } - - // overwrite existing - module.resolvedIds[ source ] = normalizedExternal; - - if ( !this.moduleById.has( normalizedExternal ) ) { - const module = new ExternalModule( normalizedExternal ); - this.externalModules.push( module ); - this.moduleById.set( normalizedExternal, module ); + let externalName; + if ( resolvedId ) { + // If the `resolvedId` is supposed to be external, make it so. + externalName = resolvedId.replace( /[\/\\]/g, '/' ); + } else if ( isRelative( source ) ) { + // This could be an external, relative dependency, based on the current module's parent dir. + externalName = resolve( module.id, '..', source ); + } + const forcedExternal = externalName && this.isExternal( externalName ); + + if ( !resolvedId || forcedExternal ) { + let normalizedExternal = source; + + if ( !forcedExternal ) { + if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` ); + if ( !this.isExternal( source ) ) this.onwarn( `Treating '${source}' as external dependency` ); + } else if ( resolvedId ) { + if ( isRelative(resolvedId) || isAbsolute(resolvedId) ) { + // Try to deduce relative path from entry dir if resolvedId is defined as a relative path. + normalizedExternal = this.getPathRelativeToEntryDirname( resolvedId ); + } else { + normalizedExternal = resolvedId; } + } + module.resolvedIds[ source ] = normalizedExternal; - return null; + if ( !this.moduleById.has( normalizedExternal ) ) { + const module = new ExternalModule( normalizedExternal ); + this.externalModules.push( module ); + this.moduleById.set( normalizedExternal, module ); } } - if ( resolvedId === module.id ) { - throw new Error( `A module cannot import itself (${resolvedId})` ); - } + else { + if ( resolvedId === module.id ) { + throw new Error( `A module cannot import itself (${resolvedId})` ); + } - return this.fetchModule( resolvedId, module.id ); + module.resolvedIds[ source ] = resolvedId; + return this.fetchModule( resolvedId, module.id ); + } }); }); } diff --git a/src/Module.js b/src/Module.js index e222b3a..9f6ae45 100644 --- a/src/Module.js +++ b/src/Module.js @@ -191,14 +191,10 @@ export default class Module { } basename () { - if ( typeof this.id === 'string' ) { - const base = basename( this.id ); - const ext = extname( this.id ); + const base = basename( this.id ); + const ext = extname( this.id ); - return makeLegalIdentifier( ext ? base.slice( 0, -ext.length ) : base ); - } - - return 'module'; + return makeLegalIdentifier( ext ? base.slice( 0, -ext.length ) : base ); } bindAliases () { diff --git a/test/function/custom-resolver-non-string/_config.js b/test/function/custom-resolver-non-string/_config.js deleted file mode 100644 index 5cdcdac..0000000 --- a/test/function/custom-resolver-non-string/_config.js +++ /dev/null @@ -1,15 +0,0 @@ -const FOO = {}; - -module.exports = { - description: 'allows resolveId to return a non-string', - options: { - plugins: [{ - resolveId: importee => { - if ( importee === 'foo' ) return FOO; - }, - load: id => { - if ( id === FOO ) return 'export default 42'; - } - }] - } -}; diff --git a/test/function/custom-resolver-non-string/main.js b/test/function/custom-resolver-non-string/main.js deleted file mode 100644 index 2e13986..0000000 --- a/test/function/custom-resolver-non-string/main.js +++ /dev/null @@ -1,2 +0,0 @@ -import foo from 'foo'; -assert.equal( foo, 42 );