Browse Source

Add incremental support build by passing previous bundle

ghi-672
Bogdan Chadkin 9 years ago
parent
commit
ad44c4ba5b
  1. 18
      src/Bundle.js
  2. 7
      src/rollup.js
  3. 5
      test/incremental/expected.js
  4. 3
      test/incremental/foo.js
  5. 3
      test/incremental/main.js
  6. 32
      test/test.js

18
src/Bundle.js

@ -19,6 +19,13 @@ import { dirname, isRelative, relative, resolve } from './utils/path.js';
export default class Bundle {
constructor ( options ) {
if ( typeof options.bundle === 'object' ) {
this.cachedModules = options.bundle.modules.reduce((modules, module) => {
modules[module.id] = module;
return modules;
}, {});
}
this.plugins = ensureArray( options.plugins );
this.plugins.forEach( plugin => {
@ -173,7 +180,16 @@ export default class Bundle {
throw new Error( `Error loading ${id}: load hook should return a string, a { code, map } object, or nothing/null` );
})
.then( source => transform( source, id, this.transformers ) )
.then( source => {
if (this.cachedModules && this.cachedModules[id]) {
const { code, originalCode } = this.cachedModules[id];
return {
code,
originalCode
};
}
return transform( source, id, this.transformers );
})
.then( source => {
const { code, originalCode, ast, sourceMapChain } = source;

7
src/rollup.js

@ -27,7 +27,8 @@ const ALLOWED_KEYS = [
'plugins',
'sourceMap',
'treeshake',
'useStrict'
'useStrict',
'bundle'
];
export function rollup ( options ) {
@ -51,9 +52,7 @@ export function rollup ( options ) {
return {
imports: bundle.externalModules.map( module => module.id ),
exports: keys( bundle.entryModule.exports ),
modules: bundle.orderedModules.map( module => {
return { id: module.id };
}),
modules: bundle.orderedModules.map( ( { id, code, originalCode } ) => ( { id, code, originalCode } ) ),
generate: options => bundle.render( options ),
write: options => {

5
test/incremental/expected.js

@ -0,0 +1,5 @@
function foo () {
console.log('foo');
};
foo();

3
test/incremental/foo.js

@ -0,0 +1,3 @@
export default function () {
console.log('foo');
};

3
test/incremental/main.js

@ -0,0 +1,3 @@
import foo from './foo.js';
foo();

32
test/test.js

@ -13,6 +13,7 @@ var FUNCTION = path.resolve( __dirname, 'function' );
var FORM = path.resolve( __dirname, 'form' );
var SOURCEMAPS = path.resolve( __dirname, 'sourcemaps' );
var CLI = path.resolve( __dirname, 'cli' );
var INCREMENTAL = path.resolve( __dirname, 'incremental' );
var PROFILES = [
{ format: 'amd' },
@ -423,4 +424,35 @@ describe( 'rollup', function () {
});
});
});
describe.only('incremental', function () {
it('uses previous bundle object to prevent unnecessary transformations', function () {
var calls = 0;
var counter = {
transform: function ( code ) {
calls += 1;
return code;
}
};
return rollup.rollup({
entry: path.join( INCREMENTAL, 'main.js' ),
plugins: [counter]
}).then( function ( bundle ) {
assert.equal( calls, 2 );
return rollup.rollup({
entry: path.join( INCREMENTAL, 'main.js' ),
plugins: [counter],
bundle
});
}).then( function ( bundle ) {
assert.equal( calls, 2 );
var result = bundle.generate({
format: 'es6'
});
return sander.readFile( path.join( INCREMENTAL, 'expected.js' ) ).then( function (expected) {
assert.equal(expected.toString().replace( /(\r\n)/g, '\n'), result.code);
});
});
});
});
});

Loading…
Cancel
Save