[79a0317] | 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 | compareModulesByIdentifier,
|
---|
| 12 | compareChunks
|
---|
| 13 | } = require("../util/comparators");
|
---|
| 14 | const createSchemaValidation = require("../util/create-schema-validation");
|
---|
| 15 | const identifierUtils = require("../util/identifier");
|
---|
| 16 |
|
---|
| 17 | /** @typedef {import("../../declarations/plugins/optimize/AggressiveSplittingPlugin").AggressiveSplittingPluginOptions} AggressiveSplittingPluginOptions */
|
---|
| 18 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 19 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 20 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 21 | /** @typedef {import("../Module")} Module */
|
---|
| 22 |
|
---|
| 23 | const validate = createSchemaValidation(
|
---|
| 24 | require("../../schemas/plugins/optimize/AggressiveSplittingPlugin.check.js"),
|
---|
| 25 | () =>
|
---|
| 26 | require("../../schemas/plugins/optimize/AggressiveSplittingPlugin.json"),
|
---|
| 27 | {
|
---|
| 28 | name: "Aggressive Splitting Plugin",
|
---|
| 29 | baseDataPath: "options"
|
---|
| 30 | }
|
---|
| 31 | );
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 35 | * @param {Chunk} oldChunk the old chunk
|
---|
| 36 | * @param {Chunk} newChunk the new chunk
|
---|
| 37 | * @returns {(module: Module) => void} function to move module between chunks
|
---|
| 38 | */
|
---|
| 39 | const moveModuleBetween = (chunkGraph, oldChunk, newChunk) => module => {
|
---|
| 40 | chunkGraph.disconnectChunkAndModule(oldChunk, module);
|
---|
| 41 | chunkGraph.connectChunkAndModule(newChunk, module);
|
---|
| 42 | };
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 46 | * @param {Chunk} chunk the chunk
|
---|
| 47 | * @returns {function(Module): boolean} filter for entry module
|
---|
| 48 | */
|
---|
| 49 | const isNotAEntryModule = (chunkGraph, chunk) => module =>
|
---|
| 50 | !chunkGraph.isEntryModuleInChunk(module, chunk);
|
---|
| 51 |
|
---|
| 52 | /** @type {WeakSet<Chunk>} */
|
---|
| 53 | const recordedChunks = new WeakSet();
|
---|
| 54 |
|
---|
| 55 | class AggressiveSplittingPlugin {
|
---|
| 56 | /**
|
---|
| 57 | * @param {AggressiveSplittingPluginOptions=} options options object
|
---|
| 58 | */
|
---|
| 59 | constructor(options = {}) {
|
---|
| 60 | validate(options);
|
---|
| 61 |
|
---|
| 62 | this.options = options;
|
---|
| 63 | if (typeof this.options.minSize !== "number") {
|
---|
| 64 | this.options.minSize = 30 * 1024;
|
---|
| 65 | }
|
---|
| 66 | if (typeof this.options.maxSize !== "number") {
|
---|
| 67 | this.options.maxSize = 50 * 1024;
|
---|
| 68 | }
|
---|
| 69 | if (typeof this.options.chunkOverhead !== "number") {
|
---|
| 70 | this.options.chunkOverhead = 0;
|
---|
| 71 | }
|
---|
| 72 | if (typeof this.options.entryChunkMultiplicator !== "number") {
|
---|
| 73 | this.options.entryChunkMultiplicator = 1;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * @param {Chunk} chunk the chunk to test
|
---|
| 79 | * @returns {boolean} true if the chunk was recorded
|
---|
| 80 | */
|
---|
| 81 | static wasChunkRecorded(chunk) {
|
---|
| 82 | return recordedChunks.has(chunk);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Apply the plugin
|
---|
| 87 | * @param {Compiler} compiler the compiler instance
|
---|
| 88 | * @returns {void}
|
---|
| 89 | */
|
---|
| 90 | apply(compiler) {
|
---|
| 91 | compiler.hooks.thisCompilation.tap(
|
---|
| 92 | "AggressiveSplittingPlugin",
|
---|
| 93 | compilation => {
|
---|
| 94 | let needAdditionalSeal = false;
|
---|
| 95 | /** @typedef {{ id?: NonNullable<Chunk["id"]>, hash?: NonNullable<Chunk["hash"]>, modules: Module[], size: number }} SplitData */
|
---|
| 96 | /** @type {SplitData[]} */
|
---|
| 97 | let newSplits;
|
---|
| 98 | /** @type {Set<Chunk>} */
|
---|
| 99 | let fromAggressiveSplittingSet;
|
---|
| 100 | /** @type {Map<Chunk, SplitData>} */
|
---|
| 101 | let chunkSplitDataMap;
|
---|
| 102 | compilation.hooks.optimize.tap("AggressiveSplittingPlugin", () => {
|
---|
| 103 | newSplits = [];
|
---|
| 104 | fromAggressiveSplittingSet = new Set();
|
---|
| 105 | chunkSplitDataMap = new Map();
|
---|
| 106 | });
|
---|
| 107 | compilation.hooks.optimizeChunks.tap(
|
---|
| 108 | {
|
---|
| 109 | name: "AggressiveSplittingPlugin",
|
---|
| 110 | stage: STAGE_ADVANCED
|
---|
| 111 | },
|
---|
| 112 | chunks => {
|
---|
| 113 | const chunkGraph = compilation.chunkGraph;
|
---|
| 114 | // Precompute stuff
|
---|
| 115 | const nameToModuleMap = new Map();
|
---|
| 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 | const usedIds = new Set();
|
---|
| 130 | for (const chunk of chunks) {
|
---|
| 131 | usedIds.add(chunk.id);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | const recordedSplits =
|
---|
| 135 | (compilation.records && compilation.records.aggressiveSplits) ||
|
---|
| 136 | [];
|
---|
| 137 | const usedSplits = newSplits
|
---|
| 138 | ? recordedSplits.concat(newSplits)
|
---|
| 139 | : recordedSplits;
|
---|
| 140 |
|
---|
| 141 | const minSize = /** @type {number} */ (this.options.minSize);
|
---|
| 142 | const maxSize = /** @type {number} */ (this.options.maxSize);
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * @param {SplitData} splitData split data
|
---|
| 146 | * @returns {boolean} true when applied, otherwise false
|
---|
| 147 | */
|
---|
| 148 | const applySplit = splitData => {
|
---|
| 149 | // Cannot split if id is already taken
|
---|
| 150 | if (splitData.id !== undefined && usedIds.has(splitData.id)) {
|
---|
| 151 | return false;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | // Get module objects from names
|
---|
| 155 | const selectedModules = splitData.modules.map(name =>
|
---|
| 156 | nameToModuleMap.get(name)
|
---|
| 157 | );
|
---|
| 158 |
|
---|
| 159 | // Does the modules exist at all?
|
---|
| 160 | if (!selectedModules.every(Boolean)) return false;
|
---|
| 161 |
|
---|
| 162 | // Check if size matches (faster than waiting for hash)
|
---|
| 163 | let size = 0;
|
---|
| 164 | for (const m of selectedModules) size += m.size();
|
---|
| 165 | if (size !== splitData.size) return false;
|
---|
| 166 |
|
---|
| 167 | // get chunks with all modules
|
---|
| 168 | const selectedChunks = intersect(
|
---|
| 169 | selectedModules.map(
|
---|
| 170 | m => new Set(chunkGraph.getModuleChunksIterable(m))
|
---|
| 171 | )
|
---|
| 172 | );
|
---|
| 173 |
|
---|
| 174 | // No relevant chunks found
|
---|
| 175 | if (selectedChunks.size === 0) return false;
|
---|
| 176 |
|
---|
| 177 | // The found chunk is already the split or similar
|
---|
| 178 | if (
|
---|
| 179 | selectedChunks.size === 1 &&
|
---|
| 180 | chunkGraph.getNumberOfChunkModules(
|
---|
| 181 | Array.from(selectedChunks)[0]
|
---|
| 182 | ) === selectedModules.length
|
---|
| 183 | ) {
|
---|
| 184 | const chunk = Array.from(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 = /** @type {TODO} */ (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 = Array.from(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 | const selectedModules = [];
|
---|
| 243 | let selectedModulesSize = 0;
|
---|
| 244 | for (let k = 0; k < modules.length; k++) {
|
---|
| 245 | const module = modules[k];
|
---|
| 246 | const newSize = selectedModulesSize + module.size();
|
---|
| 247 | if (newSize > maxSize && selectedModulesSize >= minSize) {
|
---|
| 248 | break;
|
---|
| 249 | }
|
---|
| 250 | selectedModulesSize = newSize;
|
---|
| 251 | selectedModules.push(module);
|
---|
| 252 | }
|
---|
| 253 | if (selectedModules.length === 0) continue;
|
---|
| 254 | /** @type {SplitData} */
|
---|
| 255 | const splitData = {
|
---|
| 256 | modules: selectedModules
|
---|
| 257 | .map(m => moduleToNameMap.get(m))
|
---|
| 258 | .sort(),
|
---|
| 259 | size: selectedModulesSize
|
---|
| 260 | };
|
---|
| 261 |
|
---|
| 262 | if (applySplit(splitData)) {
|
---|
| 263 | newSplits = (newSplits || []).concat(splitData);
|
---|
| 264 | changed = true;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | if (changed) return true;
|
---|
| 269 | }
|
---|
| 270 | );
|
---|
| 271 | compilation.hooks.recordHash.tap(
|
---|
| 272 | "AggressiveSplittingPlugin",
|
---|
| 273 | records => {
|
---|
| 274 | // 4. save made splittings to records
|
---|
| 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 = Array.from(allSplits);
|
---|
| 330 |
|
---|
| 331 | needAdditionalSeal = false;
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 | );
|
---|
| 335 | compilation.hooks.needAdditionalSeal.tap(
|
---|
| 336 | "AggressiveSplittingPlugin",
|
---|
| 337 | () => {
|
---|
| 338 | if (needAdditionalSeal) {
|
---|
| 339 | needAdditionalSeal = false;
|
---|
| 340 | return true;
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | );
|
---|
| 344 | }
|
---|
| 345 | );
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 | module.exports = AggressiveSplittingPlugin;
|
---|