HarmonyDetectionParserPlugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const EnvironmentNotSupportAsyncWarning = require("../EnvironmentNotSupportAsyncWarning");
  7. const { JAVASCRIPT_MODULE_TYPE_ESM } = require("../ModuleTypeConstants");
  8. const DynamicExports = require("./DynamicExports");
  9. const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
  10. const HarmonyExports = require("./HarmonyExports");
  11. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  12. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  13. /** @typedef {import("./HarmonyModulesPlugin").HarmonyModulesPluginOptions} HarmonyModulesPluginOptions */
  14. module.exports = class HarmonyDetectionParserPlugin {
  15. /**
  16. * @param {HarmonyModulesPluginOptions} options options
  17. */
  18. constructor(options) {
  19. const { topLevelAwait = false } = options || {};
  20. this.topLevelAwait = topLevelAwait;
  21. }
  22. /**
  23. * @param {JavascriptParser} parser the parser
  24. * @returns {void}
  25. */
  26. apply(parser) {
  27. parser.hooks.program.tap("HarmonyDetectionParserPlugin", ast => {
  28. const isStrictHarmony =
  29. parser.state.module.type === JAVASCRIPT_MODULE_TYPE_ESM;
  30. const isHarmony =
  31. isStrictHarmony ||
  32. ast.body.some(
  33. statement =>
  34. statement.type === "ImportDeclaration" ||
  35. statement.type === "ExportDefaultDeclaration" ||
  36. statement.type === "ExportNamedDeclaration" ||
  37. statement.type === "ExportAllDeclaration"
  38. );
  39. if (isHarmony) {
  40. const module = parser.state.module;
  41. const compatDep = new HarmonyCompatibilityDependency();
  42. compatDep.loc = {
  43. start: {
  44. line: -1,
  45. column: 0
  46. },
  47. end: {
  48. line: -1,
  49. column: 0
  50. },
  51. index: -3
  52. };
  53. module.addPresentationalDependency(compatDep);
  54. DynamicExports.bailout(parser.state);
  55. HarmonyExports.enable(parser.state, isStrictHarmony);
  56. parser.scope.isStrict = true;
  57. }
  58. });
  59. parser.hooks.topLevelAwait.tap("HarmonyDetectionParserPlugin", () => {
  60. const module = parser.state.module;
  61. if (!this.topLevelAwait) {
  62. throw new Error(
  63. "The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enable it)"
  64. );
  65. }
  66. if (!HarmonyExports.isEnabled(parser.state)) {
  67. throw new Error(
  68. "Top-level-await is only supported in EcmaScript Modules"
  69. );
  70. }
  71. /** @type {BuildMeta} */
  72. (module.buildMeta).async = true;
  73. EnvironmentNotSupportAsyncWarning.check(
  74. module,
  75. parser.state.compilation.runtimeTemplate,
  76. "topLevelAwait"
  77. );
  78. });
  79. /**
  80. * @returns {boolean | undefined} true if in harmony
  81. */
  82. const skipInHarmony = () => {
  83. if (HarmonyExports.isEnabled(parser.state)) {
  84. return true;
  85. }
  86. };
  87. /**
  88. * @returns {null | undefined} null if in harmony
  89. */
  90. const nullInHarmony = () => {
  91. if (HarmonyExports.isEnabled(parser.state)) {
  92. return null;
  93. }
  94. };
  95. const nonHarmonyIdentifiers = ["define", "exports"];
  96. for (const identifier of nonHarmonyIdentifiers) {
  97. parser.hooks.evaluateTypeof
  98. .for(identifier)
  99. .tap("HarmonyDetectionParserPlugin", nullInHarmony);
  100. parser.hooks.typeof
  101. .for(identifier)
  102. .tap("HarmonyDetectionParserPlugin", skipInHarmony);
  103. parser.hooks.evaluate
  104. .for(identifier)
  105. .tap("HarmonyDetectionParserPlugin", nullInHarmony);
  106. parser.hooks.expression
  107. .for(identifier)
  108. .tap("HarmonyDetectionParserPlugin", skipInHarmony);
  109. parser.hooks.call
  110. .for(identifier)
  111. .tap("HarmonyDetectionParserPlugin", skipInHarmony);
  112. }
  113. }
  114. };