HarmonyCompatibilityDependency.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { UsageState } = require("../ExportsInfo");
  7. const InitFragment = require("../InitFragment");
  8. const RuntimeGlobals = require("../RuntimeGlobals");
  9. const makeSerializable = require("../util/makeSerializable");
  10. const NullDependency = require("./NullDependency");
  11. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  12. /** @typedef {import("../Dependency")} Dependency */
  13. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../Module")} Module */
  15. class HarmonyCompatibilityDependency extends NullDependency {
  16. get type() {
  17. return "harmony export header";
  18. }
  19. }
  20. makeSerializable(
  21. HarmonyCompatibilityDependency,
  22. "webpack/lib/dependencies/HarmonyCompatibilityDependency"
  23. );
  24. HarmonyCompatibilityDependency.Template = class HarmonyExportDependencyTemplate extends (
  25. NullDependency.Template
  26. ) {
  27. /**
  28. * @param {Dependency} dependency the dependency for which the template should be applied
  29. * @param {ReplaceSource} source the current replace source which can be modified
  30. * @param {DependencyTemplateContext} templateContext the context object
  31. * @returns {void}
  32. */
  33. apply(
  34. dependency,
  35. source,
  36. {
  37. module,
  38. runtimeTemplate,
  39. moduleGraph,
  40. initFragments,
  41. runtimeRequirements,
  42. runtime,
  43. concatenationScope
  44. }
  45. ) {
  46. if (concatenationScope) return;
  47. const exportsInfo = moduleGraph.getExportsInfo(module);
  48. if (
  49. exportsInfo.getReadOnlyExportInfo("__esModule").getUsed(runtime) !==
  50. UsageState.Unused
  51. ) {
  52. const content = runtimeTemplate.defineEsModuleFlagStatement({
  53. exportsArgument: module.exportsArgument,
  54. runtimeRequirements
  55. });
  56. initFragments.push(
  57. new InitFragment(
  58. content,
  59. InitFragment.STAGE_HARMONY_EXPORTS,
  60. 0,
  61. "harmony compatibility"
  62. )
  63. );
  64. }
  65. if (moduleGraph.isAsync(module)) {
  66. runtimeRequirements.add(RuntimeGlobals.module);
  67. runtimeRequirements.add(RuntimeGlobals.asyncModule);
  68. initFragments.push(
  69. new InitFragment(
  70. runtimeTemplate.supportsArrowFunction()
  71. ? `${RuntimeGlobals.asyncModule}(${module.moduleArgument}, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {\n`
  72. : `${RuntimeGlobals.asyncModule}(${module.moduleArgument}, async function (__webpack_handle_async_dependencies__, __webpack_async_result__) { try {\n`,
  73. InitFragment.STAGE_ASYNC_BOUNDARY,
  74. 0,
  75. undefined,
  76. `\n__webpack_async_result__();\n} catch(e) { __webpack_async_result__(e); } }${
  77. module.buildMeta.async ? ", 1" : ""
  78. });`
  79. )
  80. );
  81. }
  82. }
  83. };
  84. module.exports = HarmonyCompatibilityDependency;