From 157b7c151f49097b6aa4e4b68958287a13b01ba1 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 21 May 2016 23:58:51 +0300 Subject: [PATCH] Invalidate module when file content is changed --- src/Bundle.js | 8 ++- src/utils/transform.js | 7 --- .../{ => not-transform-twice}/expected.js | 0 .../{ => not-transform-twice}/foo.js | 0 .../{ => not-transform-twice}/main.js | 0 .../incremental/transform-changed/expected.js | 5 ++ .../transform-changed/foo-changed.js | 3 ++ test/incremental/transform-changed/foo.js | 3 ++ test/incremental/transform-changed/main.js | 3 ++ test/test.js | 54 +++++++++++++++++-- 10 files changed, 70 insertions(+), 13 deletions(-) rename test/incremental/{ => not-transform-twice}/expected.js (100%) rename test/incremental/{ => not-transform-twice}/foo.js (100%) rename test/incremental/{ => not-transform-twice}/main.js (100%) create mode 100644 test/incremental/transform-changed/expected.js create mode 100644 test/incremental/transform-changed/foo-changed.js create mode 100644 test/incremental/transform-changed/foo.js create mode 100644 test/incremental/transform-changed/main.js diff --git a/src/Bundle.js b/src/Bundle.js index 0a56180..0f34a0f 100644 --- a/src/Bundle.js +++ b/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, diff --git a/src/utils/transform.js b/src/utils/transform.js index b401370..c2ffe43 100644 --- a/src/utils/transform.js +++ b/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; diff --git a/test/incremental/expected.js b/test/incremental/not-transform-twice/expected.js similarity index 100% rename from test/incremental/expected.js rename to test/incremental/not-transform-twice/expected.js diff --git a/test/incremental/foo.js b/test/incremental/not-transform-twice/foo.js similarity index 100% rename from test/incremental/foo.js rename to test/incremental/not-transform-twice/foo.js diff --git a/test/incremental/main.js b/test/incremental/not-transform-twice/main.js similarity index 100% rename from test/incremental/main.js rename to test/incremental/not-transform-twice/main.js diff --git a/test/incremental/transform-changed/expected.js b/test/incremental/transform-changed/expected.js new file mode 100644 index 0000000..e1c10a1 --- /dev/null +++ b/test/incremental/transform-changed/expected.js @@ -0,0 +1,5 @@ +function foo () { + console.log('foo-changed'); +}; + +foo(); \ No newline at end of file diff --git a/test/incremental/transform-changed/foo-changed.js b/test/incremental/transform-changed/foo-changed.js new file mode 100644 index 0000000..b96789c --- /dev/null +++ b/test/incremental/transform-changed/foo-changed.js @@ -0,0 +1,3 @@ +export default function () { + console.log('foo-changed'); +}; diff --git a/test/incremental/transform-changed/foo.js b/test/incremental/transform-changed/foo.js new file mode 100644 index 0000000..435568b --- /dev/null +++ b/test/incremental/transform-changed/foo.js @@ -0,0 +1,3 @@ +export default function () { + console.log('foo'); +}; \ No newline at end of file diff --git a/test/incremental/transform-changed/main.js b/test/incremental/transform-changed/main.js new file mode 100644 index 0000000..e1fb5c4 --- /dev/null +++ b/test/incremental/transform-changed/main.js @@ -0,0 +1,3 @@ +import foo from './foo.js'; + +foo(); diff --git a/test/test.js b/test/test.js index 2d8d54c..5389346 100644 --- a/test/test.js +++ b/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 ); + }); + }); }); });