Browse Source

Cache resolvedIds in modules to improve incremental build

semi-dynamic-namespace-imports
Bogdan Chadkin 9 years ago
parent
commit
684dde0a30
  1. 18
      src/Bundle.js
  2. 7
      src/Module.js
  3. 13
      test/function/has-resolved-ids/_config.js
  4. 1
      test/function/has-resolved-ids/foo.js
  5. 2
      test/function/has-resolved-ids/main.js
  6. 54
      test/test.js

18
src/Bundle.js

@ -201,9 +201,18 @@ export default class Bundle {
return transform( source, id, this.plugins ); return transform( source, id, this.plugins );
}) })
.then( source => { .then( source => {
const { code, originalCode, originalSourceMap, ast, sourceMapChain } = source; const { code, originalCode, originalSourceMap, ast, sourceMapChain, resolvedIds } = source;
const module = new Module({ id, code, originalCode, originalSourceMap, ast, sourceMapChain, bundle: this }); const module = new Module({
id,
code,
originalCode,
originalSourceMap,
ast,
sourceMapChain,
resolvedIds,
bundle: this
});
this.modules.push( module ); this.modules.push( module );
this.moduleById.set( id, module ); this.moduleById.set( id, module );
@ -232,7 +241,8 @@ export default class Bundle {
fetchAllDependencies ( module ) { fetchAllDependencies ( module ) {
return mapSequence( module.sources, source => { return mapSequence( module.sources, source => {
return this.resolveId( source, module.id ) const resolvedId = module.resolvedIds[ source ];
return ( resolvedId ? Promise.resolve( resolvedId ) : this.resolveId( source, module.id ) )
.then( resolvedId => { .then( resolvedId => {
const externalId = resolvedId || ( const externalId = resolvedId || (
isRelative( source ) ? resolve( module.id, '..', source ) : source isRelative( source ) ? resolve( module.id, '..', source ) : source

7
src/Module.js

@ -17,7 +17,7 @@ import { emptyBlockStatement } from './ast/create.js';
import extractNames from './ast/extractNames.js'; import extractNames from './ast/extractNames.js';
export default class Module { export default class Module {
constructor ({ id, code, originalCode, originalSourceMap, ast, sourceMapChain, bundle }) { constructor ({ id, code, originalCode, originalSourceMap, ast, sourceMapChain, resolvedIds, bundle }) {
this.code = code; this.code = code;
this.originalCode = originalCode; this.originalCode = originalCode;
this.originalSourceMap = originalSourceMap; this.originalSourceMap = originalSourceMap;
@ -30,7 +30,7 @@ export default class Module {
// all dependencies // all dependencies
this.sources = []; this.sources = [];
this.dependencies = []; this.dependencies = [];
this.resolvedIds = blank(); this.resolvedIds = resolvedIds || blank();
// imports and exports, indexed by local name // imports and exports, indexed by local name
this.imports = blank(); this.imports = blank();
@ -657,7 +657,8 @@ export default class Module {
code: this.code, code: this.code,
originalCode: this.originalCode, originalCode: this.originalCode,
ast: this.ast, ast: this.ast,
sourceMapChain: this.sourceMapChain sourceMapChain: this.sourceMapChain,
resolvedIds: this.resolvedIds
}; };
} }

13
test/function/has-resolved-ids/_config.js

@ -0,0 +1,13 @@
var path = require( 'path' );
var assert = require( 'assert' );
module.exports = {
description: 'user-facing bundle has resolvedIds map in every modules',
bundle: function ( bundle ) {
assert.ok( bundle.modules[ 0 ].resolvedIds );
assert.ok( bundle.modules[ 1 ].resolvedIds );
assert.equal( Object.keys( bundle.modules[ 0 ].resolvedIds ).length, 0 );
assert.equal( Object.keys( bundle.modules[ 1 ].resolvedIds ).length, 1 );
assert.equal( bundle.modules[ 1 ].resolvedIds[ './foo' ], path.resolve( __dirname, 'foo.js' ) );
}
};

1
test/function/has-resolved-ids/foo.js

@ -0,0 +1 @@
export default 42;

2
test/function/has-resolved-ids/main.js

@ -0,0 +1,2 @@
import foo from './foo';
assert.equal( foo, 42 );

54
test/test.js

@ -470,7 +470,7 @@ describe( 'rollup', function () {
}); });
}); });
describe('incremental', () => { describe( 'incremental', () => {
function executeBundle ( bundle ) { function executeBundle ( bundle ) {
const cjs = bundle.generate({ format: 'cjs' }); const cjs = bundle.generate({ format: 'cjs' });
const m = new Function( 'module', 'exports', cjs.code ); const m = new Function( 'module', 'exports', cjs.code );
@ -481,44 +481,52 @@ describe( 'rollup', function () {
return module.exports; return module.exports;
} }
let calls; let resolveIdCalls;
let transformCalls;
let modules; let modules;
const plugin = { const plugin = {
resolveId: id => id, resolveId: id => {
resolveIdCalls += 1;
return id;
},
load: id => { load: id => {
return modules[ id ]; return modules[ id ];
}, },
transform: code => { transform: code => {
calls += 1; transformCalls += 1;
return code; return code;
} }
}; };
beforeEach( () => { beforeEach( () => {
calls = 0; resolveIdCalls = 0;
transformCalls = 0;
modules = { modules = {
entry: `import foo from 'foo'; export default foo;`, entry: `import foo from 'foo'; export default foo;`,
foo: `export default 42` foo: `export default 42`,
bar: `export default 21`
}; };
}); });
it('does not transforms in the second time', () => { it('does not resolves id and transforms in the second time', () => {
return rollup.rollup({ return rollup.rollup({
entry: 'entry', entry: 'entry',
plugins: [ plugin ] plugins: [ plugin ]
}).then( bundle => { }).then( bundle => {
assert.equal( calls, 2 ); assert.equal( resolveIdCalls, 2 );
assert.equal( transformCalls, 2 );
return rollup.rollup({ return rollup.rollup({
entry: 'entry', entry: 'entry',
plugins: [ plugin ], plugins: [ plugin ],
cache: bundle cache: bundle
}); });
}).then( bundle => { }).then( bundle => {
assert.equal( calls, 2 ); assert.equal( resolveIdCalls, 3 ); // +1 for entry point which is resolved every time
assert.equal( transformCalls, 2 );
assert.equal( executeBundle( bundle ), 42 ); assert.equal( executeBundle( bundle ), 42 );
}); });
}); });
@ -530,7 +538,7 @@ describe( 'rollup', function () {
entry: 'entry', entry: 'entry',
plugins: [ plugin ] plugins: [ plugin ]
}).then( bundle => { }).then( bundle => {
assert.equal( calls, 2 ); assert.equal( transformCalls, 2 );
assert.equal( executeBundle( bundle ), 42 ); assert.equal( executeBundle( bundle ), 42 );
modules.foo = `export default 43`; modules.foo = `export default 43`;
@ -542,10 +550,34 @@ describe( 'rollup', function () {
cache cache
}); });
}).then( bundle => { }).then( bundle => {
assert.equal( calls, 3 ); assert.equal( transformCalls, 3 );
assert.equal( executeBundle( bundle ), 43 ); assert.equal( executeBundle( bundle ), 43 );
}); });
}); });
it('resolves id of new imports', () => {
let cache;
return rollup.rollup({
entry: 'entry',
plugins: [ plugin ]
}).then( bundle => {
assert.equal( resolveIdCalls, 2 );
assert.equal( executeBundle( bundle ), 42 );
modules.entry = `import bar from 'bar'; export default bar;`;
cache = bundle;
}).then( () => {
return rollup.rollup({
entry: 'entry',
plugins: [ plugin ],
cache
});
}).then( bundle => {
assert.equal( resolveIdCalls, 4 );
assert.equal( executeBundle( bundle ), 21 );
});
});
}); });
describe( 'hooks', () => { describe( 'hooks', () => {

Loading…
Cancel
Save