You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sean Larkin @thelarkinn
  4. */
  5. "use strict";
  6. const EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
  7. const AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
  8. const NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
  9. /** @typedef {import("../Compiler")} Compiler */
  10. /** @typedef {import("../Entrypoint")} Entrypoint */
  11. module.exports = class SizeLimitsPlugin {
  12. constructor(options) {
  13. this.hints = options.hints;
  14. this.maxAssetSize = options.maxAssetSize;
  15. this.maxEntrypointSize = options.maxEntrypointSize;
  16. this.assetFilter = options.assetFilter;
  17. }
  18. /**
  19. * @param {Compiler} compiler webpack compiler
  20. * @returns {void}
  21. */
  22. apply(compiler) {
  23. const entrypointSizeLimit = this.maxEntrypointSize;
  24. const assetSizeLimit = this.maxAssetSize;
  25. const hints = this.hints;
  26. const assetFilter =
  27. this.assetFilter || ((name, source, info) => !info.development);
  28. compiler.hooks.afterEmit.tap("SizeLimitsPlugin", compilation => {
  29. const warnings = [];
  30. /**
  31. * @param {Entrypoint} entrypoint an entrypoint
  32. * @returns {number} the size of the entrypoint
  33. */
  34. const getEntrypointSize = entrypoint =>
  35. entrypoint.getFiles().reduce((currentSize, file) => {
  36. const asset = compilation.getAsset(file);
  37. if (
  38. asset &&
  39. assetFilter(asset.name, asset.source, asset.info) &&
  40. asset.source
  41. ) {
  42. return currentSize + (asset.info.size || asset.source.size());
  43. }
  44. return currentSize;
  45. }, 0);
  46. const assetsOverSizeLimit = [];
  47. for (const { name, source, info } of compilation.getAssets()) {
  48. if (!assetFilter(name, source, info) || !source) {
  49. continue;
  50. }
  51. const size = info.size || source.size();
  52. if (size > assetSizeLimit) {
  53. assetsOverSizeLimit.push({
  54. name,
  55. size
  56. });
  57. /** @type {any} */ (source).isOverSizeLimit = true;
  58. }
  59. }
  60. const fileFilter = name => {
  61. const asset = compilation.getAsset(name);
  62. return asset && assetFilter(asset.name, asset.source, asset.info);
  63. };
  64. const entrypointsOverLimit = [];
  65. for (const [name, entry] of compilation.entrypoints) {
  66. const size = getEntrypointSize(entry);
  67. if (size > entrypointSizeLimit) {
  68. entrypointsOverLimit.push({
  69. name: name,
  70. size: size,
  71. files: entry.getFiles().filter(fileFilter)
  72. });
  73. /** @type {any} */ (entry).isOverSizeLimit = true;
  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(assetsOverSizeLimit, assetSizeLimit)
  84. );
  85. }
  86. if (entrypointsOverLimit.length > 0) {
  87. warnings.push(
  88. new EntrypointsOverSizeLimitWarning(
  89. entrypointsOverLimit,
  90. entrypointSizeLimit
  91. )
  92. );
  93. }
  94. if (warnings.length > 0) {
  95. const hasAsyncChunks =
  96. compilation.chunks.filter(chunk => !chunk.canBeInitial()).length >
  97. 0;
  98. if (!hasAsyncChunks) {
  99. warnings.push(new NoAsyncChunksWarning());
  100. }
  101. if (hints === "error") {
  102. compilation.errors.push(...warnings);
  103. } else {
  104. compilation.warnings.push(...warnings);
  105. }
  106. }
  107. }
  108. });
  109. }
  110. };