You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.6 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
  7. const HarmonyInitDependency = require("./HarmonyInitDependency");
  8. const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
  9. const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
  10. const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
  11. const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
  12. const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
  13. const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
  14. const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
  15. const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
  16. const NullFactory = require("../NullFactory");
  17. const HarmonyDetectionParserPlugin = require("./HarmonyDetectionParserPlugin");
  18. const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyParserPlugin");
  19. const HarmonyExportDependencyParserPlugin = require("./HarmonyExportDependencyParserPlugin");
  20. const HarmonyTopLevelThisParserPlugin = require("./HarmonyTopLevelThisParserPlugin");
  21. class HarmonyModulesPlugin {
  22. constructor(options) {
  23. this.options = options;
  24. }
  25. apply(compiler) {
  26. compiler.hooks.compilation.tap(
  27. "HarmonyModulesPlugin",
  28. (compilation, { normalModuleFactory }) => {
  29. compilation.dependencyFactories.set(
  30. HarmonyCompatibilityDependency,
  31. new NullFactory()
  32. );
  33. compilation.dependencyTemplates.set(
  34. HarmonyCompatibilityDependency,
  35. new HarmonyCompatibilityDependency.Template()
  36. );
  37. compilation.dependencyFactories.set(
  38. HarmonyInitDependency,
  39. new NullFactory()
  40. );
  41. compilation.dependencyTemplates.set(
  42. HarmonyInitDependency,
  43. new HarmonyInitDependency.Template()
  44. );
  45. compilation.dependencyFactories.set(
  46. HarmonyImportSideEffectDependency,
  47. normalModuleFactory
  48. );
  49. compilation.dependencyTemplates.set(
  50. HarmonyImportSideEffectDependency,
  51. new HarmonyImportSideEffectDependency.Template()
  52. );
  53. compilation.dependencyFactories.set(
  54. HarmonyImportSpecifierDependency,
  55. normalModuleFactory
  56. );
  57. compilation.dependencyTemplates.set(
  58. HarmonyImportSpecifierDependency,
  59. new HarmonyImportSpecifierDependency.Template()
  60. );
  61. compilation.dependencyFactories.set(
  62. HarmonyExportHeaderDependency,
  63. new NullFactory()
  64. );
  65. compilation.dependencyTemplates.set(
  66. HarmonyExportHeaderDependency,
  67. new HarmonyExportHeaderDependency.Template()
  68. );
  69. compilation.dependencyFactories.set(
  70. HarmonyExportExpressionDependency,
  71. new NullFactory()
  72. );
  73. compilation.dependencyTemplates.set(
  74. HarmonyExportExpressionDependency,
  75. new HarmonyExportExpressionDependency.Template()
  76. );
  77. compilation.dependencyFactories.set(
  78. HarmonyExportSpecifierDependency,
  79. new NullFactory()
  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.dependencyFactories.set(
  94. HarmonyAcceptDependency,
  95. new NullFactory()
  96. );
  97. compilation.dependencyTemplates.set(
  98. HarmonyAcceptDependency,
  99. new HarmonyAcceptDependency.Template()
  100. );
  101. compilation.dependencyFactories.set(
  102. HarmonyAcceptImportDependency,
  103. normalModuleFactory
  104. );
  105. compilation.dependencyTemplates.set(
  106. HarmonyAcceptImportDependency,
  107. new HarmonyAcceptImportDependency.Template()
  108. );
  109. const handler = (parser, parserOptions) => {
  110. if (parserOptions.harmony !== undefined && !parserOptions.harmony)
  111. return;
  112. new HarmonyDetectionParserPlugin().apply(parser);
  113. new HarmonyImportDependencyParserPlugin(this.options).apply(parser);
  114. new HarmonyExportDependencyParserPlugin(this.options).apply(parser);
  115. new HarmonyTopLevelThisParserPlugin().apply(parser);
  116. };
  117. normalModuleFactory.hooks.parser
  118. .for("javascript/auto")
  119. .tap("HarmonyModulesPlugin", handler);
  120. normalModuleFactory.hooks.parser
  121. .for("javascript/esm")
  122. .tap("HarmonyModulesPlugin", handler);
  123. }
  124. );
  125. }
  126. }
  127. module.exports = HarmonyModulesPlugin;