Browse Source

ensure that external alias are not treated as relative path

gh-669
Mickael Jeanroy 9 years ago
parent
commit
e5437ed2f5
  1. 9
      src/Bundle.js
  2. 28
      test/function/external-alias/_config.js
  3. 10
      test/function/external-alias/first/main.js
  4. 7
      test/function/external-alias/first/module.js

9
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;

28
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;
}
}
};

10
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 );
};

7
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;
});
};
Loading…
Cancel
Save