2018-09-05 20:22:10 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-10-17 21:30:17 +08:00
|
|
|
const { compareChunksNatural } = require("../util/comparators");
|
2018-12-07 19:26:35 +08:00
|
|
|
const {
|
2025-07-03 17:06:45 +08:00
|
|
|
assignAscendingChunkIds,
|
2018-12-07 19:26:35 +08:00
|
|
|
assignNames,
|
2025-07-03 17:06:45 +08:00
|
|
|
getLongChunkName,
|
|
|
|
getShortChunkName,
|
|
|
|
getUsedChunkIds
|
2018-12-07 19:26:35 +08:00
|
|
|
} = require("./IdHelpers");
|
2018-09-05 20:22:10 +08:00
|
|
|
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
2023-05-27 01:21:35 +08:00
|
|
|
/**
|
2024-06-11 21:09:50 +08:00
|
|
|
* @typedef {object} NamedChunkIdsPluginOptions
|
2025-04-16 22:04:11 +08:00
|
|
|
* @property {string=} context context
|
|
|
|
* @property {string=} delimiter delimiter
|
2023-05-27 01:21:35 +08:00
|
|
|
*/
|
|
|
|
|
2025-04-23 20:03:37 +08:00
|
|
|
const PLUGIN_NAME = "NamedChunkIdsPlugin";
|
|
|
|
|
2018-09-05 20:22:10 +08:00
|
|
|
class NamedChunkIdsPlugin {
|
2023-05-27 01:21:35 +08:00
|
|
|
/**
|
|
|
|
* @param {NamedChunkIdsPluginOptions=} options options
|
|
|
|
*/
|
2018-12-07 19:26:35 +08:00
|
|
|
constructor(options) {
|
2018-12-07 21:43:24 +08:00
|
|
|
this.delimiter = (options && options.delimiter) || "-";
|
2018-12-07 19:26:35 +08:00
|
|
|
this.context = options && options.context;
|
2018-09-05 20:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
2018-09-05 20:22:10 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
2025-07-17 00:13:14 +08:00
|
|
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
2025-08-20 18:50:12 +08:00
|
|
|
const hashFunction = compilation.outputOptions.hashFunction;
|
2025-07-17 00:13:14 +08:00
|
|
|
compilation.hooks.chunkIds.tap(PLUGIN_NAME, (chunks) => {
|
2018-09-05 20:22:10 +08:00
|
|
|
const chunkGraph = compilation.chunkGraph;
|
2018-12-07 19:26:35 +08:00
|
|
|
const context = this.context ? this.context : compiler.context;
|
|
|
|
const delimiter = this.delimiter;
|
2018-09-05 20:22:10 +08:00
|
|
|
|
2018-12-07 19:26:35 +08:00
|
|
|
const unnamedChunks = assignNames(
|
2025-07-17 00:13:14 +08:00
|
|
|
[...chunks].filter((chunk) => {
|
2018-12-07 22:14:14 +08:00
|
|
|
if (chunk.name) {
|
|
|
|
chunk.id = chunk.name;
|
|
|
|
chunk.ids = [chunk.name];
|
|
|
|
}
|
2018-12-07 19:26:35 +08:00
|
|
|
return chunk.id === null;
|
|
|
|
}),
|
2025-07-17 00:13:14 +08:00
|
|
|
(chunk) =>
|
2019-01-19 19:40:00 +08:00
|
|
|
getShortChunkName(
|
|
|
|
chunk,
|
|
|
|
chunkGraph,
|
|
|
|
context,
|
|
|
|
delimiter,
|
2021-09-22 18:12:46 +08:00
|
|
|
hashFunction,
|
2019-01-19 19:40:00 +08:00
|
|
|
compiler.root
|
|
|
|
),
|
2025-07-17 00:13:14 +08:00
|
|
|
(chunk) =>
|
2018-12-08 01:12:04 +08:00
|
|
|
getLongChunkName(
|
|
|
|
chunk,
|
|
|
|
chunkGraph,
|
|
|
|
context,
|
|
|
|
delimiter,
|
2021-09-22 18:12:46 +08:00
|
|
|
hashFunction,
|
2018-12-08 01:12:04 +08:00
|
|
|
compiler.root
|
|
|
|
),
|
2018-12-07 19:26:35 +08:00
|
|
|
compareChunksNatural(chunkGraph),
|
|
|
|
getUsedChunkIds(compilation),
|
|
|
|
(chunk, name) => {
|
|
|
|
chunk.id = name;
|
|
|
|
chunk.ids = [name];
|
2018-09-05 20:22:10 +08:00
|
|
|
}
|
2018-12-07 19:26:35 +08:00
|
|
|
);
|
|
|
|
if (unnamedChunks.length > 0) {
|
|
|
|
assignAscendingChunkIds(unnamedChunks, compilation);
|
2018-09-05 20:22:10 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NamedChunkIdsPlugin;
|