TinyChunksPlugin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const TinyChunksWarning = require("../errors/TinyChunksWarning");
  7. const { compareStrings } = require("../util/comparators");
  8. /** @import { PerformanceOptions } from "../../declarations/WebpackOptions" */
  9. /** @import Compiler from "../Compiler" */
  10. /** @import { TinyChunkDetails } from "../errors/TinyChunksWarning" */
  11. const PLUGIN_NAME = "TinyChunksPlugin";
  12. // Enough to name the offenders without printing the chunk graph.
  13. const MAX_REPORTED_CHUNKS = 5;
  14. // One small chunk is a lazy route; a crowd of them is a request pattern, and
  15. // only then does grouping them save more than it costs.
  16. const MIN_REPORTED_CHUNKS = 10;
  17. // What `splitChunks.minSize` falls back to when it is absent or given per
  18. // source type rather than as one number.
  19. const DEFAULT_MIN_SIZE = 20000;
  20. class TinyChunksPlugin {
  21. /**
  22. * Creates an instance of TinyChunksPlugin.
  23. * @param {PerformanceOptions} options the plugin options
  24. */
  25. constructor(options) {
  26. /** @type {PerformanceOptions["hints"]} */
  27. this.hints = options.hints;
  28. }
  29. /**
  30. * Applies the plugin by registering its hooks on the compiler.
  31. * @param {Compiler} compiler the compiler instance
  32. * @returns {void}
  33. */
  34. apply(compiler) {
  35. const hints = this.hints;
  36. if (!hints) return;
  37. const { splitChunks } = compiler.options.optimization;
  38. const configured = splitChunks && splitChunks.minSize;
  39. /**
  40. * Read from `splitChunks.minSize`, so raising the size worth splitting
  41. * raises what counts as too small. Either one number or one per type.
  42. * @param {string} sourceType the type in question
  43. * @returns {number} the floor below which a chunk of it is too small
  44. */
  45. const minSizeFor = (sourceType) => {
  46. if (typeof configured === "number") return configured;
  47. if (configured) {
  48. const forType = configured[sourceType];
  49. if (typeof forType === "number") return forType;
  50. }
  51. return DEFAULT_MIN_SIZE;
  52. };
  53. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  54. // `afterSeal` is past the hash, which folds every message into it — a
  55. // hint reported earlier would change the build's identity.
  56. compilation.hooks.afterSeal.tap(PLUGIN_NAME, () => {
  57. const { chunkGraph } = compilation;
  58. /** @type {TinyChunkDetails[]} */
  59. const tiny = [];
  60. let totalSize = 0;
  61. for (const chunk of compilation.chunks) {
  62. // An initial chunk is downloaded whatever its size; only one fetched
  63. // on demand costs a request of its own.
  64. if (chunk.canBeInitial()) continue;
  65. const sizes = chunkGraph.getChunkModulesSizes(chunk);
  66. const types = Object.keys(sizes);
  67. if (types.length === 0) continue;
  68. // Under its own floor for every type it carries: a chunk holding
  69. // enough of any one of them is worth the request it costs.
  70. let size = 0;
  71. let small = true;
  72. for (const type of types) {
  73. size += sizes[type];
  74. if (sizes[type] >= minSizeFor(type)) small = false;
  75. }
  76. if (!small) continue;
  77. totalSize += size;
  78. tiny.push({ name: chunk.name || `${chunk.id}`, size });
  79. }
  80. if (tiny.length < MIN_REPORTED_CHUNKS) return;
  81. // Smallest first, and ties break by name: chunk order is not stable.
  82. tiny.sort((a, b) => a.size - b.size || compareStrings(a.name, b.name));
  83. const warning = new TinyChunksWarning(
  84. tiny.slice(0, MAX_REPORTED_CHUNKS),
  85. tiny.length,
  86. totalSize
  87. );
  88. if (hints === "error") {
  89. compilation.errors.push(warning);
  90. } else if (hints === "stats") {
  91. compilation.hints.push(warning);
  92. } else {
  93. compilation.warnings.push(warning);
  94. }
  95. });
  96. });
  97. }
  98. }
  99. module.exports = TinyChunksPlugin;