JavascriptMetaInfoPlugin.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sergey Melyukov @smelukov
  4. */
  5. "use strict";
  6. const InnerGraph = require("./optimize/InnerGraph");
  7. /** @typedef {import("./Compiler")} Compiler */
  8. /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
  9. class JavascriptMetaInfoPlugin {
  10. /**
  11. * Apply the plugin
  12. * @param {Compiler} compiler the compiler instance
  13. * @returns {void}
  14. */
  15. apply(compiler) {
  16. compiler.hooks.compilation.tap(
  17. "JavascriptMetaInfoPlugin",
  18. (compilation, { normalModuleFactory }) => {
  19. /**
  20. * @param {JavascriptParser} parser the parser
  21. * @returns {void}
  22. */
  23. const handler = parser => {
  24. parser.hooks.call.for("eval").tap("JavascriptMetaInfoPlugin", () => {
  25. parser.state.module.buildInfo.moduleConcatenationBailout = "eval()";
  26. parser.state.module.buildInfo.usingEval = true;
  27. const currentSymbol = InnerGraph.getTopLevelSymbol(parser.state);
  28. if (currentSymbol) {
  29. InnerGraph.addUsage(parser.state, null, currentSymbol);
  30. } else {
  31. InnerGraph.bailout(parser.state);
  32. }
  33. });
  34. parser.hooks.finish.tap("JavascriptMetaInfoPlugin", () => {
  35. let topLevelDeclarations =
  36. parser.state.module.buildInfo.topLevelDeclarations;
  37. if (topLevelDeclarations === undefined) {
  38. topLevelDeclarations =
  39. parser.state.module.buildInfo.topLevelDeclarations = new Set();
  40. }
  41. for (const name of parser.scope.definitions.asSet()) {
  42. const freeInfo = parser.getFreeInfoFromVariable(name);
  43. if (freeInfo === undefined) {
  44. topLevelDeclarations.add(name);
  45. }
  46. }
  47. });
  48. };
  49. normalModuleFactory.hooks.parser
  50. .for("javascript/auto")
  51. .tap("JavascriptMetaInfoPlugin", handler);
  52. normalModuleFactory.hooks.parser
  53. .for("javascript/dynamic")
  54. .tap("JavascriptMetaInfoPlugin", handler);
  55. normalModuleFactory.hooks.parser
  56. .for("javascript/esm")
  57. .tap("JavascriptMetaInfoPlugin", handler);
  58. }
  59. );
  60. }
  61. }
  62. module.exports = JavascriptMetaInfoPlugin;