JavascriptMetaInfoPlugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sergey Melyukov @smelukov
  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 { getInnerGraphUtils } = require("./optimize/InnerGraph");
  12. /** @import Compiler from "./Compiler" */
  13. /** @import { BuildInfo } from "./Module" */
  14. /**
  15. * @import {
  16. * JavascriptModuleBuildInfo
  17. * } from "./javascript/JavascriptModule"
  18. */
  19. /** @import JavascriptParser from "./javascript/JavascriptParser" */
  20. const PLUGIN_NAME = "JavascriptMetaInfoPlugin";
  21. class JavascriptMetaInfoPlugin {
  22. /**
  23. * Applies the plugin by registering its hooks on the compiler.
  24. * @param {Compiler} compiler the compiler instance
  25. * @returns {void}
  26. */
  27. apply(compiler) {
  28. compiler.hooks.compilation.tap(
  29. PLUGIN_NAME,
  30. (compilation, { normalModuleFactory }) => {
  31. const innerGraph = getInnerGraphUtils(compilation);
  32. /**
  33. * Handles the hook callback for this code path.
  34. * @param {JavascriptParser} parser the parser
  35. * @returns {void}
  36. */
  37. const handler = (parser) => {
  38. parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
  39. // Nested blocks keep their own disposal scope, only a module-scope
  40. // declaration ties disposal to the end of module evaluation.
  41. if (
  42. ast.body.some(
  43. (statement) =>
  44. statement.type === "VariableDeclaration" &&
  45. (statement.kind === "using" ||
  46. statement.kind === "await using")
  47. )
  48. ) {
  49. const buildInfo =
  50. /** @type {JavascriptModuleBuildInfo} */
  51. (parser.state.module.buildInfo);
  52. buildInfo.usesTopLevelUsingDeclaration = true;
  53. // Concatenation would defer disposal to the enclosing scope.
  54. buildInfo.moduleConcatenationBailout =
  55. "a top-level using declaration";
  56. }
  57. });
  58. parser.hooks.call.for("eval").tap(PLUGIN_NAME, () => {
  59. const buildInfo =
  60. /** @type {JavascriptModuleBuildInfo} */
  61. (parser.state.module.buildInfo);
  62. buildInfo.moduleConcatenationBailout = "eval()";
  63. const currentSymbol = innerGraph.getTopLevelSymbol(parser.state);
  64. if (currentSymbol) {
  65. innerGraph.addUsage(parser.state, null, currentSymbol);
  66. } else {
  67. // Only worth reporting when the analysis was actually running
  68. const wasEnabled = innerGraph.isEnabled(parser.state);
  69. innerGraph.bailout(parser.state);
  70. if (wasEnabled) {
  71. compilation.moduleGraph
  72. .getOptimizationBailout(parser.state.module)
  73. .push("Inner graph bailout: eval()");
  74. }
  75. }
  76. });
  77. parser.hooks.finish.tap(PLUGIN_NAME, () => {
  78. const buildInfo =
  79. /** @type {BuildInfo} */
  80. (parser.state.module.buildInfo);
  81. let topLevelDeclarations = buildInfo.topLevelDeclarations;
  82. if (topLevelDeclarations === undefined) {
  83. topLevelDeclarations = buildInfo.topLevelDeclarations = new Set();
  84. }
  85. for (const name of parser.scope.definitions.asSet()) {
  86. if (parser.isVariableDefined(name)) {
  87. topLevelDeclarations.add(name);
  88. }
  89. }
  90. });
  91. };
  92. normalModuleFactory.hooks.parser
  93. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  94. .tap(PLUGIN_NAME, handler);
  95. normalModuleFactory.hooks.parser
  96. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  97. .tap(PLUGIN_NAME, handler);
  98. normalModuleFactory.hooks.parser
  99. .for(JAVASCRIPT_MODULE_TYPE_ESM)
  100. .tap(PLUGIN_NAME, handler);
  101. }
  102. );
  103. }
  104. }
  105. module.exports = JavascriptMetaInfoPlugin;