| [9af201e] | 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_ADVANCED } = require("../OptimizationStages");
|
|---|
| 9 | const { intersect } = require("../util/SetHelpers");
|
|---|
| 10 | const {
|
|---|
| 11 | compareChunks,
|
|---|
| 12 | compareModulesByIdentifier
|
|---|
| 13 | } = require("../util/comparators");
|
|---|
| 14 | const identifierUtils = require("../util/identifier");
|
|---|
| 15 |
|
|---|
| 16 | /** @typedef {import("../../declarations/plugins/optimize/AggressiveSplittingPlugin").AggressiveSplittingPluginOptions} AggressiveSplittingPluginOptions */
|
|---|
| 17 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 18 | /** @typedef {import("../Chunk").ChunkId} ChunkId */
|
|---|
| 19 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
|---|
| 20 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 21 | /** @typedef {import("../Module")} Module */
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Move module between.
|
|---|
| 25 | * @param {ChunkGraph} chunkGraph the chunk graph
|
|---|
| 26 | * @param {Chunk} oldChunk the old chunk
|
|---|
| 27 | * @param {Chunk} newChunk the new chunk
|
|---|
| 28 | * @returns {(module: Module) => void} function to move module between chunks
|
|---|
| 29 | */
|
|---|
| 30 | const moveModuleBetween = (chunkGraph, oldChunk, newChunk) => (module) => {
|
|---|
| 31 | chunkGraph.disconnectChunkAndModule(oldChunk, module);
|
|---|
| 32 | chunkGraph.connectChunkAndModule(newChunk, module);
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Checks whether this object is not a entry module.
|
|---|
| 37 | * @param {ChunkGraph} chunkGraph the chunk graph
|
|---|
| 38 | * @param {Chunk} chunk the chunk
|
|---|
| 39 | * @returns {(module: Module) => boolean} filter for entry module
|
|---|
| 40 | */
|
|---|
| 41 | const isNotAEntryModule = (chunkGraph, chunk) => (module) =>
|
|---|
| 42 | !chunkGraph.isEntryModuleInChunk(module, chunk);
|
|---|
| 43 |
|
|---|
| 44 | /** @typedef {{ id?: NonNullable<Chunk["id"]>, hash?: NonNullable<Chunk["hash"]>, modules: string[], size: number }} SplitData */
|
|---|
| 45 |
|
|---|
| 46 | /** @type {WeakSet<Chunk>} */
|
|---|
| 47 | const recordedChunks = new WeakSet();
|
|---|
| 48 |
|
|---|
| 49 | const PLUGIN_NAME = "AggressiveSplittingPlugin";
|
|---|
| 50 |
|
|---|
| 51 | class AggressiveSplittingPlugin {
|
|---|
| 52 | /**
|
|---|
| 53 | * Creates an instance of AggressiveSplittingPlugin.
|
|---|
| 54 | * @param {AggressiveSplittingPluginOptions=} options options object
|
|---|
| 55 | */
|
|---|
| 56 | constructor(options = {}) {
|
|---|
| 57 | /** @type {AggressiveSplittingPluginOptions} */
|
|---|
| 58 | this.options = options;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Was chunk recorded.
|
|---|
| 63 | * @param {Chunk} chunk the chunk to test
|
|---|
| 64 | * @returns {boolean} true if the chunk was recorded
|
|---|
| 65 | */
|
|---|
| 66 | static wasChunkRecorded(chunk) {
|
|---|
| 67 | return recordedChunks.has(chunk);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 72 | * @param {Compiler} compiler the compiler instance
|
|---|
| 73 | * @returns {void}
|
|---|
| 74 | */
|
|---|
| 75 | apply(compiler) {
|
|---|
| 76 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 77 | compiler.validate(
|
|---|
| 78 | () =>
|
|---|
| 79 | require("../../schemas/plugins/optimize/AggressiveSplittingPlugin.json"),
|
|---|
| 80 | this.options,
|
|---|
| 81 | {
|
|---|
| 82 | name: "Aggressive Splitting Plugin",
|
|---|
| 83 | baseDataPath: "options"
|
|---|
| 84 | },
|
|---|
| 85 | (options) =>
|
|---|
| 86 | require("../../schemas/plugins/optimize/AggressiveSplittingPlugin.check")(
|
|---|
| 87 | options
|
|---|
| 88 | )
|
|---|
| 89 | );
|
|---|
| 90 | });
|
|---|
| 91 |
|
|---|
| 92 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 93 | let needAdditionalSeal = false;
|
|---|
| 94 | /** @type {SplitData[]} */
|
|---|
| 95 | let newSplits;
|
|---|
| 96 | /** @type {Set<Chunk>} */
|
|---|
| 97 | let fromAggressiveSplittingSet;
|
|---|
| 98 | /** @type {Map<Chunk, SplitData>} */
|
|---|
| 99 | let chunkSplitDataMap;
|
|---|
| 100 | compilation.hooks.optimize.tap(PLUGIN_NAME, () => {
|
|---|
| 101 | newSplits = [];
|
|---|
| 102 | fromAggressiveSplittingSet = new Set();
|
|---|
| 103 | chunkSplitDataMap = new Map();
|
|---|
| 104 | });
|
|---|
| 105 | compilation.hooks.optimizeChunks.tap(
|
|---|
| 106 | {
|
|---|
| 107 | name: PLUGIN_NAME,
|
|---|
| 108 | stage: STAGE_ADVANCED
|
|---|
| 109 | },
|
|---|
| 110 | (chunks) => {
|
|---|
| 111 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 112 | // Precompute stuff
|
|---|
| 113 | /** @type {Map<string, Module>} */
|
|---|
| 114 | const nameToModuleMap = new Map();
|
|---|
| 115 | /** @type {Map<Module, string>} */
|
|---|
| 116 | const moduleToNameMap = new Map();
|
|---|
| 117 | const makePathsRelative =
|
|---|
| 118 | identifierUtils.makePathsRelative.bindContextCache(
|
|---|
| 119 | compiler.context,
|
|---|
| 120 | compiler.root
|
|---|
| 121 | );
|
|---|
| 122 | for (const m of compilation.modules) {
|
|---|
| 123 | const name = makePathsRelative(m.identifier());
|
|---|
| 124 | nameToModuleMap.set(name, m);
|
|---|
| 125 | moduleToNameMap.set(m, name);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | // Check used chunk ids
|
|---|
| 129 | /** @type {Set<ChunkId>} */
|
|---|
| 130 | const usedIds = new Set();
|
|---|
| 131 | for (const chunk of chunks) {
|
|---|
| 132 | usedIds.add(/** @type {ChunkId} */ (chunk.id));
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | const recordedSplits =
|
|---|
| 136 | (compilation.records && compilation.records.aggressiveSplits) || [];
|
|---|
| 137 | const usedSplits = newSplits
|
|---|
| 138 | ? [...recordedSplits, ...newSplits]
|
|---|
| 139 | : recordedSplits;
|
|---|
| 140 |
|
|---|
| 141 | const minSize = this.options.minSize || 30 * 1024;
|
|---|
| 142 | const maxSize = this.options.maxSize || 50 * 1024;
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Returns true when applied, otherwise false.
|
|---|
| 146 | * @param {SplitData} splitData split data
|
|---|
| 147 | * @returns {boolean} true when applied, otherwise false
|
|---|
| 148 | */
|
|---|
| 149 | const applySplit = (splitData) => {
|
|---|
| 150 | // Cannot split if id is already taken
|
|---|
| 151 | if (splitData.id !== undefined && usedIds.has(splitData.id)) {
|
|---|
| 152 | return false;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | // Get module objects from names
|
|---|
| 156 | const selectedModules = splitData.modules.map(
|
|---|
| 157 | (name) => /** @type {Module} */ (nameToModuleMap.get(name))
|
|---|
| 158 | );
|
|---|
| 159 |
|
|---|
| 160 | // Does the modules exist at all?
|
|---|
| 161 | if (!selectedModules.every(Boolean)) return false;
|
|---|
| 162 |
|
|---|
| 163 | // Check if size matches (faster than waiting for hash)
|
|---|
| 164 | let size = 0;
|
|---|
| 165 | for (const m of selectedModules) size += m.size();
|
|---|
| 166 | if (size !== splitData.size) return false;
|
|---|
| 167 |
|
|---|
| 168 | // get chunks with all modules
|
|---|
| 169 | const selectedChunks = intersect(
|
|---|
| 170 | selectedModules.map(
|
|---|
| 171 | (m) => new Set(chunkGraph.getModuleChunksIterable(m))
|
|---|
| 172 | )
|
|---|
| 173 | );
|
|---|
| 174 |
|
|---|
| 175 | // No relevant chunks found
|
|---|
| 176 | if (selectedChunks.size === 0) return false;
|
|---|
| 177 |
|
|---|
| 178 | // The found chunk is already the split or similar
|
|---|
| 179 | if (
|
|---|
| 180 | selectedChunks.size === 1 &&
|
|---|
| 181 | chunkGraph.getNumberOfChunkModules([...selectedChunks][0]) ===
|
|---|
| 182 | selectedModules.length
|
|---|
| 183 | ) {
|
|---|
| 184 | const chunk = [...selectedChunks][0];
|
|---|
| 185 | if (fromAggressiveSplittingSet.has(chunk)) return false;
|
|---|
| 186 | fromAggressiveSplittingSet.add(chunk);
|
|---|
| 187 | chunkSplitDataMap.set(chunk, splitData);
|
|---|
| 188 | return true;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | // split the chunk into two parts
|
|---|
| 192 | const newChunk = compilation.addChunk();
|
|---|
| 193 | newChunk.chunkReason = "aggressive splitted";
|
|---|
| 194 | for (const chunk of selectedChunks) {
|
|---|
| 195 | for (const module of selectedModules) {
|
|---|
| 196 | moveModuleBetween(chunkGraph, chunk, newChunk)(module);
|
|---|
| 197 | }
|
|---|
| 198 | chunk.split(newChunk);
|
|---|
| 199 | chunk.name = null;
|
|---|
| 200 | }
|
|---|
| 201 | fromAggressiveSplittingSet.add(newChunk);
|
|---|
| 202 | chunkSplitDataMap.set(newChunk, splitData);
|
|---|
| 203 |
|
|---|
| 204 | if (splitData.id !== null && splitData.id !== undefined) {
|
|---|
| 205 | newChunk.id = splitData.id;
|
|---|
| 206 | newChunk.ids = [splitData.id];
|
|---|
| 207 | }
|
|---|
| 208 | return true;
|
|---|
| 209 | };
|
|---|
| 210 |
|
|---|
| 211 | // try to restore to recorded splitting
|
|---|
| 212 | let changed = false;
|
|---|
| 213 | for (let j = 0; j < usedSplits.length; j++) {
|
|---|
| 214 | const splitData = usedSplits[j];
|
|---|
| 215 | if (applySplit(splitData)) changed = true;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | // for any chunk which isn't splitted yet, split it and create a new entry
|
|---|
| 219 | // start with the biggest chunk
|
|---|
| 220 | const cmpFn = compareChunks(chunkGraph);
|
|---|
| 221 | const sortedChunks = [...chunks].sort((a, b) => {
|
|---|
| 222 | const diff1 =
|
|---|
| 223 | chunkGraph.getChunkModulesSize(b) -
|
|---|
| 224 | chunkGraph.getChunkModulesSize(a);
|
|---|
| 225 | if (diff1) return diff1;
|
|---|
| 226 | const diff2 =
|
|---|
| 227 | chunkGraph.getNumberOfChunkModules(a) -
|
|---|
| 228 | chunkGraph.getNumberOfChunkModules(b);
|
|---|
| 229 | if (diff2) return diff2;
|
|---|
| 230 | return cmpFn(a, b);
|
|---|
| 231 | });
|
|---|
| 232 | for (const chunk of sortedChunks) {
|
|---|
| 233 | if (fromAggressiveSplittingSet.has(chunk)) continue;
|
|---|
| 234 | const size = chunkGraph.getChunkModulesSize(chunk);
|
|---|
| 235 | if (
|
|---|
| 236 | size > maxSize &&
|
|---|
| 237 | chunkGraph.getNumberOfChunkModules(chunk) > 1
|
|---|
| 238 | ) {
|
|---|
| 239 | const modules = chunkGraph
|
|---|
| 240 | .getOrderedChunkModules(chunk, compareModulesByIdentifier)
|
|---|
| 241 | .filter(isNotAEntryModule(chunkGraph, chunk));
|
|---|
| 242 | /** @type {Module[]} */
|
|---|
| 243 | const selectedModules = [];
|
|---|
| 244 | let selectedModulesSize = 0;
|
|---|
| 245 | for (let k = 0; k < modules.length; k++) {
|
|---|
| 246 | const module = modules[k];
|
|---|
| 247 | const newSize = selectedModulesSize + module.size();
|
|---|
| 248 | if (newSize > maxSize && selectedModulesSize >= minSize) {
|
|---|
| 249 | break;
|
|---|
| 250 | }
|
|---|
| 251 | selectedModulesSize = newSize;
|
|---|
| 252 | selectedModules.push(module);
|
|---|
| 253 | }
|
|---|
| 254 | if (selectedModules.length === 0) continue;
|
|---|
| 255 | /** @type {SplitData} */
|
|---|
| 256 | const splitData = {
|
|---|
| 257 | modules: selectedModules
|
|---|
| 258 | .map((m) => /** @type {string} */ (moduleToNameMap.get(m)))
|
|---|
| 259 | .sort(),
|
|---|
| 260 | size: selectedModulesSize
|
|---|
| 261 | };
|
|---|
| 262 |
|
|---|
| 263 | if (applySplit(splitData)) {
|
|---|
| 264 | newSplits = [...(newSplits || []), splitData];
|
|---|
| 265 | changed = true;
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 | }
|
|---|
| 269 | if (changed) return true;
|
|---|
| 270 | }
|
|---|
| 271 | );
|
|---|
| 272 | compilation.hooks.recordHash.tap(PLUGIN_NAME, (records) => {
|
|---|
| 273 | // 4. save made splittings to records
|
|---|
| 274 | /** @type {Set<SplitData>} */
|
|---|
| 275 | const allSplits = new Set();
|
|---|
| 276 | /** @type {Set<SplitData>} */
|
|---|
| 277 | const invalidSplits = new Set();
|
|---|
| 278 |
|
|---|
| 279 | // Check if some splittings are invalid
|
|---|
| 280 | // We remove invalid splittings and try again
|
|---|
| 281 | for (const chunk of compilation.chunks) {
|
|---|
| 282 | const splitData = chunkSplitDataMap.get(chunk);
|
|---|
| 283 | if (
|
|---|
| 284 | splitData !== undefined &&
|
|---|
| 285 | splitData.hash &&
|
|---|
| 286 | chunk.hash !== splitData.hash
|
|---|
| 287 | ) {
|
|---|
| 288 | // Split was successful, but hash doesn't equal
|
|---|
| 289 | // We can throw away the split since it's useless now
|
|---|
| 290 | invalidSplits.add(splitData);
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | if (invalidSplits.size > 0) {
|
|---|
| 295 | records.aggressiveSplits =
|
|---|
| 296 | /** @type {SplitData[]} */
|
|---|
| 297 | (records.aggressiveSplits).filter(
|
|---|
| 298 | (splitData) => !invalidSplits.has(splitData)
|
|---|
| 299 | );
|
|---|
| 300 | needAdditionalSeal = true;
|
|---|
| 301 | } else {
|
|---|
| 302 | // set hash and id values on all (new) splittings
|
|---|
| 303 | for (const chunk of compilation.chunks) {
|
|---|
| 304 | const splitData = chunkSplitDataMap.get(chunk);
|
|---|
| 305 | if (splitData !== undefined) {
|
|---|
| 306 | splitData.hash =
|
|---|
| 307 | /** @type {NonNullable<Chunk["hash"]>} */
|
|---|
| 308 | (chunk.hash);
|
|---|
| 309 | splitData.id =
|
|---|
| 310 | /** @type {NonNullable<Chunk["id"]>} */
|
|---|
| 311 | (chunk.id);
|
|---|
| 312 | allSplits.add(splitData);
|
|---|
| 313 | // set flag for stats
|
|---|
| 314 | recordedChunks.add(chunk);
|
|---|
| 315 | }
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | // Also add all unused historical splits (after the used ones)
|
|---|
| 319 | // They can still be used in some future compilation
|
|---|
| 320 | const recordedSplits =
|
|---|
| 321 | compilation.records && compilation.records.aggressiveSplits;
|
|---|
| 322 | if (recordedSplits) {
|
|---|
| 323 | for (const splitData of recordedSplits) {
|
|---|
| 324 | if (!invalidSplits.has(splitData)) allSplits.add(splitData);
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | // record all splits
|
|---|
| 329 | records.aggressiveSplits = [...allSplits];
|
|---|
| 330 |
|
|---|
| 331 | needAdditionalSeal = false;
|
|---|
| 332 | }
|
|---|
| 333 | });
|
|---|
| 334 | compilation.hooks.needAdditionalSeal.tap(PLUGIN_NAME, () => {
|
|---|
| 335 | if (needAdditionalSeal) {
|
|---|
| 336 | needAdditionalSeal = false;
|
|---|
| 337 | return true;
|
|---|
| 338 | }
|
|---|
| 339 | });
|
|---|
| 340 | });
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | module.exports = AggressiveSplittingPlugin;
|
|---|