Skip to content

Instantly share code, notes, and snippets.

@benthemonkey
Last active January 25, 2018 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benthemonkey/05eceda801b8e1cee5a4dfd368caab2b to your computer and use it in GitHub Desktop.
Save benthemonkey/05eceda801b8e1cee5a4dfd368caab2b to your computer and use it in GitHub Desktop.
SingleRuntimeChunkPlugin

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);
}
}
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment