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

Commit

Permalink
ensure plugin bundle hook applies for Rollup (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 17, 2016
1 parent 48d5173 commit 9785944
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 84 deletions.
5 changes: 3 additions & 2 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ Builder.prototype.buildStatic = function(expressionOrTree, outFile, opts) {
return compileTree(self.loader, inlinedTree, traceOpts, compileOpts, outputOpts, self.cache.compile);

// attempt rollup optimizations of ESM entry points
return rollupTree(self.loader, inlinedTree, [], traceOpts, compileOpts)
return rollupTree(self.loader, inlinedTree, [], traceOpts, compileOpts, outputOpts)
.then(function(rolledUp) {
inlineMap = rolledUp.inlineMap;

Expand All @@ -715,14 +715,15 @@ Builder.prototype.buildStatic = function(expressionOrTree, outFile, opts) {
else if (rolledUp.outputs)
return {
outputs: (compileOpts.formatHint ? [getFormatHint(compileOpts)] : []).concat(rolledUp.outputs),
assetList: rolledUp.assetList
};
})
.then(function(compiled) {
return {
modules: Object.keys(tree).filter(function(m) {
return tree[m] && !tree[m].conditional;
}),
assetList: [],
assetList: compiled.assetList || [],
outputs: compiled.outputs
};
})
Expand Down
184 changes: 109 additions & 75 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,76 @@ function getTreeModulesPostOrder(tree, traceOpts) {
};
}

// run the plugin bundle hook on the list of loads
// returns the assetList
exports.pluginBundleHook = pluginBundleHook;
function pluginBundleHook(loader, loads, compileOpts, outputOpts) {
var outputs = [];
// plugins have the ability to report an asset list during builds
var assetList = [];
var pluginLoads = {};

// store just plugin loads
loads.forEach(function(load) {
if (load.metadata.loader) {
var pluginLoad = extend({}, load);
pluginLoad.address = loader.baseURL + load.path;
(pluginLoads[load.metadata.loader] = pluginLoads[load.metadata.loader] || []).push(pluginLoad);
}
});

return Promise.all(Object.keys(pluginLoads).map(function(pluginName) {
var loads = pluginLoads[pluginName];
var loaderModule = loads[0].metadata.loaderModule;

return Promise.resolve()
.then(function() {
if (loaderModule.listAssets)
return Promise.resolve(loaderModule.listAssets.call(loader.pluginLoader, loads, compileOpts, outputOpts))
.then(function(_assetList) {
assetList = assetList.concat(_assetList.map(function(asset) {
return {
url: asset.url,
type: asset.type,
source: asset.source,
sourceMap: asset.sourceMap
};
}));
});
})
.then(function() {
if (compileOpts.inlinePlugins) {
if (loaderModule.inline) {
return Promise.resolve(loaderModule.inline.call(loader.pluginLoader, loads, compileOpts, outputOpts));
}
// NB deprecate bundle hook for inline hook
else if (loaderModule.bundle) {
// NB deprecate the 2 argument form
if (loaderModule.bundle.length < 3)
return Promise.resolve(loaderModule.bundle.call(loader.pluginLoader, loads, extend(extend({}, compileOpts), outputOpts)));
else
return Promise.resolve(loaderModule.bundle.call(loader.pluginLoader, loads, compileOpts, outputOpts));
}
}
});
}))
.then(function(compiled) {
var outputs = [];
compiled = compiled || [];
compiled.forEach(function(output) {
if (output instanceof Array)
outputs = outputs.concat(output);
else if (output)
outputs.push(output);
});

return {
outputs: outputs,
assetList: assetList
};
});
}

exports.compileTree = compileTree;
function compileTree(loader, tree, traceOpts, compileOpts, outputOpts, cache) {

Expand All @@ -203,13 +273,8 @@ function compileTree(loader, tree, traceOpts, compileOpts, outputOpts, cache) {

var modules;

// plugins have the ability to report an asset list during builds
var assetList = [];

var outputs = [];

// store plugins with a bundle hook to allow post-processing
var pluginLoads = {};
var compilers = {};

return Promise.resolve()
Expand Down Expand Up @@ -260,13 +325,6 @@ function compileTree(loader, tree, traceOpts, compileOpts, outputOpts, cache) {
if (load === true)
throw new TypeError(name + ' was defined via a bundle, so can only be used for subtraction or union operations.');

// store plugin loads for bundle hook
if (load.metadata.loader) {
var pluginLoad = extend({}, load);
pluginLoad.address = loader.baseURL + load.path;
(pluginLoads[load.metadata.loader] = pluginLoads[load.metadata.loader] || []).push(pluginLoad);
}

return compileLoad(loader, tree[name], compileOpts, cache);
});
}));
Expand All @@ -277,74 +335,50 @@ function compileTree(loader, tree, traceOpts, compileOpts, outputOpts, cache) {

// run plugin bundle hook
.then(function() {
return Promise.all(Object.keys(pluginLoads).map(function(pluginName) {
var loads = pluginLoads[pluginName];
var loaderModule = loads[0].metadata.loaderModule;
var pluginLoads = [];

return Promise.resolve()
.then(function() {
if (loaderModule.listAssets)
return Promise.resolve(loaderModule.listAssets.call(loader.pluginLoader, loads, compileOpts, outputOpts))
.then(function(_assetList) {
assetList = assetList.concat(_assetList.map(function(asset) {
return {
url: asset.url,
type: asset.type,
source: asset.source,
sourceMap: asset.sourceMap
};
}));
});
})
.then(function() {
if (compileOpts.inlinePlugins) {
if (loaderModule.inline) {
return Promise.resolve(loaderModule.inline.call(loader.pluginLoader, loads, compileOpts, outputOpts));
}
// NB deprecate bundle hook for inline hook
else if (loaderModule.bundle) {
// NB deprecate the 2 argument form
if (loaderModule.bundle.length < 3)
return Promise.resolve(loaderModule.bundle.call(loader.pluginLoader, loads, extend(extend({}, compileOpts), outputOpts)));
else
return Promise.resolve(loaderModule.bundle.call(loader.pluginLoader, loads, compileOpts, outputOpts));
}
}
});
}))
.then(function(compiled) {
compiled = compiled || [];
compiled.forEach(function(output) {
if (output instanceof Array)
outputs = outputs.concat(output);
else if (output)
outputs.push(output);
});
});
})
.then(function() {
modules.forEach(function(name) {
var load = tree[name];

// if any module in the bundle is AMD, add a "bundle" meta to the bundle
// this can be deprecated if https://github.com/systemjs/builder/issues/264 lands
if (modules.some(function(name) {
return tree[name].metadata.format == 'amd';
}) && !compileOpts.static)
outputs.unshift('"bundle";');
pluginLoads.push(load);

// static bundle wraps with a self-executing loader
if (compileOpts.static)
return wrapSFXOutputs(loader, tree, modules, outputs, entryPoints, compileOpts, cache);
// if we used Rollup, we should still run the bundle hook for the child loads that were compacted
if (load.compactedLoads)
load.compactedLoads.forEach(function(load) {
pluginLoads.push(load);
});
});

return outputs;
return pluginBundleHook(loader, pluginLoads, compileOpts, outputOpts);
})
.then(function(outputs) {
// NB also include all aliases of all entryPoints along with entryPoints
return {
outputs: outputs,
entryPoints: entryPoints,
assetList: assetList,
modules: modules.reverse()
};
.then(function(pluginResults) {
outputs = outputs.concat(pluginResults.outputs);
var assetList = pluginResults.assetList;

return Promise.resolve()
.then(function() {
// if any module in the bundle is AMD, add a "bundle" meta to the bundle
// this can be deprecated if https://github.com/systemjs/builder/issues/264 lands
if (modules.some(function(name) {
return tree[name].metadata.format == 'amd';
}) && !compileOpts.static)
outputs.unshift('"bundle";');

// static bundle wraps with a self-executing loader
if (compileOpts.static)
return wrapSFXOutputs(loader, tree, modules, outputs, entryPoints, compileOpts, cache);

return outputs;
})
.then(function(outputs) {
// NB also include all aliases of all entryPoints along with entryPoints
return {
outputs: outputs,
entryPoints: entryPoints,
assetList: assetList,
modules: modules.reverse()
};
});
});
}

Expand Down
28 changes: 22 additions & 6 deletions lib/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ var traverseTree = require('./arithmetic').traverseTree;
var getConditionModule = require('./trace').getConditionModule;
var extend = require('./utils').extend;
var getAlias = require('./utils').getAlias;
var pluginBundleHook = require('./compile').pluginBundleHook;

exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts) {
exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts, outputOpts) {
/*
* 1. Determine the tree entry points and optimization points
*
Expand Down Expand Up @@ -209,6 +210,7 @@ exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts)

return Promise.all(Object.keys(optimizationGraphExternals).map(function(entryPoint) {
var externals = optimizationGraphExternals[entryPoint];
var loadList = [];

// if all externals are outside the tree then this really is a full tree rollup
if (fullTreeRollup)
Expand Down Expand Up @@ -236,6 +238,9 @@ exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts)
return resolved;
},
load: function(id, options) {
if (loadList.indexOf(tree[id]) == -1)
loadList.push(tree[id]);
loadList.push(tree[id]);
return {
code: tree[id].metadata.originalSource || tree[id].source,
map: tree[id].metadata.sourceMap
Expand Down Expand Up @@ -308,15 +313,26 @@ exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts)
originalSource: undefined,
sourceMap: output.map
}),
source: output.code
source: output.code,
compactedLoads: loadList
});
});
}))
.then(function(outputs) {
if (fullTreeRollup)
return {
outputs: outputs
};
if (fullTreeRollup) {
// for the full tree rollup case, we need to run the plugin bundle hook as we skip compile entirely
return pluginBundleHook(loader, Object.keys(tree).map(function(name) {
return tree[name];
}).filter(function(load) {
return load;
}), compileOpts, outputOpts)
.then(function(pluginResult) {
return {
outputs: outputs.concat(pluginResult.outputs),
assetList: pluginResult.assetList
};
});
}

return {
tree: rolledUpTree,
Expand Down
5 changes: 4 additions & 1 deletion lib/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ Trace.prototype.getLoadRecord = function(canonical, traceOpts, parentStack) {
isPackageConfig: isPackageConfig(loader, canonical),

// these are only populated by the separate builder.getDeferredImports(tree) method
deferredImports: null
deferredImports: null,

// in the case of Rollup, a single load can represent several compacted loads
compactedLoads: null,
};
var curHook = 'locate';
var originalSource;
Expand Down

0 comments on commit 9785944

Please sign in to comment.