| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { STAGE_BASIC } = require("../OptimizationStages");
|
|---|
| 9 | const { runtimeEqual } = require("../util/runtime");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../../declarations/plugins/optimize/MergeDuplicateChunksPlugin").MergeDuplicateChunksPluginOptions} MergeDuplicateChunksPluginOptions */
|
|---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 13 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 14 |
|
|---|
| 15 | const PLUGIN_NAME = "MergeDuplicateChunksPlugin";
|
|---|
| 16 |
|
|---|
| 17 | class MergeDuplicateChunksPlugin {
|
|---|
| 18 | /**
|
|---|
| 19 | * Creates an instance of MergeDuplicateChunksPlugin.
|
|---|
| 20 | * @param {MergeDuplicateChunksPluginOptions=} options options object
|
|---|
| 21 | */
|
|---|
| 22 | constructor(options = { stage: STAGE_BASIC }) {
|
|---|
| 23 | /** @type {MergeDuplicateChunksPluginOptions} */
|
|---|
| 24 | this.options = options;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 29 | * @param {Compiler} compiler the compiler
|
|---|
| 30 | * @returns {void}
|
|---|
| 31 | */
|
|---|
| 32 | apply(compiler) {
|
|---|
| 33 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 34 | compiler.validate(
|
|---|
| 35 | () =>
|
|---|
| 36 | require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.json"),
|
|---|
| 37 | this.options,
|
|---|
| 38 | {
|
|---|
| 39 | name: "Merge Duplicate Chunks Plugin",
|
|---|
| 40 | baseDataPath: "options"
|
|---|
| 41 | },
|
|---|
| 42 | (options) =>
|
|---|
| 43 | require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.check")(
|
|---|
| 44 | options
|
|---|
| 45 | )
|
|---|
| 46 | );
|
|---|
| 47 | });
|
|---|
| 48 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 49 | compilation.hooks.optimizeChunks.tap(
|
|---|
| 50 | {
|
|---|
| 51 | name: PLUGIN_NAME,
|
|---|
| 52 | stage: this.options.stage
|
|---|
| 53 | },
|
|---|
| 54 | (chunks) => {
|
|---|
| 55 | const { chunkGraph, moduleGraph } = compilation;
|
|---|
| 56 |
|
|---|
| 57 | // remember already tested chunks for performance
|
|---|
| 58 | /** @type {Set<Chunk>} */
|
|---|
| 59 | const notDuplicates = new Set();
|
|---|
| 60 |
|
|---|
| 61 | // for each chunk
|
|---|
| 62 | for (const chunk of chunks) {
|
|---|
| 63 | // track a Set of all chunk that could be duplicates
|
|---|
| 64 | /** @type {Set<Chunk> | undefined} */
|
|---|
| 65 | let possibleDuplicates;
|
|---|
| 66 | for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
|
|---|
| 67 | if (possibleDuplicates === undefined) {
|
|---|
| 68 | // when possibleDuplicates is not yet set,
|
|---|
| 69 | // create a new Set from chunks of the current module
|
|---|
| 70 | // including only chunks with the same number of modules
|
|---|
| 71 | for (const dup of chunkGraph.getModuleChunksIterable(module)) {
|
|---|
| 72 | if (
|
|---|
| 73 | dup !== chunk &&
|
|---|
| 74 | chunkGraph.getNumberOfChunkModules(chunk) ===
|
|---|
| 75 | chunkGraph.getNumberOfChunkModules(dup) &&
|
|---|
| 76 | !notDuplicates.has(dup)
|
|---|
| 77 | ) {
|
|---|
| 78 | // delay allocating the new Set until here, reduce memory pressure
|
|---|
| 79 | if (possibleDuplicates === undefined) {
|
|---|
| 80 | possibleDuplicates = new Set();
|
|---|
| 81 | }
|
|---|
| 82 | possibleDuplicates.add(dup);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | // when no chunk is possible we can break here
|
|---|
| 86 | if (possibleDuplicates === undefined) break;
|
|---|
| 87 | } else {
|
|---|
| 88 | // validate existing possible duplicates
|
|---|
| 89 | for (const dup of possibleDuplicates) {
|
|---|
| 90 | // remove possible duplicate when module is not contained
|
|---|
| 91 | if (!chunkGraph.isModuleInChunk(module, dup)) {
|
|---|
| 92 | possibleDuplicates.delete(dup);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | // when all chunks has been removed we can break here
|
|---|
| 96 | if (possibleDuplicates.size === 0) break;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | // when we found duplicates
|
|---|
| 101 | if (
|
|---|
| 102 | possibleDuplicates !== undefined &&
|
|---|
| 103 | possibleDuplicates.size > 0
|
|---|
| 104 | ) {
|
|---|
| 105 | outer: for (const otherChunk of possibleDuplicates) {
|
|---|
| 106 | if (otherChunk.hasRuntime() !== chunk.hasRuntime()) continue;
|
|---|
| 107 | if (chunkGraph.getNumberOfEntryModules(chunk) > 0) continue;
|
|---|
| 108 | if (chunkGraph.getNumberOfEntryModules(otherChunk) > 0) {
|
|---|
| 109 | continue;
|
|---|
| 110 | }
|
|---|
| 111 | if (!runtimeEqual(chunk.runtime, otherChunk.runtime)) {
|
|---|
| 112 | for (const module of chunkGraph.getChunkModulesIterable(
|
|---|
| 113 | chunk
|
|---|
| 114 | )) {
|
|---|
| 115 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
|---|
| 116 | if (
|
|---|
| 117 | !exportsInfo.isEquallyUsed(
|
|---|
| 118 | chunk.runtime,
|
|---|
| 119 | otherChunk.runtime
|
|---|
| 120 | )
|
|---|
| 121 | ) {
|
|---|
| 122 | continue outer;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | // merge them
|
|---|
| 127 | if (chunkGraph.canChunksBeIntegrated(chunk, otherChunk)) {
|
|---|
| 128 | chunkGraph.integrateChunks(chunk, otherChunk);
|
|---|
| 129 | compilation.chunks.delete(otherChunk);
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | // don't check already processed chunks twice
|
|---|
| 135 | notDuplicates.add(chunk);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | );
|
|---|
| 139 | });
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | module.exports = MergeDuplicateChunksPlugin;
|
|---|