Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
ensure external depMap merging through rollup optimization (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Feb 15, 2016
1 parent a8e6fac commit d935eed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
38 changes: 29 additions & 9 deletions lib/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts)
if (inlinedModules.indexOf(moduleName) == -1)
rolledUpTree[moduleName] = tree[moduleName];
});

// compute the inlineMap
var inlineMap = {};
inlinedModules.forEach(function(moduleName) {
var optimizationParent = optimizationParentMap[moduleName];
(inlineMap[optimizationParent] = inlineMap[optimizationParent] || []).push(moduleName);
});

// if every module in the tree is rolled-up, then we can do a full tree rollup
var fullTreeRollup = entryPoints.length == 1 && optimizationPoints.length == 1 && Object.keys(optimizationGraphExternals).length == 1;
Expand Down Expand Up @@ -270,10 +277,29 @@ exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts)
};

// replace the entry point module itself with the inlined subgraph module
var curInlined = inlineMap[entryPoint];

// merge all external deps across all inlined modules
// when a duplicate dependency is hit, throw as we don't
// have the ability to handle renaming without https://github.com/rollup/rollup/issues/424
var mergedDepMap = extend({}, entryPointLoad.depMap);
var mergedDeps = [].concat(entryPointLoad.deps.filter(function(dep) {
return externals.indexOf(entryPointLoad.depMap[dep]) != -1;
}));

curInlined.forEach(function(inlinedModule) {
var inlinedLoad = tree[inlinedModule];

extend(mergedDepMap, inlinedLoad.depMap);

mergedDeps = mergedDeps.concat(inlinedLoad.deps.filter(function(dep) {
return externals.indexOf(inlinedLoad.depMap[dep]) != -1;
}));
});

rolledUpTree[entryPoint] = extend(extend({}, entryPointLoad), {
deps: entryPointLoad.deps.filter(function(dep) {
return externals.indexOf(entryPointLoad.depMap[dep]) != -1;
}),
deps: mergedDeps,
depMap: mergedDepMap,
metadata: extend(extend({}, entryPointLoad.metadata), {
originalSource: undefined,
sourceMap: output.map
Expand All @@ -288,12 +314,6 @@ exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts)
outputs: outputs
};

var inlineMap = {};
inlinedModules.forEach(function(moduleName) {
var optimizationParent = optimizationParentMap[moduleName];
(inlineMap[optimizationParent] = inlineMap[optimizationParent] || []).push(moduleName);
});

return {
tree: rolledUpTree,
inlineMap: inlineMap
Expand Down
6 changes: 3 additions & 3 deletions test/static-optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ builder.config({

suite('SFX Optimizations', function() {
test('All ES6 rollup optimization', function(done) {
builder.buildStatic('a.js', 'test/output/es-sfx.js', { runtime: false, minify: minify, format: 'esm' })
builder.buildStatic('a.js', 'test/output/es-sfx.js', { runtime: false, minify: minify, format: 'esm', rollup: true })
.then(function(output) {
assert(output.source, 'var b = \'b\';\n\nvar a = \'a\';\n\nexport { a, b };');
done();
}, done)
});

test('ES6 rollup with a global dep', function(done) {
builder.buildStatic('global-dep.js', 'test/output/es-sfx.js', { runtime: false, minify: minify, format: 'esm', encodeNames: true })
builder.buildStatic('global-dep.js', 'test/output/es-sfx.js', { runtime: false, minify: minify, format: 'esm', rollup: true })
.then(function(output) {
assert(output.source.indexOf('System.registerDynamic("4"') != -1 && output.source.indexOf(', ["4"') != -1);
assert(output.source.indexOf('System.registerDynamic("2"') != -1 && output.source.indexOf(', ["2"') != -1);
done();
}, done);
});
Expand Down

0 comments on commit d935eed

Please sign in to comment.