SizeLimitsPlugin.js
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sean Larkin @thelarkinn
*/
"use strict";
const EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
const AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
const NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
module.exports = class SizeLimitsPlugin {
constructor(options) {
this.hints = options.hints;
this.maxAssetSize = options.maxAssetSize;
this.maxEntrypointSize = options.maxEntrypointSize;
this.assetFilter = options.assetFilter;
}
apply(compiler) {
const entrypointSizeLimit = this.maxEntrypointSize;
const assetSizeLimit = this.maxAssetSize;
const hints = this.hints;
const assetFilter = this.assetFilter || (asset => !(/\.map$/.test(asset)));
compiler.plugin("after-emit", (compilation, callback) => {
const warnings = [];
const getEntrypointSize = entrypoint =>
entrypoint.getFiles()
.filter(assetFilter)
.map(file => compilation.assets[file])
.filter(Boolean)
.map(asset => asset.size())
.reduce((currentSize, nextSize) => currentSize + nextSize, 0);
const assetsOverSizeLimit = [];
Object.keys(compilation.assets)
.filter(assetFilter)
.forEach(assetName => {
const asset = compilation.assets[assetName];
const size = asset.size();
if(size > assetSizeLimit) {
assetsOverSizeLimit.push({
name: assetName,
size: size,
});
asset.isOverSizeLimit = true;
}
});
const entrypointsOverLimit = [];
Object.keys(compilation.entrypoints)
.forEach(key => {
const entry = compilation.entrypoints[key];
const size = getEntrypointSize(entry, compilation);
if(size > entrypointSizeLimit) {
entrypointsOverLimit.push({
name: key,
size: size,
files: entry.getFiles().filter(assetFilter)
});
entry.isOverSizeLimit = true;
}
});
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
if(assetsOverSizeLimit.length > 0) {
warnings.push(
new AssetsOverSizeLimitWarning(
assetsOverSizeLimit,
assetSizeLimit));
}
if(entrypointsOverLimit.length > 0) {
warnings.push(
new EntrypointsOverSizeLimitWarning(
entrypointsOverLimit,
entrypointSizeLimit));
}
if(warnings.length > 0) {
const hasAsyncChunks = compilation.chunks.filter(chunk => !chunk.isInitial()).length > 0;
if(!hasAsyncChunks) {
warnings.push(new NoAsyncChunksWarning());
}
if(hints === "error") {
Array.prototype.push.apply(compilation.errors, warnings);
} else {
Array.prototype.push.apply(compilation.warnings, warnings);
}
}
}
callback();
});
}
};