Browse Source

support custom resolvePath functions

contingency-plan
Rich-Harris 10 years ago
parent
commit
7dc633ce89
  1. 8
      README.md
  2. 16
      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
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 ) {
// generate code and a sourcemap
const { code, map } = bundle.generate({

16
src/Bundle.js

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

2
src/rollup.js

@ -8,7 +8,7 @@ SOURCEMAPPING_URL += 'ppingURL';
export function rollup ( entry, options = {} ) {
const bundle = new Bundle({
entry,
base: options.base || process.cwd()
resolvePath: options.resolvePath
});
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' );
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 () {
it( 'exists', function () {
assert.ok( !!rollup );
@ -23,11 +33,11 @@ describe( 'rollup', function () {
var config = require( SAMPLES + '/' + dir + '/_config' );
( 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 ) {
var result = bundle.generate({
var result = bundle.generate( extend( {}, config.bundleOptions, {
format: 'cjs'
});
}));
try {
var fn = new Function( 'require', 'exports', 'assert', result.code );

Loading…
Cancel
Save