webpack/lib/performance/SizeLimitsPlugin.js

169 lines
4.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sean Larkin @thelarkinn
*/
2018-07-30 23:08:51 +08:00
"use strict";
2018-07-30 23:08:51 +08:00
2018-09-06 22:59:11 +08:00
const { find } = require("../util/SetHelpers");
const AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
2018-07-30 23:08:51 +08:00
const EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
const NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
2018-11-09 05:13:56 +08:00
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").PerformanceOptions} PerformanceOptions */
/** @typedef {import("../ChunkGroup")} ChunkGroup */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Entrypoint")} Entrypoint */
2018-11-09 05:13:56 +08:00
/** @typedef {import("../WebpackError")} WebpackError */
/**
* @typedef {Object} AssetDetails
* @property {string} name
* @property {number} size
*/
/**
* @typedef {Object} EntrypointDetails
* @property {string} name
* @property {number} size
* @property {string[]} files
*/
const isOverSizeLimitSet = new WeakSet();
2019-09-13 17:12:26 +08:00
const excludeSourceMap = (name, source, info) => !info.development;
2018-11-09 05:13:56 +08:00
module.exports = class SizeLimitsPlugin {
2018-11-09 05:13:56 +08:00
/**
* @param {PerformanceOptions} options the plugin options
*/
constructor(options) {
this.hints = options.hints;
this.maxAssetSize = options.maxAssetSize;
this.maxEntrypointSize = options.maxEntrypointSize;
this.assetFilter = options.assetFilter;
}
2018-11-09 05:13:56 +08:00
/**
* @param {ChunkGroup | Source} thing the resource to test
* @returns {boolean} true if over the limit
*/
static isOverSizeLimit(thing) {
return isOverSizeLimitSet.has(thing);
}
2018-11-09 05:13:56 +08:00
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
2018-11-09 05:13:56 +08:00
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const entrypointSizeLimit = this.maxEntrypointSize;
const assetSizeLimit = this.maxAssetSize;
const hints = this.hints;
2018-11-09 05:13:56 +08:00
const assetFilter = this.assetFilter || excludeSourceMap;
2018-02-25 09:00:20 +08:00
compiler.hooks.afterEmit.tap("SizeLimitsPlugin", compilation => {
2018-11-09 05:13:56 +08:00
/** @type {WebpackError[]} */
const warnings = [];
2018-11-09 05:13:56 +08:00
/**
* @param {Entrypoint} entrypoint an entrypoint
* @returns {number} the size of the entrypoint
2018-11-09 05:13:56 +08:00
*/
const getEntrypointSize = entrypoint => {
let size = 0;
for (const file of entrypoint.getFiles()) {
const asset = compilation.getAsset(file);
if (
asset &&
assetFilter(asset.name, asset.source, asset.info) &&
asset.source
) {
size += asset.info.size || asset.source.size();
2018-01-20 05:29:44 +08:00
}
}
return size;
};
2018-11-09 05:13:56 +08:00
/** @type {AssetDetails[]} */
const assetsOverSizeLimit = [];
for (const { name, source, info } of compilation.getAssets()) {
if (!assetFilter(name, source, info) || !source) {
2018-01-22 20:52:43 +08:00
continue;
}
const size = info.size || source.size();
2023-06-13 01:24:59 +08:00
if (size > /** @type {number} */ (assetSizeLimit)) {
2018-01-22 20:52:43 +08:00
assetsOverSizeLimit.push({
name,
size
2018-01-22 20:52:43 +08:00
});
2019-09-13 17:12:26 +08:00
isOverSizeLimitSet.add(source);
2018-01-22 20:52:43 +08:00
}
}
const fileFilter = name => {
const asset = compilation.getAsset(name);
return asset && assetFilter(asset.name, asset.source, asset.info);
};
2018-11-09 05:13:56 +08:00
/** @type {EntrypointDetails[]} */
const entrypointsOverLimit = [];
for (const [name, entry] of compilation.entrypoints) {
const size = getEntrypointSize(entry);
2023-06-13 01:24:59 +08:00
if (size > /** @type {number} */ (entrypointSizeLimit)) {
entrypointsOverLimit.push({
name: name,
size: size,
files: entry.getFiles().filter(fileFilter)
});
isOverSizeLimitSet.add(entry);
}
}
2018-02-25 09:00:20 +08:00
if (hints) {
// 1. Individual Chunk: Size < 250kb
// 2. Collective Initial Chunks [entrypoint] (Each Set?): Size < 250kb
// 3. No Async Chunks
// if !1, then 2, if !2 return
2018-02-25 09:00:20 +08:00
if (assetsOverSizeLimit.length > 0) {
warnings.push(
2023-06-13 01:24:59 +08:00
new AssetsOverSizeLimitWarning(
assetsOverSizeLimit,
/** @type {number} */ (assetSizeLimit)
)
2018-02-25 09:00:20 +08:00
);
}
2018-02-25 09:00:20 +08:00
if (entrypointsOverLimit.length > 0) {
warnings.push(
new EntrypointsOverSizeLimitWarning(
entrypointsOverLimit,
2023-06-13 01:24:59 +08:00
/** @type {number} */ (entrypointSizeLimit)
2018-02-25 09:00:20 +08:00
)
);
}
2018-02-25 09:00:20 +08:00
if (warnings.length > 0) {
2018-09-06 22:59:11 +08:00
const someAsyncChunk = find(
compilation.chunks,
chunk => !chunk.canBeInitial()
);
2018-09-06 22:59:11 +08:00
if (!someAsyncChunk) {
warnings.push(new NoAsyncChunksWarning());
}
2018-02-25 09:00:20 +08:00
if (hints === "error") {
2017-11-08 18:32:05 +08:00
compilation.errors.push(...warnings);
} else {
2017-11-08 18:32:05 +08:00
compilation.warnings.push(...warnings);
}
}
}
});
}
};