webpack/lib/performance/SizeLimitsPlugin.js

115 lines
2.9 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
const AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
2018-07-30 23:08:51 +08:00
const EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
const NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
const isOverSizeLimitSet = new WeakSet();
module.exports = class SizeLimitsPlugin {
constructor(options) {
this.hints = options.hints;
this.maxAssetSize = options.maxAssetSize;
this.maxEntrypointSize = options.maxEntrypointSize;
this.assetFilter = options.assetFilter;
}
static isOverSizeLimit(thing) {
return isOverSizeLimitSet.has(thing);
}
apply(compiler) {
const entrypointSizeLimit = this.maxEntrypointSize;
const assetSizeLimit = this.maxAssetSize;
const hints = this.hints;
2018-02-25 09:00:20 +08:00
const assetFilter = this.assetFilter || (asset => !asset.endsWith(".map"));
2018-02-25 09:00:20 +08:00
compiler.hooks.afterEmit.tap("SizeLimitsPlugin", compilation => {
const warnings = [];
const getEntrypointSize = entrypoint =>
2018-02-25 09:00:20 +08:00
entrypoint.getFiles().reduce((currentSize, file) => {
if (assetFilter(file) && compilation.assets[file]) {
2018-01-20 05:29:44 +08:00
return currentSize + compilation.assets[file].size();
}
return currentSize;
}, 0);
const assetsOverSizeLimit = [];
2018-02-25 09:00:20 +08:00
for (const assetName of Object.keys(compilation.assets)) {
if (!assetFilter(assetName)) {
2018-01-22 20:52:43 +08:00
continue;
}
const asset = compilation.assets[assetName];
const size = asset.size();
2018-02-25 09:00:20 +08:00
if (size > assetSizeLimit) {
2018-01-22 20:52:43 +08:00
assetsOverSizeLimit.push({
name: assetName,
2018-02-25 09:00:20 +08:00
size: size
2018-01-22 20:52:43 +08:00
});
isOverSizeLimitSet.add(asset);
2018-01-22 20:52:43 +08:00
}
}
const entrypointsOverLimit = [];
2018-02-25 09:00:20 +08:00
for (const pair of compilation.entrypoints) {
const name = pair[0];
const entry = pair[1];
const size = getEntrypointSize(entry);
2018-02-25 09:00:20 +08:00
if (size > entrypointSizeLimit) {
entrypointsOverLimit.push({
name: name,
size: size,
files: entry.getFiles().filter(assetFilter)
});
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(
2018-02-25 09:00:20 +08:00
new AssetsOverSizeLimitWarning(assetsOverSizeLimit, assetSizeLimit)
);
}
2018-02-25 09:00:20 +08:00
if (entrypointsOverLimit.length > 0) {
warnings.push(
new EntrypointsOverSizeLimitWarning(
entrypointsOverLimit,
2018-02-25 09:00:20 +08:00
entrypointSizeLimit
)
);
}
2018-02-25 09:00:20 +08:00
if (warnings.length > 0) {
const hasAsyncChunks =
compilation.chunks.filter(chunk => !chunk.canBeInitial()).length >
0;
2018-02-25 09:00:20 +08:00
if (!hasAsyncChunks) {
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);
}
}
}
});
}
};