source: trip-planner-front/node_modules/webpack/lib/performance/SizeLimitsPlugin.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Sean Larkin @thelarkinn
4*/
5
6"use strict";
7
8const { find } = require("../util/SetHelpers");
9const AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
10const EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
11const NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
12
13/** @typedef {import("webpack-sources").Source} Source */
14/** @typedef {import("../../declarations/WebpackOptions").PerformanceOptions} PerformanceOptions */
15/** @typedef {import("../ChunkGroup")} ChunkGroup */
16/** @typedef {import("../Compiler")} Compiler */
17/** @typedef {import("../Entrypoint")} Entrypoint */
18/** @typedef {import("../WebpackError")} WebpackError */
19
20/**
21 * @typedef {Object} AssetDetails
22 * @property {string} name
23 * @property {number} size
24 */
25
26/**
27 * @typedef {Object} EntrypointDetails
28 * @property {string} name
29 * @property {number} size
30 * @property {string[]} files
31 */
32
33const isOverSizeLimitSet = new WeakSet();
34
35const excludeSourceMap = (name, source, info) => !info.development;
36
37module.exports = class SizeLimitsPlugin {
38 /**
39 * @param {PerformanceOptions} options the plugin options
40 */
41 constructor(options) {
42 this.hints = options.hints;
43 this.maxAssetSize = options.maxAssetSize;
44 this.maxEntrypointSize = options.maxEntrypointSize;
45 this.assetFilter = options.assetFilter;
46 }
47
48 /**
49 * @param {ChunkGroup | Source} thing the resource to test
50 * @returns {boolean} true if over the limit
51 */
52 static isOverSizeLimit(thing) {
53 return isOverSizeLimitSet.has(thing);
54 }
55
56 /**
57 * Apply the plugin
58 * @param {Compiler} compiler the compiler instance
59 * @returns {void}
60 */
61 apply(compiler) {
62 const entrypointSizeLimit = this.maxEntrypointSize;
63 const assetSizeLimit = this.maxAssetSize;
64 const hints = this.hints;
65 const assetFilter = this.assetFilter || excludeSourceMap;
66
67 compiler.hooks.afterEmit.tap("SizeLimitsPlugin", compilation => {
68 /** @type {WebpackError[]} */
69 const warnings = [];
70
71 /**
72 * @param {Entrypoint} entrypoint an entrypoint
73 * @returns {number} the size of the entrypoint
74 */
75 const getEntrypointSize = entrypoint => {
76 let size = 0;
77 for (const file of entrypoint.getFiles()) {
78 const asset = compilation.getAsset(file);
79 if (
80 asset &&
81 assetFilter(asset.name, asset.source, asset.info) &&
82 asset.source
83 ) {
84 size += asset.info.size || asset.source.size();
85 }
86 }
87 return size;
88 };
89
90 /** @type {AssetDetails[]} */
91 const assetsOverSizeLimit = [];
92 for (const { name, source, info } of compilation.getAssets()) {
93 if (!assetFilter(name, source, info) || !source) {
94 continue;
95 }
96
97 const size = info.size || source.size();
98 if (size > assetSizeLimit) {
99 assetsOverSizeLimit.push({
100 name,
101 size
102 });
103 isOverSizeLimitSet.add(source);
104 }
105 }
106
107 const fileFilter = name => {
108 const asset = compilation.getAsset(name);
109 return asset && assetFilter(asset.name, asset.source, asset.info);
110 };
111
112 /** @type {EntrypointDetails[]} */
113 const entrypointsOverLimit = [];
114 for (const [name, entry] of compilation.entrypoints) {
115 const size = getEntrypointSize(entry);
116
117 if (size > entrypointSizeLimit) {
118 entrypointsOverLimit.push({
119 name: name,
120 size: size,
121 files: entry.getFiles().filter(fileFilter)
122 });
123 isOverSizeLimitSet.add(entry);
124 }
125 }
126
127 if (hints) {
128 // 1. Individual Chunk: Size < 250kb
129 // 2. Collective Initial Chunks [entrypoint] (Each Set?): Size < 250kb
130 // 3. No Async Chunks
131 // if !1, then 2, if !2 return
132 if (assetsOverSizeLimit.length > 0) {
133 warnings.push(
134 new AssetsOverSizeLimitWarning(assetsOverSizeLimit, assetSizeLimit)
135 );
136 }
137 if (entrypointsOverLimit.length > 0) {
138 warnings.push(
139 new EntrypointsOverSizeLimitWarning(
140 entrypointsOverLimit,
141 entrypointSizeLimit
142 )
143 );
144 }
145
146 if (warnings.length > 0) {
147 const someAsyncChunk = find(
148 compilation.chunks,
149 chunk => !chunk.canBeInitial()
150 );
151
152 if (!someAsyncChunk) {
153 warnings.push(new NoAsyncChunksWarning());
154 }
155
156 if (hints === "error") {
157 compilation.errors.push(...warnings);
158 } else {
159 compilation.warnings.push(...warnings);
160 }
161 }
162 }
163 });
164 }
165};
Note: See TracBrowser for help on using the repository browser.