Browse Source

Invalidate module when file content is changed

ghi-672
Bogdan Chadkin 9 years ago
parent
commit
157b7c151f
  1. 8
      src/Bundle.js
  2. 7
      src/utils/transform.js
  3. 0
      test/incremental/not-transform-twice/expected.js
  4. 0
      test/incremental/not-transform-twice/foo.js
  5. 0
      test/incremental/not-transform-twice/main.js
  6. 5
      test/incremental/transform-changed/expected.js
  7. 3
      test/incremental/transform-changed/foo-changed.js
  8. 3
      test/incremental/transform-changed/foo.js
  9. 3
      test/incremental/transform-changed/main.js
  10. 54
      test/test.js

8
src/Bundle.js

@ -181,7 +181,13 @@ 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 => {
if (this.cachedModules && this.cachedModules[id]) {
if ( typeof source === 'string' ) {
source = {
code: source,
ast: null
};
}
if (this.cachedModules && this.cachedModules[id] && this.cachedModules[id].originalCode === source.code) {
const { code, originalCode } = this.cachedModules[id];
return {
code,

7
src/utils/transform.js

@ -3,13 +3,6 @@ import Promise from 'es6-promise/lib/es6-promise/promise.js';
export default function transform ( source, id, transformers ) {
let sourceMapChain = [];
if ( typeof source === 'string' ) {
source = {
code: source,
ast: null
};
}
let originalCode = source.code;
let ast = source.ast;

0
test/incremental/expected.js → test/incremental/not-transform-twice/expected.js

0
test/incremental/foo.js → test/incremental/not-transform-twice/foo.js

0
test/incremental/main.js → test/incremental/not-transform-twice/main.js

5
test/incremental/transform-changed/expected.js

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

3
test/incremental/transform-changed/foo-changed.js

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

3
test/incremental/transform-changed/foo.js

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

3
test/incremental/transform-changed/main.js

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

54
test/test.js

@ -426,7 +426,7 @@ describe( 'rollup', function () {
});
describe.only('incremental', function () {
it('uses previous bundle object to prevent unnecessary transformations', function () {
it('does not transforms in the second time', function () {
var calls = 0;
var counter = {
transform: function ( code ) {
@ -435,12 +435,12 @@ describe( 'rollup', function () {
}
};
return rollup.rollup({
entry: path.join( INCREMENTAL, 'main.js' ),
entry: path.join( INCREMENTAL, 'not-transform-twice', 'main.js' ),
plugins: [counter]
}).then( function ( bundle ) {
assert.equal( calls, 2 );
return rollup.rollup({
entry: path.join( INCREMENTAL, 'main.js' ),
entry: path.join( INCREMENTAL, 'not-transform-twice', 'main.js' ),
plugins: [counter],
bundle
});
@ -449,10 +449,54 @@ describe( 'rollup', function () {
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);
return sander.readFile( path.join( INCREMENTAL, 'not-transform-twice', 'expected.js' ) ).then( function (expected) {
assert.equal( normaliseOutput( expected ), result.code );
});
});
});
it('transforms modified sources', function () {
var calls = 0;
var counter = {
transform: function ( code ) {
calls += 1;
return code;
}
};
var bundle;
var foo;
var changed;
var expected;
return Promise.all([
rollup.rollup({
entry: path.join( INCREMENTAL, 'transform-changed', 'main.js' ),
plugins: [counter]
}),
sander.readFile( path.join( INCREMENTAL, 'transform-changed', 'foo.js' ) ),
sander.readFile( path.join( INCREMENTAL, 'transform-changed', 'foo-changed.js' ) ),
sander.readFile( path.join( INCREMENTAL, 'transform-changed', 'expected.js' ) )
]).then( function ( [_bundle, _foo, _changed, _expected] ) {
bundle = _bundle;
foo = normaliseOutput( _foo );
changed = normaliseOutput( _changed );
expected = normaliseOutput( _expected );
assert.equal( calls, 2 );
return sander.writeFile( INCREMENTAL, 'transform-changed', 'foo.js', changed );
}).then(function () {
return rollup.rollup({
entry: path.join( INCREMENTAL, 'transform-changed', 'main.js' ),
plugins: [counter],
bundle
});
}).then( function ( bundle ) {
assert.equal( calls, 3 );
var result = bundle.generate({
format: 'es6'
});
assert.equal(expected, result.code);
return sander.writeFile( INCREMENTAL, 'transform-changed', 'foo.js', foo );
});
});
});
});

Loading…
Cancel
Save