HarmonyCompatibilityDependency.js 2.8 KB

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