EvalUsagePlugin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const {
  7. JAVASCRIPT_MODULE_TYPE_AUTO,
  8. JAVASCRIPT_MODULE_TYPE_DYNAMIC,
  9. JAVASCRIPT_MODULE_TYPE_ESM
  10. } = require("../ModuleTypeConstants");
  11. const EvalUsageWarning = require("../errors/EvalUsageWarning");
  12. const { compareStrings } = require("../util/comparators");
  13. /** @import { PerformanceOptions } from "../../declarations/WebpackOptions" */
  14. /** @import Compiler from "../Compiler" */
  15. /** @import { BuildInfo } from "../Module" */
  16. /** @import JavascriptParser from "../javascript/JavascriptParser" */
  17. const PLUGIN_NAME = "EvalUsagePlugin";
  18. // Enough to name the offenders without listing the graph.
  19. const MAX_REPORTED_MODULES = 5;
  20. class EvalUsagePlugin {
  21. /**
  22. * Creates an instance of EvalUsagePlugin.
  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. compiler.hooks.compilation.tap(
  38. PLUGIN_NAME,
  39. (compilation, { normalModuleFactory }) => {
  40. /**
  41. * @param {JavascriptParser} parser the parser
  42. */
  43. const handler = (parser) => {
  44. // Only a call of the name itself is a direct eval; `(0, eval)()` and
  45. // `globalThis.eval()` run in global scope and reach no local name.
  46. parser.hooks.call.for("eval").tap(PLUGIN_NAME, () => {
  47. // Kept on the module rather than in a map here, so a module the
  48. // filesystem cache restores rather than parses still reports.
  49. const buildInfo = /** @type {BuildInfo} */ (
  50. parser.state.module.buildInfo
  51. );
  52. buildInfo.usesEval = true;
  53. });
  54. };
  55. normalModuleFactory.hooks.parser
  56. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  57. .tap(PLUGIN_NAME, handler);
  58. normalModuleFactory.hooks.parser
  59. .for(JAVASCRIPT_MODULE_TYPE_ESM)
  60. .tap(PLUGIN_NAME, handler);
  61. normalModuleFactory.hooks.parser
  62. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  63. .tap(PLUGIN_NAME, handler);
  64. // `afterSeal` is past the hash, which folds every message into it — a
  65. // hint reported earlier would change the build's identity.
  66. compilation.hooks.afterSeal.tap(PLUGIN_NAME, () => {
  67. const { requestShortener } = compilation;
  68. /** @type {string[]} */
  69. const callers = [];
  70. for (const module of compilation.modules) {
  71. // Every built module carries one, so only the field is in question.
  72. const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
  73. if (!buildInfo.usesEval) continue;
  74. callers.push(module.readableIdentifier(requestShortener));
  75. }
  76. if (callers.length === 0) return;
  77. // Sorted by name: module order is not stable across runs.
  78. callers.sort(compareStrings);
  79. const warning = new EvalUsageWarning(
  80. callers.slice(0, MAX_REPORTED_MODULES),
  81. callers.length
  82. );
  83. if (hints === "error") {
  84. compilation.errors.push(warning);
  85. } else if (hints === "stats") {
  86. compilation.hints.push(warning);
  87. } else {
  88. compilation.warnings.push(warning);
  89. }
  90. });
  91. }
  92. );
  93. }
  94. }
  95. module.exports = EvalUsagePlugin;