webpack/lib/optimize/RuntimeChunkPlugin.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2023-06-04 01:52:25 +08:00
/** @typedef {import("../Compilation").EntryData} EntryData */
2018-11-09 05:59:19 +08:00
/** @typedef {import("../Compiler")} Compiler */
2023-06-04 01:52:25 +08:00
/** @typedef {import("../Entrypoint")} Entrypoint */
2018-11-09 05:59:19 +08:00
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "RuntimeChunkPlugin";
2025-05-01 22:36:51 +08:00
/** @typedef {(entrypoint: { name: string }) => string} RuntimeChunkFunction */
class RuntimeChunkPlugin {
2024-02-22 22:20:17 +08:00
/**
2025-05-01 22:36:51 +08:00
* @param {{ name?: RuntimeChunkFunction }=} options options
2024-02-22 22:20:17 +08:00
*/
constructor(options) {
this.options = {
2025-05-01 22:36:51 +08:00
/** @type {RuntimeChunkFunction} */
name: entrypoint => `runtime~${entrypoint.name}`,
...options
};
}
2018-11-09 05:59:19 +08:00
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
2018-11-09 05:59:19 +08:00
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
2025-04-23 20:03:37 +08:00
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
compilation.hooks.addEntry.tap(PLUGIN_NAME, (_, { name: entryName }) => {
if (entryName === undefined) return;
const data =
/** @type {EntryData} */
(compilation.entries.get(entryName));
if (data.options.runtime === undefined && !data.options.dependOn) {
// Determine runtime chunk name
let name =
2025-05-01 22:36:51 +08:00
/** @type {string | RuntimeChunkFunction} */
2025-04-23 20:03:37 +08:00
(this.options.name);
if (typeof name === "function") {
name = name({ name: entryName });
}
2025-04-23 20:03:37 +08:00
data.options.runtime = name;
}
2025-04-23 20:03:37 +08:00
});
});
}
}
module.exports = RuntimeChunkPlugin;