HarmonyModulesPlugin.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 HarmonyDetectionParserPlugin = require("./HarmonyDetectionParserPlugin");
  17. const HarmonyExportDependencyParserPlugin = require("./HarmonyExportDependencyParserPlugin");
  18. const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyParserPlugin");
  19. const HarmonyTopLevelThisParserPlugin = require("./HarmonyTopLevelThisParserPlugin");
  20. /** @typedef {import("../Compiler")} Compiler */
  21. class HarmonyModulesPlugin {
  22. constructor(options) {
  23. this.options = options;
  24. }
  25. /**
  26. * Apply the plugin
  27. * @param {Compiler} compiler the compiler instance
  28. * @returns {void}
  29. */
  30. apply(compiler) {
  31. compiler.hooks.compilation.tap(
  32. "HarmonyModulesPlugin",
  33. (compilation, { normalModuleFactory }) => {
  34. compilation.dependencyTemplates.set(
  35. HarmonyCompatibilityDependency,
  36. new HarmonyCompatibilityDependency.Template()
  37. );
  38. compilation.dependencyFactories.set(
  39. HarmonyImportSideEffectDependency,
  40. normalModuleFactory
  41. );
  42. compilation.dependencyTemplates.set(
  43. HarmonyImportSideEffectDependency,
  44. new HarmonyImportSideEffectDependency.Template()
  45. );
  46. compilation.dependencyFactories.set(
  47. HarmonyImportSpecifierDependency,
  48. normalModuleFactory
  49. );
  50. compilation.dependencyTemplates.set(
  51. HarmonyImportSpecifierDependency,
  52. new HarmonyImportSpecifierDependency.Template()
  53. );
  54. compilation.dependencyFactories.set(
  55. HarmonyEvaluatedImportSpecifierDependency,
  56. normalModuleFactory
  57. );
  58. compilation.dependencyTemplates.set(
  59. HarmonyEvaluatedImportSpecifierDependency,
  60. new HarmonyEvaluatedImportSpecifierDependency.Template()
  61. );
  62. compilation.dependencyTemplates.set(
  63. HarmonyExportHeaderDependency,
  64. new HarmonyExportHeaderDependency.Template()
  65. );
  66. compilation.dependencyTemplates.set(
  67. HarmonyExportExpressionDependency,
  68. new HarmonyExportExpressionDependency.Template()
  69. );
  70. compilation.dependencyTemplates.set(
  71. HarmonyExportSpecifierDependency,
  72. new HarmonyExportSpecifierDependency.Template()
  73. );
  74. compilation.dependencyFactories.set(
  75. HarmonyExportImportedSpecifierDependency,
  76. normalModuleFactory
  77. );
  78. compilation.dependencyTemplates.set(
  79. HarmonyExportImportedSpecifierDependency,
  80. new HarmonyExportImportedSpecifierDependency.Template()
  81. );
  82. compilation.dependencyTemplates.set(
  83. HarmonyAcceptDependency,
  84. new HarmonyAcceptDependency.Template()
  85. );
  86. compilation.dependencyFactories.set(
  87. HarmonyAcceptImportDependency,
  88. normalModuleFactory
  89. );
  90. compilation.dependencyTemplates.set(
  91. HarmonyAcceptImportDependency,
  92. new HarmonyAcceptImportDependency.Template()
  93. );
  94. const handler = (parser, parserOptions) => {
  95. // TODO webpack 6: rename harmony to esm or module
  96. if (parserOptions.harmony !== undefined && !parserOptions.harmony)
  97. return;
  98. new HarmonyDetectionParserPlugin(this.options).apply(parser);
  99. new HarmonyImportDependencyParserPlugin(parserOptions).apply(parser);
  100. new HarmonyExportDependencyParserPlugin(parserOptions).apply(parser);
  101. new HarmonyTopLevelThisParserPlugin().apply(parser);
  102. };
  103. normalModuleFactory.hooks.parser
  104. .for("javascript/auto")
  105. .tap("HarmonyModulesPlugin", handler);
  106. normalModuleFactory.hooks.parser
  107. .for("javascript/esm")
  108. .tap("HarmonyModulesPlugin", handler);
  109. }
  110. );
  111. }
  112. }
  113. module.exports = HarmonyModulesPlugin;