UNPKG

1.04 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8/** @typedef {import("../Compiler")} Compiler */
9
10class RuntimeChunkPlugin {
11 constructor(options) {
12 this.options = {
13 name: entrypoint => `runtime~${entrypoint.name}`,
14 ...options
15 };
16 }
17
18 /**
19 * Apply the plugin
20 * @param {Compiler} compiler the compiler instance
21 * @returns {void}
22 */
23 apply(compiler) {
24 compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
25 compilation.hooks.addEntry.tap(
26 "RuntimeChunkPlugin",
27 (_, { name: entryName }) => {
28 if (entryName === undefined) return;
29 const data = compilation.entries.get(entryName);
30 if (data.options.runtime === undefined && !data.options.dependOn) {
31 // Determine runtime chunk name
32 let name = this.options.name;
33 if (typeof name === "function") {
34 name = name({ name: entryName });
35 }
36 data.options.runtime = name;
37 }
38 }
39 );
40 });
41 }
42}
43
44module.exports = RuntimeChunkPlugin;