Browse Source

undo non-string ID change (#692)

ghi-672
Rich-Harris 9 years ago
parent
commit
001901a695
  1. 76
      src/Bundle.js
  2. 10
      src/Module.js
  3. 15
      test/function/custom-resolver-non-string/_config.js
  4. 2
      test/function/custom-resolver-non-string/main.js

76
src/Bundle.js

@ -198,53 +198,47 @@ export default class Bundle {
return mapSequence( module.sources, source => { return mapSequence( module.sources, source => {
return this.resolveId( source, module.id ) return this.resolveId( source, module.id )
.then( resolvedId => { .then( resolvedId => {
module.resolvedIds[ source ] = resolvedId; let externalName;
if ( resolvedId ) {
if ( !resolvedId || typeof resolvedId === 'string' ) { // If the `resolvedId` is supposed to be external, make it so.
let externalName; externalName = resolvedId.replace( /[\/\\]/g, '/' );
if ( resolvedId ) { } else if ( isRelative( source ) ) {
// If the `resolvedId` is supposed to be external, make it so. // This could be an external, relative dependency, based on the current module's parent dir.
externalName = resolvedId.replace( /[\/\\]/g, '/' ); externalName = resolve( module.id, '..', source );
} else if ( isRelative( source ) ) { }
// This could be an external, relative dependency, based on the current module's parent dir. const forcedExternal = externalName && this.isExternal( externalName );
externalName = resolve( module.id, '..', source );
} if ( !resolvedId || forcedExternal ) {
let normalizedExternal = source;
const forcedExternal = externalName && this.isExternal( externalName );
if ( !forcedExternal ) {
if ( !resolvedId || forcedExternal ) { if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` );
let normalizedExternal = source; if ( !this.isExternal( source ) ) this.onwarn( `Treating '${source}' as external dependency` );
} else if ( resolvedId ) {
if ( !forcedExternal ) { if ( isRelative(resolvedId) || isAbsolute(resolvedId) ) {
if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` ); // Try to deduce relative path from entry dir if resolvedId is defined as a relative path.
if ( !this.isExternal( source ) ) this.onwarn( `Treating '${source}' as external dependency` ); normalizedExternal = this.getPathRelativeToEntryDirname( resolvedId );
} else if ( resolvedId ) { } else {
if ( isRelative(resolvedId) || isAbsolute(resolvedId) ) { normalizedExternal = 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 );
} }
}
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 ) { else {
throw new Error( `A module cannot import itself (${resolvedId})` ); 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 );
}
}); });
}); });
} }

10
src/Module.js

@ -191,14 +191,10 @@ export default class Module {
} }
basename () { basename () {
if ( typeof this.id === 'string' ) { const base = basename( this.id );
const base = basename( this.id ); const ext = extname( this.id );
const ext = extname( this.id );
return makeLegalIdentifier( ext ? base.slice( 0, -ext.length ) : base ); return makeLegalIdentifier( ext ? base.slice( 0, -ext.length ) : base );
}
return 'module';
} }
bindAliases () { bindAliases () {

15
test/function/custom-resolver-non-string/_config.js

@ -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';
}
}]
}
};

2
test/function/custom-resolver-non-string/main.js

@ -1,2 +0,0 @@
import foo from 'foo';
assert.equal( foo, 42 );
Loading…
Cancel
Save