| | |
| | | "use strict"; |
| | | |
| | | const { compareChunksNatural } = require("../util/comparators"); |
| | | const createSchemaValidation = require("../util/create-schema-validation"); |
| | | const { assignAscendingChunkIds } = require("./IdHelpers"); |
| | | |
| | | /** @typedef {import("../../declarations/plugins/ids/OccurrenceChunkIdsPlugin").OccurrenceChunkIdsPluginOptions} OccurrenceChunkIdsPluginOptions */ |
| | | /** @typedef {import("../Chunk")} Chunk */ |
| | | /** @typedef {import("../Compiler")} Compiler */ |
| | | |
| | | const validate = createSchemaValidation( |
| | | require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.check"), |
| | | () => require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.json"), |
| | | { |
| | | name: "Occurrence Order Chunk Ids Plugin", |
| | | baseDataPath: "options" |
| | | } |
| | | ); |
| | | |
| | | const PLUGIN_NAME = "OccurrenceChunkIdsPlugin"; |
| | | |
| | | class OccurrenceChunkIdsPlugin { |
| | | /** |
| | | * Creates an instance of OccurrenceChunkIdsPlugin. |
| | | * @param {OccurrenceChunkIdsPluginOptions=} options options object |
| | | */ |
| | | constructor(options = {}) { |
| | | validate(options); |
| | | /** @type {OccurrenceChunkIdsPluginOptions} */ |
| | | this.options = options; |
| | | } |
| | | |
| | | /** |
| | | * Apply the plugin |
| | | * Applies the plugin by registering its hooks on the compiler. |
| | | * @param {Compiler} compiler the compiler instance |
| | | * @returns {void} |
| | | */ |
| | | apply(compiler) { |
| | | const prioritiseInitial = this.options.prioritiseInitial; |
| | | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| | | compiler.validate( |
| | | () => |
| | | require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.json"), |
| | | this.options, |
| | | { |
| | | name: "Occurrence Order Chunk Ids Plugin", |
| | | baseDataPath: "options" |
| | | }, |
| | | (options) => |
| | | require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.check")( |
| | | options |
| | | ) |
| | | ); |
| | | }); |
| | | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
| | | compilation.hooks.chunkIds.tap(PLUGIN_NAME, (chunks) => { |
| | | const chunkGraph = compilation.chunkGraph; |
| | |
| | | |
| | | /** @type {Chunk[]} */ |
| | | const chunksInOccurrenceOrder = [...chunks].sort((a, b) => { |
| | | if (prioritiseInitial) { |
| | | if (this.options.prioritiseInitial) { |
| | | const aEntryOccurs = |
| | | /** @type {number} */ |
| | | (occursInInitialChunksMap.get(a)); |