| | |
| | | "use strict"; |
| | | |
| | | const { STAGE_BASIC } = require("../OptimizationStages"); |
| | | const createSchemaValidation = require("../util/create-schema-validation"); |
| | | const { runtimeEqual } = require("../util/runtime"); |
| | | |
| | | /** @typedef {import("../../declarations/plugins/optimize/MergeDuplicateChunksPlugin").MergeDuplicateChunksPluginOptions} MergeDuplicateChunksPluginOptions */ |
| | | /** @typedef {import("../Compiler")} Compiler */ |
| | | |
| | | const validate = createSchemaValidation( |
| | | require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.check"), |
| | | () => |
| | | require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.json"), |
| | | { |
| | | name: "Merge Duplicate Chunks Plugin", |
| | | baseDataPath: "options" |
| | | } |
| | | ); |
| | | /** @typedef {import("../Chunk")} Chunk */ |
| | | |
| | | const PLUGIN_NAME = "MergeDuplicateChunksPlugin"; |
| | | |
| | | class MergeDuplicateChunksPlugin { |
| | | /** |
| | | * @param {MergeDuplicateChunksPluginOptions} options options object |
| | | * Creates an instance of MergeDuplicateChunksPlugin. |
| | | * @param {MergeDuplicateChunksPluginOptions=} options options object |
| | | */ |
| | | constructor(options = { stage: STAGE_BASIC }) { |
| | | validate(options); |
| | | /** @type {MergeDuplicateChunksPluginOptions} */ |
| | | this.options = options; |
| | | } |
| | | |
| | | /** |
| | | * Applies the plugin by registering its hooks on the compiler. |
| | | * @param {Compiler} compiler the compiler |
| | | * @returns {void} |
| | | */ |
| | | apply(compiler) { |
| | | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| | | compiler.validate( |
| | | () => |
| | | require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.json"), |
| | | this.options, |
| | | { |
| | | name: "Merge Duplicate Chunks Plugin", |
| | | baseDataPath: "options" |
| | | }, |
| | | (options) => |
| | | require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.check")( |
| | | options |
| | | ) |
| | | ); |
| | | }); |
| | | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
| | | compilation.hooks.optimizeChunks.tap( |
| | | { |
| | |
| | | const { chunkGraph, moduleGraph } = compilation; |
| | | |
| | | // remember already tested chunks for performance |
| | | /** @type {Set<Chunk>} */ |
| | | const notDuplicates = new Set(); |
| | | |
| | | // for each chunk |
| | | for (const chunk of chunks) { |
| | | // track a Set of all chunk that could be duplicates |
| | | /** @type {Set<Chunk> | undefined} */ |
| | | let possibleDuplicates; |
| | | for (const module of chunkGraph.getChunkModulesIterable(chunk)) { |
| | | if (possibleDuplicates === undefined) { |