source: frontend/node_modules/webpack/lib/optimize/AggressiveMergingPlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[9af201e]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { STAGE_ADVANCED } = require("../OptimizationStages");
9
10/** @typedef {import("../Chunk")} Chunk */
11/** @typedef {import("../Compiler")} Compiler */
12
13/**
14 * Defines the aggressive merging plugin options type used by this module.
15 * @typedef {object} AggressiveMergingPluginOptions
16 * @property {number=} minSizeReduce minimal size reduction to trigger merging
17 */
18
19const PLUGIN_NAME = "AggressiveMergingPlugin";
20
21class AggressiveMergingPlugin {
22 /**
23 * Creates an instance of AggressiveMergingPlugin.
24 * @param {AggressiveMergingPluginOptions=} options options object
25 */
26 constructor(options) {
27 if (
28 (options !== undefined && typeof options !== "object") ||
29 Array.isArray(options)
30 ) {
31 throw new Error(
32 "Argument should be an options object. To use defaults, pass in nothing.\nFor more info on options, see https://webpack.js.org/plugins/"
33 );
34 }
35 /** @type {AggressiveMergingPluginOptions} */
36 this.options = options || {};
37 }
38
39 /**
40 * Applies the plugin by registering its hooks on the compiler.
41 * @param {Compiler} compiler the compiler instance
42 * @returns {void}
43 */
44 apply(compiler) {
45 const options = this.options;
46 const minSizeReduce = options.minSizeReduce || 1.5;
47
48 compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
49 compilation.hooks.optimizeChunks.tap(
50 {
51 name: PLUGIN_NAME,
52 stage: STAGE_ADVANCED
53 },
54 (chunks) => {
55 const chunkGraph = compilation.chunkGraph;
56 /** @type {{ a: Chunk, b: Chunk, improvement: number }[]} */
57 const combinations = [];
58 for (const a of chunks) {
59 if (a.canBeInitial()) continue;
60 for (const b of chunks) {
61 if (b.canBeInitial()) continue;
62 if (b === a) break;
63 if (!chunkGraph.canChunksBeIntegrated(a, b)) {
64 continue;
65 }
66 const aSize = chunkGraph.getChunkSize(b, {
67 chunkOverhead: 0
68 });
69 const bSize = chunkGraph.getChunkSize(a, {
70 chunkOverhead: 0
71 });
72 const abSize = chunkGraph.getIntegratedChunksSize(b, a, {
73 chunkOverhead: 0
74 });
75 const improvement = (aSize + bSize) / abSize;
76 combinations.push({
77 a,
78 b,
79 improvement
80 });
81 }
82 }
83
84 combinations.sort((a, b) => b.improvement - a.improvement);
85
86 const pair = combinations[0];
87
88 if (!pair) return;
89 if (pair.improvement < minSizeReduce) return;
90
91 chunkGraph.integrateChunks(pair.b, pair.a);
92 compilation.chunks.delete(pair.a);
93 return true;
94 }
95 );
96 });
97 }
98}
99
100module.exports = AggressiveMergingPlugin;
Note: See TracBrowser for help on using the repository browser.