webpack/lib/optimize/RuntimeChunkPlugin.js

53 lines
1.3 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
class RuntimeChunkPlugin {
constructor(options) {
this.options = {
2023-06-04 01:52:25 +08:00
/**
* @param {Entrypoint} entrypoint entrypoint name
* @returns {string} runtime chunk name
*/
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) {
compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
compilation.hooks.addEntry.tap(
"RuntimeChunkPlugin",
(_, { name: entryName }) => {
if (entryName === undefined) return;
2023-06-04 01:52:25 +08:00
const data =
/** @type {EntryData} */
(compilation.entries.get(entryName));
if (data.options.runtime === undefined && !data.options.dependOn) {
// Determine runtime chunk name
let name = this.options.name;
if (typeof name === "function") {
name = name({ name: entryName });
}
data.options.runtime = name;
}
}
);
});
}
}
module.exports = RuntimeChunkPlugin;