diff --git a/src/rollup.js b/src/rollup.js index a9c50fd..9a7df6f 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -1,6 +1,6 @@ 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 validateKeys from './utils/validateKeys.js'; import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; import Bundle from './Bundle.js'; @@ -52,19 +52,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 = []; @@ -85,5 +99,7 @@ export function rollup ( options ) { return Promise.all( promises ); } }; + + return result; }); } diff --git a/test/test.js b/test/test.js index 30ec5f2..b494e5d 100644 --- a/test/test.js +++ b/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,34 @@ 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' } + ]); + }); + }); + }); });