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