ScopeHoistingBailoutsPlugin.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const ScopeHoistingBailoutsWarning = require("../errors/ScopeHoistingBailoutsWarning");
  7. const {
  8. BAILOUT_PREFIX,
  9. REJECTED_PREFIX
  10. } = require("../optimize/ModuleConcatenationPlugin");
  11. const { compareStrings } = require("../util/comparators");
  12. const getModuleSize = require("./getModuleSize");
  13. /** @import { PerformanceOptions } from "../../declarations/WebpackOptions" */
  14. /** @import Compiler from "../Compiler" */
  15. /** @import Module from "../Module" */
  16. /** @import RequestShortener from "../RequestShortener" */
  17. /**
  18. * @import {
  19. * ScopeHoistingBailoutGroup
  20. * } from "../errors/ScopeHoistingBailoutsWarning"
  21. */
  22. const PLUGIN_NAME = "ScopeHoistingBailoutsPlugin";
  23. // Reasons repeat far more than they differ, so a few name the problem.
  24. const MAX_REPORTED_REASONS = 5;
  25. // Enough per reason to recognize the offenders without listing every module.
  26. const MAX_REPORTED_NAMES = 3;
  27. // A source position makes two identical reasons look like two problems.
  28. const POSITION_REGEXP = / at \d+:\d+-\d+$/;
  29. /**
  30. * The module's own reasons for staying out of a scope, without the ones it
  31. * only carries because something it imports bailed out.
  32. * @param {(string | ((requestShortener: RequestShortener) => string))[]} bailouts what the module graph recorded
  33. * @param {RequestShortener} requestShortener the request shortener
  34. * @returns {string[]} the reasons, stripped of their prefix and position
  35. */
  36. const getOwnReasons = (bailouts, requestShortener) => {
  37. /** @type {string[]} */
  38. const reasons = [];
  39. for (const bailout of bailouts) {
  40. const message =
  41. typeof bailout === "function" ? bailout(requestShortener) : bailout;
  42. if (!message.startsWith(BAILOUT_PREFIX)) continue;
  43. const reason = message.slice(BAILOUT_PREFIX.length);
  44. // One root lists every module it could not take in; each of those
  45. // modules reports the same problem itself.
  46. if (reason.startsWith(REJECTED_PREFIX)) continue;
  47. reasons.push(reason.replace(POSITION_REGEXP, ""));
  48. }
  49. return reasons;
  50. };
  51. class ScopeHoistingBailoutsPlugin {
  52. /**
  53. * Creates an instance of ScopeHoistingBailoutsPlugin.
  54. * @param {PerformanceOptions} options the plugin options
  55. */
  56. constructor(options) {
  57. /** @type {PerformanceOptions["hints"]} */
  58. this.hints = options.hints;
  59. }
  60. /**
  61. * Applies the plugin by registering its hooks on the compiler.
  62. * @param {Compiler} compiler the compiler instance
  63. * @returns {void}
  64. */
  65. apply(compiler) {
  66. const hints = this.hints;
  67. if (!hints) return;
  68. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  69. // After seal: a module that was hoisted is gone from `modules` by now,
  70. // absorbed into the concatenation that took it.
  71. compilation.hooks.afterSeal.tap(PLUGIN_NAME, () => {
  72. const { chunkGraph, moduleGraph, requestShortener } = compilation;
  73. /** @type {Map<string, { size: number, name: string }[]>} */
  74. const byReason = new Map();
  75. /** @type {Set<Module>} */
  76. const affected = new Set();
  77. for (const module of compilation.modules) {
  78. // One webpack left out was never a candidate.
  79. if (chunkGraph.getNumberOfModuleChunks(module) === 0) continue;
  80. const reasons = getOwnReasons(
  81. moduleGraph.getOptimizationBailout(module),
  82. requestShortener
  83. );
  84. if (reasons.length === 0) continue;
  85. affected.add(module);
  86. const entry = {
  87. name: module.readableIdentifier(requestShortener),
  88. size: getModuleSize(module)
  89. };
  90. for (const reason of new Set(reasons)) {
  91. const modules = byReason.get(reason);
  92. if (modules === undefined) {
  93. byReason.set(reason, [entry]);
  94. } else {
  95. modules.push(entry);
  96. }
  97. }
  98. }
  99. if (affected.size === 0) return;
  100. /** @type {ScopeHoistingBailoutGroup[]} */
  101. const groups = [];
  102. for (const [reason, modules] of byReason) {
  103. // Ties break by name: module order is not stable across runtimes.
  104. modules.sort(
  105. (a, b) => b.size - a.size || compareStrings(a.name, b.name)
  106. );
  107. groups.push({
  108. reason,
  109. count: modules.length,
  110. names: modules.slice(0, MAX_REPORTED_NAMES).map((m) => m.name)
  111. });
  112. }
  113. groups.sort(
  114. (a, b) => b.count - a.count || compareStrings(a.reason, b.reason)
  115. );
  116. const warning = new ScopeHoistingBailoutsWarning(
  117. groups.slice(0, MAX_REPORTED_REASONS),
  118. affected.size
  119. );
  120. if (hints === "error") {
  121. compilation.errors.push(warning);
  122. } else if (hints === "stats") {
  123. compilation.hints.push(warning);
  124. } else {
  125. compilation.warnings.push(warning);
  126. }
  127. });
  128. });
  129. }
  130. }
  131. module.exports = ScopeHoistingBailoutsPlugin;