Browse Source

Merge pull request #742 from rollup/hooks

Hooks
semi-dynamic-namespace-imports
Rich Harris 9 years ago
committed by GitHub
parent
commit
89329f4452
  1. 33
      src/rollup.js
  2. 81
      test/test.js

33
src/rollup.js

@ -1,6 +1,7 @@
import { basename } from './utils/path.js';
import { writeFile } from './utils/fs.js';
import { keys } from './utils/object.js';
import { assign, keys } from './utils/object.js';
import { mapSequence } from './utils/promise.js';
import validateKeys from './utils/validateKeys.js';
import SOURCEMAPPING_URL from './utils/sourceMappingURL.js';
import Bundle from './Bundle.js';
@ -52,19 +53,33 @@ export function rollup ( options ) {
const bundle = new Bundle( options );
return bundle.build().then( () => {
return {
function generate ( options ) {
const rendered = bundle.render( options );
bundle.plugins.forEach( plugin => {
if ( plugin.ongenerate ) {
plugin.ongenerate( assign({
bundle: result
}, options ));
}
});
return rendered;
}
var result = {
imports: bundle.externalModules.map( module => module.id ),
exports: keys( bundle.entryModule.exports ),
modules: bundle.orderedModules.map( module => module.toJSON() ),
generate: options => bundle.render( options ),
generate,
write: options => {
if ( !options || !options.dest ) {
throw new Error( 'You must supply options.dest to bundle.write' );
}
const dest = options.dest;
let { code, map } = bundle.render( options );
let { code, map } = generate( options );
let promises = [];
@ -82,8 +97,16 @@ export function rollup ( options ) {
}
promises.push( writeFile( dest, code ) );
return Promise.all( promises );
return Promise.all( promises ).then( () => {
return mapSequence( bundle.plugins.filter( plugin => plugin.onwrite ), plugin => {
return Promise.resolve( plugin.onwrite( assign({
bundle: result
}, options )));
});
});
}
};
return result;
});
}

81
test/test.js

@ -44,6 +44,18 @@ function loadConfig ( path ) {
}
}
function loader ( modules ) {
return {
resolveId ( id ) {
return id;
},
load ( id ) {
return modules[ id ];
}
};
}
describe( 'rollup', function () {
this.timeout( 10000 );
@ -533,4 +545,73 @@ describe( 'rollup', function () {
});
});
});
describe( 'hooks', () => {
it( 'calls ongenerate hooks in sequence', () => {
var result = [];
return rollup.rollup({
entry: 'entry',
plugins: [
loader({ entry: `alert('hello')` }),
{
ongenerate ( info ) {
result.push({ a: info.format });
}
},
{
ongenerate ( info ) {
result.push({ b: info.format });
}
}
]
}).then( bundle => {
bundle.generate({ format: 'cjs' });
assert.deepEqual( result, [
{ a: 'cjs' },
{ b: 'cjs' }
]);
});
});
it( 'calls onwrite hooks in sequence', () => {
var result = [];
var dest = path.join( __dirname, 'tmp/bundle.js' );
return rollup.rollup({
entry: 'entry',
plugins: [
loader({ entry: `alert('hello')` }),
{
onwrite ( info ) {
return new Promise( ( fulfil ) => {
result.push({ a: info.dest, format: info.format });
fulfil();
});
}
},
{
onwrite ( info ) {
result.push({ b: info.dest, format: info.format });
}
}
]
}).then( bundle => {
return bundle.write({
dest,
format: 'cjs'
});
}).then( () => {
assert.deepEqual( result, [
{ a: dest, format: 'cjs' },
{ b: dest, format: 'cjs' }
]);
return sander.unlink( dest );
});
});
});
});

Loading…
Cancel
Save