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("../Compilation").EntryData} EntryData */
|
---|
9 | /** @typedef {import("../Compiler")} Compiler */
|
---|
10 | /** @typedef {import("../Entrypoint")} Entrypoint */
|
---|
11 |
|
---|
12 | class RuntimeChunkPlugin {
|
---|
13 | /**
|
---|
14 | * @param {{ name?: (entrypoint: { name: string }) => string }} options options
|
---|
15 | */
|
---|
16 | constructor(options) {
|
---|
17 | this.options = {
|
---|
18 | /**
|
---|
19 | * @param {Entrypoint} entrypoint entrypoint name
|
---|
20 | * @returns {string} runtime chunk name
|
---|
21 | */
|
---|
22 | name: entrypoint => `runtime~${entrypoint.name}`,
|
---|
23 | ...options
|
---|
24 | };
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Apply the plugin
|
---|
29 | * @param {Compiler} compiler the compiler instance
|
---|
30 | * @returns {void}
|
---|
31 | */
|
---|
32 | apply(compiler) {
|
---|
33 | compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
|
---|
34 | compilation.hooks.addEntry.tap(
|
---|
35 | "RuntimeChunkPlugin",
|
---|
36 | (_, { name: entryName }) => {
|
---|
37 | if (entryName === undefined) return;
|
---|
38 | const data =
|
---|
39 | /** @type {EntryData} */
|
---|
40 | (compilation.entries.get(entryName));
|
---|
41 | if (data.options.runtime === undefined && !data.options.dependOn) {
|
---|
42 | // Determine runtime chunk name
|
---|
43 | let name =
|
---|
44 | /** @type {string | ((entrypoint: { name: string }) => string)} */
|
---|
45 | (this.options.name);
|
---|
46 | if (typeof name === "function") {
|
---|
47 | name = name({ name: entryName });
|
---|
48 | }
|
---|
49 | data.options.runtime = name;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | );
|
---|
53 | });
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | module.exports = RuntimeChunkPlugin;
|
---|