prepack-webpack-plugin
prepack-webpack-plugin copied to clipboard
Code splits limitation
trafficstars
Detailed Description
Since code split creates multiple independent chunks, prepack can only apply optimizations to chunk "shells", not modules contained in these chunks (maybe except for entry chunk). I wondering if there is any workaround for this ?
Possible Implementation
call __optimize(module) for each module in a chunk
eg: https://prepack.io/repl.html#GYVwdgxgLglg9mABAWzgExAGwKYEYAUAlIgN4BQiioksCVccRpFliATtlCG0gOTANeAbhYBfFtWjwkAIwCGbJuVbtO3PvLbCxLDlx71GxANSJNREeLIB9a3AAOsZDABe2fKgw5chMkA
compiler.hooks.thisCompilation.tap('PrepackPlugin', compilation => {
compilation.chunkTemplate.hooks.render.tap(
{
name: 'PrepackPlugin',
stage: 9999, // stage:9999 // need to be at latest stage
},
(source, chunk, module) => {
const jsonpFunction = compilation.chunkTemplate.outputOptions.jsonpFunction;
const globalObject = compilation.chunkTemplate.outputOptions.globalObject;
source.add(`;(function(){
var chunkModuleList = ${globalObject}[${JSON.stringify(jsonpFunction)}].slice(-1)[0][1];
for (var id in chunkModuleList)
__optimize(chunkModuleList[id]);
})()`);
}
)
});