From e5437ed2f5756fd7adf238e6a5b8f80fec86487e Mon Sep 17 00:00:00 2001 From: Mickael Jeanroy Date: Sun, 22 May 2016 18:26:38 +0200 Subject: [PATCH] ensure that external alias are not treated as relative path --- src/Bundle.js | 9 +++++-- test/function/external-alias/_config.js | 28 ++++++++++++++++++++ test/function/external-alias/first/main.js | 10 +++++++ test/function/external-alias/first/module.js | 7 +++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/function/external-alias/_config.js create mode 100644 test/function/external-alias/first/main.js create mode 100644 test/function/external-alias/first/module.js diff --git a/src/Bundle.js b/src/Bundle.js index a050de2..0b7902f 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -15,7 +15,7 @@ import transformBundle from './utils/transformBundle.js'; import collapseSourcemaps from './utils/collapseSourcemaps.js'; import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; import callIfFunction from './utils/callIfFunction.js'; -import { dirname, isRelative, relative, resolve } from './utils/path.js'; +import { dirname, isRelative, isAbsolute, relative, resolve } from './utils/path.js'; export default class Bundle { constructor ( options ) { @@ -207,7 +207,12 @@ export default class Bundle { if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` ); if ( !~this.external.indexOf( source ) ) this.onwarn( `Treating '${source}' as external dependency` ); } else if ( resolvedId ) { - normalizedExternal = this.getPathRelativeToEntryDirname( 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; diff --git a/test/function/external-alias/_config.js b/test/function/external-alias/_config.js new file mode 100644 index 0000000..3232f3b --- /dev/null +++ b/test/function/external-alias/_config.js @@ -0,0 +1,28 @@ +var assert = require( 'assert' ); +var path = require( 'path' ); + +module.exports = { + description: 'includes an external module included dynamically by an alias', + options: { + entry: path.join( __dirname, 'first', 'main.js' ), + external: [ 'lodash' ], + + // Define a simple alias plugin for underscore + plugins: [ + { + resolveId: function ( id ) { + if ( id === 'underscore' ) { + return 'lodash'; + } + } + } + ] + }, + + context: { + require: function ( required ) { + assert.equal( required, 'lodash' ); + return 1; + } + } +}; diff --git a/test/function/external-alias/first/main.js b/test/function/external-alias/first/main.js new file mode 100644 index 0000000..3503622 --- /dev/null +++ b/test/function/external-alias/first/main.js @@ -0,0 +1,10 @@ +import _ from 'underscore'; +import first from './module'; + +export default function ( inputs ) { + if ( !_.isArray( inputs ) ) { + return inputs; + } + + return first.square( inputs ); +}; diff --git a/test/function/external-alias/first/module.js b/test/function/external-alias/first/module.js new file mode 100644 index 0000000..4bacbea --- /dev/null +++ b/test/function/external-alias/first/module.js @@ -0,0 +1,7 @@ +import _ from 'underscore'; + +export default function square ( inputs ) { + return _.map( inputs, function ( x ) { + return x * x; + }); +};