HarmonyModulesPlugin.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
  7. const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
  8. const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
  9. const HarmonyEvaluatedImportSpecifierDependency = require("./HarmonyEvaluatedImportSpecifierDependency");
  10. const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
  11. const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
  12. const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
  13. const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
  14. const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
  15. const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
  16. const {
  17. JAVASCRIPT_MODULE_TYPE_AUTO,
  18. JAVASCRIPT_MODULE_TYPE_ESM
  19. } = require("../ModuleTypeConstants");
  20. const HarmonyDetectionParserPlugin = require("./HarmonyDetectionParserPlugin");
  21. const HarmonyExportDependencyParserPlugin = require("./HarmonyExportDependencyParserPlugin");
  22. const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyParserPlugin");
  23. const HarmonyTopLevelThisParserPlugin = require("./HarmonyTopLevelThisParserPlugin");
  24. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  25. /** @typedef {import("../Compiler")} Compiler */
  26. /** @typedef {import("../javascript/JavascriptParser")} Parser */
  27. const PLUGIN_NAME = "HarmonyModulesPlugin";
  28. /** @typedef {{ topLevelAwait?: boolean }} HarmonyModulesPluginOptions */
  29. class HarmonyModulesPlugin {
  30. /**
  31. * @param {HarmonyModulesPluginOptions} options options
  32. */
  33. constructor(options) {
  34. this.options = options;
  35. }
  36. /**
  37. * Apply the plugin
  38. * @param {Compiler} compiler the compiler instance
  39. * @returns {void}
  40. */
  41. apply(compiler) {
  42. compiler.hooks.compilation.tap(
  43. PLUGIN_NAME,
  44. (compilation, { normalModuleFactory }) => {
  45. compilation.dependencyTemplates.set(
  46. HarmonyCompatibilityDependency,
  47. new HarmonyCompatibilityDependency.Template()
  48. );
  49. compilation.dependencyFactories.set(
  50. HarmonyImportSideEffectDependency,
  51. normalModuleFactory
  52. );
  53. compilation.dependencyTemplates.set(
  54. HarmonyImportSideEffectDependency,
  55. new HarmonyImportSideEffectDependency.Template()
  56. );
  57. compilation.dependencyFactories.set(
  58. HarmonyImportSpecifierDependency,
  59. normalModuleFactory
  60. );
  61. compilation.dependencyTemplates.set(
  62. HarmonyImportSpecifierDependency,
  63. new HarmonyImportSpecifierDependency.Template()
  64. );
  65. compilation.dependencyFactories.set(
  66. HarmonyEvaluatedImportSpecifierDependency,
  67. normalModuleFactory
  68. );
  69. compilation.dependencyTemplates.set(
  70. HarmonyEvaluatedImportSpecifierDependency,
  71. new HarmonyEvaluatedImportSpecifierDependency.Template()
  72. );
  73. compilation.dependencyTemplates.set(
  74. HarmonyExportHeaderDependency,
  75. new HarmonyExportHeaderDependency.Template()
  76. );
  77. compilation.dependencyTemplates.set(
  78. HarmonyExportExpressionDependency,
  79. new HarmonyExportExpressionDependency.Template()
  80. );
  81. compilation.dependencyTemplates.set(
  82. HarmonyExportSpecifierDependency,
  83. new HarmonyExportSpecifierDependency.Template()
  84. );
  85. compilation.dependencyFactories.set(
  86. HarmonyExportImportedSpecifierDependency,
  87. normalModuleFactory
  88. );
  89. compilation.dependencyTemplates.set(
  90. HarmonyExportImportedSpecifierDependency,
  91. new HarmonyExportImportedSpecifierDependency.Template()
  92. );
  93. compilation.dependencyTemplates.set(
  94. HarmonyAcceptDependency,
  95. new HarmonyAcceptDependency.Template()
  96. );
  97. compilation.dependencyFactories.set(
  98. HarmonyAcceptImportDependency,
  99. normalModuleFactory
  100. );
  101. compilation.dependencyTemplates.set(
  102. HarmonyAcceptImportDependency,
  103. new HarmonyAcceptImportDependency.Template()
  104. );
  105. /**
  106. * @param {Parser} parser parser parser
  107. * @param {JavascriptParserOptions} parserOptions parserOptions
  108. * @returns {void}
  109. */
  110. const handler = (parser, parserOptions) => {
  111. // TODO webpack 6: rename harmony to esm or module
  112. if (parserOptions.harmony !== undefined && !parserOptions.harmony)
  113. return;
  114. new HarmonyDetectionParserPlugin(this.options).apply(parser);
  115. new HarmonyImportDependencyParserPlugin(parserOptions).apply(parser);
  116. new HarmonyExportDependencyParserPlugin(parserOptions).apply(parser);
  117. new HarmonyTopLevelThisParserPlugin().apply(parser);
  118. };
  119. normalModuleFactory.hooks.parser
  120. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  121. .tap(PLUGIN_NAME, handler);
  122. normalModuleFactory.hooks.parser
  123. .for(JAVASCRIPT_MODULE_TYPE_ESM)
  124. .tap(PLUGIN_NAME, handler);
  125. }
  126. );
  127. }
  128. }
  129. module.exports = HarmonyModulesPlugin;