Browse Source

Merge branch 'resolver-chain' of https://github.com/Victorystick/rollup into chained-resolvers-and-loaders

better-aggressive
Rich-Harris 9 years ago
parent
commit
cc005b426d
  1. 11
      src/Bundle.js
  2. 11
      src/utils/attempt.js
  3. 2
      test/function/custom-path-resolver-on-entry/_config.js
  4. 23
      test/function/custom-path-resolver-plural/_config.js
  5. 2
      test/function/custom-path-resolver-plural/globals-math.js
  6. 4
      test/function/custom-path-resolver-plural/main.js

11
src/Bundle.js

@ -1,5 +1,6 @@
import Promise from 'es6-promise/lib/es6-promise/promise';
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';
@ -16,12 +17,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 = {

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] === '@' ) {
return path.resolve( __dirname, importee ) + '.js';
}
return path.resolve( path.dirname( importer ), importee ) + '.js';
},
load: function ( moduleId ) {
if ( moduleId[0] === '@' ) {

23
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 '<empty>';
}
],
load: function ( id ) {
if ( id === '<empty>' ) return '';
}
},
exports: function ( exports ) {
assert.strictEqual( exports.res, 0 );
}
};

2
test/function/custom-path-resolver-plural/globals-math.js

@ -0,0 +1,2 @@
export var sin = Math.sin;
export var cos = Math.cos;

4
test/function/custom-path-resolver-plural/main.js

@ -0,0 +1,4 @@
import { sin } from '@Math';
import '!path';
export var res = sin( 0 );
Loading…
Cancel
Save