Browse Source

synchronous ongenerate hook (#353)

semi-dynamic-namespace-imports
Rich Harris 9 years ago
parent
commit
b28a019014
  1. 24
      src/rollup.js
  2. 42
      test/test.js

24
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;
});
}

42
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' }
]);
});
});
});
});

Loading…
Cancel
Save