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