From e8d299192e44d87710230159a4d2d4fa63f3c947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Sat, 12 Sep 2015 16:46:57 +0200 Subject: [PATCH 1/2] Made the return value of resolver functions optional. Uses a fallback on failure. --- src/Bundle.js | 11 ++++++++--- src/utils/attempt.js | 11 +++++++++++ .../function/custom-path-resolver-on-entry/_config.js | 2 -- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 src/utils/attempt.js diff --git a/src/Bundle.js b/src/Bundle.js index 6f0857b..53e1652 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -1,5 +1,6 @@ import { Promise } from 'sander'; import MagicString from 'magic-string'; +import attempt from './utils/attempt.js'; import { blank, keys } from './utils/object'; import Module from './Module'; import ExternalModule from './ExternalModule'; @@ -17,12 +18,16 @@ export default class Bundle { this.entry = options.entry; this.entryModule = null; - this.resolveId = options.resolveId || defaultResolver; - this.load = options.load || defaultLoader; + this.resolveId = ensureArray( options.resolveId ) + .reduce( attempt, defaultResolver ); + + this.load = ensureArray( options.load ) + .reduce( attempt, defaultLoader ); this.resolveOptions = { external: ensureArray( options.external ), - resolveExternal: options.resolveExternal || defaultExternalResolver + resolveExternal: ensureArray( options.resolveExternal ) + .reduce( attempt, defaultExternalResolver ) }; this.loadOptions = { diff --git a/src/utils/attempt.js b/src/utils/attempt.js new file mode 100644 index 0000000..29eacf0 --- /dev/null +++ b/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 ); + }; +} diff --git a/test/function/custom-path-resolver-on-entry/_config.js b/test/function/custom-path-resolver-on-entry/_config.js index 83a6a67..5a983b2 100644 --- a/test/function/custom-path-resolver-on-entry/_config.js +++ b/test/function/custom-path-resolver-on-entry/_config.js @@ -18,8 +18,6 @@ module.exports = { if ( importer[0] === '@' ) { return path.resolve( __dirname, importee ) + '.js'; } - - return path.resolve( path.dirname( importer ), importee ) + '.js'; }, load: function ( moduleId ) { if ( moduleId[0] === '@' ) { From 162fba9c28773369586da5a9790b69bae1a3d383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Sat, 12 Sep 2015 16:58:57 +0200 Subject: [PATCH 2/2] Test for plural resolveId functions. --- .../custom-path-resolver-plural/_config.js | 23 +++++++++++++++++++ .../globals-math.js | 2 ++ .../custom-path-resolver-plural/main.js | 4 ++++ 3 files changed, 29 insertions(+) create mode 100644 test/function/custom-path-resolver-plural/_config.js create mode 100644 test/function/custom-path-resolver-plural/globals-math.js create mode 100644 test/function/custom-path-resolver-plural/main.js diff --git a/test/function/custom-path-resolver-plural/_config.js b/test/function/custom-path-resolver-plural/_config.js new file mode 100644 index 0000000..dd63a44 --- /dev/null +++ b/test/function/custom-path-resolver-plural/_config.js @@ -0,0 +1,23 @@ +var path = require( 'path' ); +var assert = require( 'assert' ); + +module.exports = { + description: 'uses custom path resolvers (plural)', + options: { + resolveId: [ + function ( importee ) { + if ( importee[0] === '@' ) + return path.resolve( __dirname, 'globals-' + importee.slice( 1 ).toLowerCase() + '.js' ); + }, + function ( importee ) { + if ( importee[0] === '!' ) return ''; + } + ], + load: function ( id ) { + if ( id === '' ) return ''; + } + }, + exports: function ( exports ) { + assert.strictEqual( exports.res, 0 ); + } +}; diff --git a/test/function/custom-path-resolver-plural/globals-math.js b/test/function/custom-path-resolver-plural/globals-math.js new file mode 100644 index 0000000..daa4e39 --- /dev/null +++ b/test/function/custom-path-resolver-plural/globals-math.js @@ -0,0 +1,2 @@ +export var sin = Math.sin; +export var cos = Math.cos; diff --git a/test/function/custom-path-resolver-plural/main.js b/test/function/custom-path-resolver-plural/main.js new file mode 100644 index 0000000..70511f5 --- /dev/null +++ b/test/function/custom-path-resolver-plural/main.js @@ -0,0 +1,4 @@ +import { sin } from '@Math'; +import '!path'; + +export var res = sin( 0 );