Usage: new SingleRuntimeChunkPlugin({name: chunkName})
const SingleRuntimeChunkPlugin = require('./SingleRuntimeChunkPlugin.js');
module.exports = {
plugins: [new SingleRuntimeChunkPlugin({name: 'webpack-runtime'})]
};
module.exports = class SingleRuntimeChunkPlugin { | |
constructor(options) { | |
this.name = options.name || 'runtime'; | |
this.runtimeChunk = null; | |
} | |
apply(compiler) { | |
compiler.hooks.thisCompilation.tap('SingleRuntimeChunkPlugin', compilation => { | |
compilation.hooks.optimizeChunksAdvanced.tap('SingleRuntimeChunkPlugin', () => { | |
for(const entrypoint of compilation.entrypoints.values()) { | |
const chunk = entrypoint.getRuntimeChunk(); | |
if(chunk.getNumberOfModules() > 0) { | |
if (!this.runtimeChunk) { | |
this.runtimeChunk = compilation.addChunk(this.name); | |
} | |
entrypoint.unshiftChunk(this.runtimeChunk); | |
this.runtimeChunk.addGroup(entrypoint); | |
entrypoint.setRuntimeChunk(this.runtimeChunk); | |
} | |
} | |
}); | |
}); | |
} | |
}; |