Browse Source

use resolvers/loaders in order, dont catch errors

better-aggressive
Rich-Harris 9 years ago
parent
commit
9f2d1b8ada
  1. 12
      src/Bundle.js
  2. 11
      src/utils/attempt.js
  3. 11
      src/utils/first.js

12
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 = {

11
src/utils/attempt.js

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

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