From 9f2d1b8adae8ddd60559f15e736ce735c1afef28 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Thu, 8 Oct 2015 22:51:50 -0400 Subject: [PATCH] use resolvers/loaders in order, dont catch errors --- src/Bundle.js | 12 ++++-------- src/utils/attempt.js | 11 ----------- src/utils/first.js | 11 +++++++++++ 3 files changed, 15 insertions(+), 19 deletions(-) delete mode 100644 src/utils/attempt.js create mode 100644 src/utils/first.js diff --git a/src/Bundle.js b/src/Bundle.js index 4c94419..9bff6d0 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -1,6 +1,6 @@ import Promise from 'es6-promise/lib/es6-promise/promise'; import MagicString from 'magic-string'; -import attempt from './utils/attempt.js'; +import first from './utils/first.js'; import { blank, keys } from './utils/object'; import Module from './Module'; import ExternalModule from './ExternalModule'; @@ -17,16 +17,12 @@ export default class Bundle { this.entry = options.entry; this.entryModule = null; - this.resolveId = ensureArray( options.resolveId ) - .reduce( attempt, defaultResolver ); - - this.load = ensureArray( options.load ) - .reduce( attempt, defaultLoader ); + this.resolveId = first( ensureArray( options.resolveId ).concat( defaultResolver ) ); + this.load = first( ensureArray( options.load ).concat( defaultLoader ) ); this.resolveOptions = { external: ensureArray( options.external ), - resolveExternal: ensureArray( options.resolveExternal ) - .reduce( attempt, defaultExternalResolver ) + resolveExternal: first( ensureArray( options.resolveExternal ).concat( defaultExternalResolver ) ) }; this.loadOptions = { diff --git a/src/utils/attempt.js b/src/utils/attempt.js deleted file mode 100644 index 29eacf0..0000000 --- a/src/utils/attempt.js +++ /dev/null @@ -1,11 +0,0 @@ -// 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 ); - }; -} diff --git a/src/utils/first.js b/src/utils/first.js new file mode 100644 index 0000000..43a12f4 --- /dev/null +++ b/src/utils/first.js @@ -0,0 +1,11 @@ +// Return the first non-falsy result from an array of +// maybe-sync, maybe-promise-returning functions +export default function first ( candidates ) { + return function ( ...args ) { + return candidates.reduce( ( promise, candidate ) => { + return promise.then( result => result != null ? + result : + Promise.resolve( candidate( ...args ) ) ); + }, Promise.resolve() ); + } +}