Browse Source

Made the return value of resolver functions optional. Uses a fallback on failure.

better-aggressive
Oskar Segersvärd 10 years ago
parent
commit
e8d299192e
  1. 11
      src/Bundle.js
  2. 11
      src/utils/attempt.js
  3. 2
      test/function/custom-path-resolver-on-entry/_config.js

11
src/Bundle.js

@ -1,5 +1,6 @@
import { Promise } from 'sander'; import { Promise } from 'sander';
import MagicString from 'magic-string'; import MagicString from 'magic-string';
import attempt from './utils/attempt.js';
import { blank, keys } from './utils/object'; import { blank, keys } from './utils/object';
import Module from './Module'; import Module from './Module';
import ExternalModule from './ExternalModule'; import ExternalModule from './ExternalModule';
@ -17,12 +18,16 @@ export default class Bundle {
this.entry = options.entry; this.entry = options.entry;
this.entryModule = null; this.entryModule = null;
this.resolveId = options.resolveId || defaultResolver; this.resolveId = ensureArray( options.resolveId )
this.load = options.load || defaultLoader; .reduce( attempt, defaultResolver );
this.load = ensureArray( options.load )
.reduce( attempt, defaultLoader );
this.resolveOptions = { this.resolveOptions = {
external: ensureArray( options.external ), external: ensureArray( options.external ),
resolveExternal: options.resolveExternal || defaultExternalResolver resolveExternal: ensureArray( options.resolveExternal )
.reduce( attempt, defaultExternalResolver )
}; };
this.loadOptions = { this.loadOptions = {

11
src/utils/attempt.js

@ -0,0 +1,11 @@
// Given a `fallback` and a `preferred` function,
// return a function that attempts to call the `preferred`.
// If it fails, or returns undefined, call the fallback and return its value.
export default function attempt ( fallback, preferred ) {
return function ( ...args ) {
const boundFallback = () => fallback( ...args );
return Promise.resolve( preferred( ...args ) )
.then( res => res === undefined ? boundFallback() : res, boundFallback );
};
}

2
test/function/custom-path-resolver-on-entry/_config.js

@ -18,8 +18,6 @@ module.exports = {
if ( importer[0] === '@' ) { if ( importer[0] === '@' ) {
return path.resolve( __dirname, importee ) + '.js'; return path.resolve( __dirname, importee ) + '.js';
} }
return path.resolve( path.dirname( importer ), importee ) + '.js';
}, },
load: function ( moduleId ) { load: function ( moduleId ) {
if ( moduleId[0] === '@' ) { if ( moduleId[0] === '@' ) {

Loading…
Cancel
Save