webpack/lib/ids/NaturalChunkIdsPlugin.js

36 lines
985 B
JavaScript
Raw Normal View History

2018-09-05 20:22:10 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { compareChunksNatural } = require("../util/comparators");
const { assignAscendingChunkIds } = require("./IdHelpers");
2018-09-05 20:22:10 +08:00
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "NaturalChunkIdsPlugin";
2018-09-05 20:22:10 +08:00
class NaturalChunkIdsPlugin {
/**
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) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.chunkIds.tap(PLUGIN_NAME, (chunks) => {
2018-09-05 20:22:10 +08:00
const chunkGraph = compilation.chunkGraph;
const compareNatural = compareChunksNatural(chunkGraph);
2025-07-03 17:06:45 +08:00
/** @type {Chunk[]} */
const chunksInNaturalOrder = [...chunks].sort(compareNatural);
2018-09-05 20:22:10 +08:00
assignAscendingChunkIds(chunksInNaturalOrder, compilation);
});
});
}
}
module.exports = NaturalChunkIdsPlugin;