Browse Source

allow options.external to be a function. closes #522

ghi-672
Rich-Harris 9 years ago
parent
commit
40217fd2e1
  1. 14
      src/Bundle.js
  2. 14
      test/function/external-function/_config.js
  3. 2
      test/function/external-function/main.js

14
src/Bundle.js

@ -34,7 +34,7 @@ export default class Bundle {
this.treeshake = options.treeshake !== false;
this.resolveId = first(
[ id => ~this.external.indexOf( id ) ? false : null ]
[ id => this.isExternal( id ) ? false : null ]
.concat( this.plugins.map( plugin => plugin.resolveId ).filter( Boolean ) )
.concat( resolveId )
);
@ -62,7 +62,13 @@ export default class Bundle {
this.assumedGlobals = blank();
this.external = ensureArray( options.external ).map( id => id.replace( /[\/\\]/g, '/' ) );
if ( typeof options.external === 'function' ) {
this.isExternal = options.external;
} else {
const ids = ensureArray( options.external ).map( id => id.replace( /[\/\\]/g, '/' ) );
this.isExternal = id => ids.indexOf( id ) !== -1;
}
this.onwarn = options.onwarn || makeOnwarn();
// TODO strictly speaking, this only applies with non-ES6, non-default-only bundles
@ -198,14 +204,14 @@ export default class Bundle {
// This could be an external, relative dependency, based on the current module's parent dir.
externalName = resolve( module.id, '..', source );
}
const forcedExternal = externalName && ~this.external.indexOf( externalName );
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.external.indexOf( source ) ) this.onwarn( `Treating '${source}' as external dependency` );
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.

14
test/function/external-function/_config.js

@ -0,0 +1,14 @@
module.exports = {
description: 'allows external option to be a function (#522)',
options: {
external: id => {
return id === 'external';
}
},
context: {
require: id => {
if ( id === 'external' ) return 42;
return require( id );
}
}
};

2
test/function/external-function/main.js

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