2018-01-20 00:06:59 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
module.exports = class RuntimeChunkPlugin {
|
2018-01-26 01:52:36 +08:00
|
|
|
constructor(options) {
|
|
|
|
this.single = options === "single";
|
|
|
|
}
|
2018-01-20 00:06:59 +08:00
|
|
|
|
|
|
|
apply(compiler) {
|
2018-01-24 19:00:50 +08:00
|
|
|
compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
|
2018-01-20 00:06:59 +08:00
|
|
|
compilation.hooks.optimizeChunksAdvanced.tap("RuntimeChunkPlugin", () => {
|
2018-01-26 01:52:36 +08:00
|
|
|
const singleRuntimeChunk = this.single ? compilation.addChunk("runtime") : undefined;
|
|
|
|
|
2018-01-20 00:06:59 +08:00
|
|
|
for(const entrypoint of compilation.entrypoints.values()) {
|
|
|
|
const chunk = entrypoint.getRuntimeChunk();
|
|
|
|
if(chunk.getNumberOfModules() > 0) {
|
2018-01-26 01:52:36 +08:00
|
|
|
const newChunk = this.single ? singleRuntimeChunk : compilation.addChunk(entrypoint.name + "-runtime");
|
2018-01-20 00:06:59 +08:00
|
|
|
entrypoint.unshiftChunk(newChunk);
|
|
|
|
newChunk.addGroup(entrypoint);
|
|
|
|
entrypoint.setRuntimeChunk(newChunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|