Browse Source

support custom resolvePath functions

contingency-plan
Rich-Harris 10 years ago
parent
commit
7dc633ce89
  1. 8
      README.md
  2. 58
      src/Bundle.js
  3. 2
      src/rollup.js
  4. 20
      test/samples/custom-path-resolver-async/_config.js
  5. 1
      test/samples/custom-path-resolver-async/bar.js
  6. 6
      test/samples/custom-path-resolver-async/main.js
  7. 15
      test/samples/custom-path-resolver-sync/_config.js
  8. 1
      test/samples/custom-path-resolver-sync/bar.js
  9. 6
      test/samples/custom-path-resolver-sync/main.js
  10. 16
      test/test.js

8
README.md

@ -92,7 +92,13 @@ The example below is aspirational. It isn't yet implemented - it exists in the n
```js ```js
rollup.rollup( 'app.js', { rollup.rollup( 'app.js', {
/* options */ // Override the default path resolution
resolvePath: function ( importee, importer ) {
// return a string or a falsy value - if falsy,
// import is kept external to the bundle.
// Alternative, return a Promise that fulfils
// with a string or falsy value
}
}).then( function ( bundle ) { }).then( function ( bundle ) {
// generate code and a sourcemap // generate code and a sourcemap
const { code, map } = bundle.generate({ const { code, map } = bundle.generate({

58
src/Bundle.js

@ -1,5 +1,5 @@
import { resolve } from 'path'; import { dirname, resolve } from 'path';
import { readFile } from 'sander'; import { readFile, Promise } from 'sander';
import MagicString from 'magic-string'; import MagicString from 'magic-string';
import { keys, has } from './utils/object'; import { keys, has } from './utils/object';
import { sequence } from './utils/promise'; import { sequence } from './utils/promise';
@ -12,8 +12,9 @@ import { defaultResolver } from './utils/resolvePath';
export default class Bundle { export default class Bundle {
constructor ( options ) { constructor ( options ) {
this.base = options.base || process.cwd(); this.entryPath = resolve( options.entry ).replace( /\.js$/, '' ) + '.js';
this.entryPath = resolve( this.base, options.entry ).replace( /\.js$/, '' ) + '.js'; this.base = dirname( this.entryPath );
this.resolvePath = options.resolvePath || defaultResolver; this.resolvePath = options.resolvePath || defaultResolver;
this.entryModule = null; this.entryModule = null;
@ -23,38 +24,39 @@ export default class Bundle {
} }
fetchModule ( importee, importer ) { fetchModule ( importee, importer ) {
const path = this.resolvePath( importee, importer ); return Promise.resolve( importer === null ? importee : this.resolvePath( importee, importer ) )
.then( path => {
if ( !path ) { if ( !path ) {
// external module // external module
if ( !has( this.modulePromises, importee ) ) { if ( !has( this.modulePromises, importee ) ) {
const module = new ExternalModule( importee ); const module = new ExternalModule( importee );
this.externalModules.push( module ); this.externalModules.push( module );
this.modulePromises[ importee ] = Promise.resolve( module ); this.modulePromises[ importee ] = Promise.resolve( module );
} }
return this.modulePromises[ importee ]; return this.modulePromises[ importee ];
} }
if ( !has( this.modulePromises, path ) ) { if ( !has( this.modulePromises, path ) ) {
this.modulePromises[ path ] = readFile( path, { encoding: 'utf-8' }) this.modulePromises[ path ] = readFile( path, { encoding: 'utf-8' })
.then( code => { .then( code => {
const module = new Module({ const module = new Module({
path, path,
code, code,
bundle: this bundle: this
}); });
return module; return module;
}); });
} }
return this.modulePromises[ path ]; return this.modulePromises[ path ];
});
} }
build () { build () {
// bring in top-level AST nodes from the entry module // bring in top-level AST nodes from the entry module
return this.fetchModule( this.entryPath ) return this.fetchModule( this.entryPath, null )
.then( entryModule => { .then( entryModule => {
this.entryModule = entryModule; this.entryModule = entryModule;

2
src/rollup.js

@ -8,7 +8,7 @@ SOURCEMAPPING_URL += 'ppingURL';
export function rollup ( entry, options = {} ) { export function rollup ( entry, options = {} ) {
const bundle = new Bundle({ const bundle = new Bundle({
entry, entry,
base: options.base || process.cwd() resolvePath: options.resolvePath
}); });
return bundle.build().then( () => { return bundle.build().then( () => {

20
test/samples/custom-path-resolver-async/_config.js

@ -0,0 +1,20 @@
module.exports = {
description: 'uses a custom path resolver (synchronous)',
options: {
resolvePath: function ( importee, importer ) {
var Promise = require( 'sander' ).Promise;
var resolved;
if ( importee === 'foo' ) {
resolved = require( 'path' ).resolve( __dirname, 'bar.js' );
} else {
resolved = false;
}
return Promise.resolve( resolved );
}
},
exports: function ( exports, assert ) {
assert.strictEqual( exports.path, require( 'path' ) );
}
};

1
test/samples/custom-path-resolver-async/bar.js

@ -0,0 +1 @@
export default 'bar';

6
test/samples/custom-path-resolver-async/main.js

@ -0,0 +1,6 @@
import foo from 'foo';
import path from 'path';
assert.equal( foo, 'bar' );
export { path };

15
test/samples/custom-path-resolver-sync/_config.js

@ -0,0 +1,15 @@
module.exports = {
description: 'uses a custom path resolver (synchronous)',
options: {
resolvePath: function ( importee, importer ) {
if ( importee === 'foo' ) {
return require( 'path' ).resolve( __dirname, 'bar.js' );
}
return false;
}
},
exports: function ( exports, assert ) {
assert.strictEqual( exports.path, require( 'path' ) );
}
};

1
test/samples/custom-path-resolver-sync/bar.js

@ -0,0 +1 @@
export default 'bar';

6
test/samples/custom-path-resolver-sync/main.js

@ -0,0 +1,6 @@
import foo from 'foo';
import path from 'path';
assert.equal( foo, 'bar' );
export { path };

16
test/test.js

@ -8,6 +8,16 @@ var rollup = require( '../dist/rollup' );
var SAMPLES = path.resolve( __dirname, 'samples' ); var SAMPLES = path.resolve( __dirname, 'samples' );
function extend ( target ) {
[].slice.call( arguments, 1 ).forEach( function ( source ) {
source && Object.keys( source ).forEach( function ( key ) {
target[ key ] = source[ key ];
});
});
return target;
}
describe( 'rollup', function () { describe( 'rollup', function () {
it( 'exists', function () { it( 'exists', function () {
assert.ok( !!rollup ); assert.ok( !!rollup );
@ -23,11 +33,11 @@ describe( 'rollup', function () {
var config = require( SAMPLES + '/' + dir + '/_config' ); var config = require( SAMPLES + '/' + dir + '/_config' );
( config.solo ? it.only : it )( config.description, function () { ( config.solo ? it.only : it )( config.description, function () {
return rollup.rollup( SAMPLES + '/' + dir + '/main.js' ) return rollup.rollup( SAMPLES + '/' + dir + '/main.js', extend( {}, config.options ) )
.then( function ( bundle ) { .then( function ( bundle ) {
var result = bundle.generate({ var result = bundle.generate( extend( {}, config.bundleOptions, {
format: 'cjs' format: 'cjs'
}); }));
try { try {
var fn = new Function( 'require', 'exports', 'assert', result.code ); var fn = new Function( 'require', 'exports', 'assert', result.code );

Loading…
Cancel
Save