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