HarmonyExportSpecifierDependency.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
  8. const NullDependency = require("./NullDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("./HarmonyExportInitFragment").UnusedExports} UnusedExports */
  18. /** @typedef {import("./HarmonyExportInitFragment").ExportMap} ExportMap */
  19. class HarmonyExportSpecifierDependency extends NullDependency {
  20. /**
  21. * Creates an instance of HarmonyExportSpecifierDependency.
  22. * @param {string} id the id
  23. * @param {string} name the name
  24. */
  25. constructor(id, name) {
  26. super();
  27. this.id = id;
  28. this.name = name;
  29. }
  30. get type() {
  31. return "harmony export specifier";
  32. }
  33. /**
  34. * Returns the exported names
  35. * @param {ModuleGraph} moduleGraph module graph
  36. * @returns {ExportsSpec | undefined} export names
  37. */
  38. getExports(moduleGraph) {
  39. return {
  40. exports: [this.name],
  41. priority: 1,
  42. terminalBinding: true,
  43. dependencies: undefined
  44. };
  45. }
  46. /**
  47. * Gets module evaluation side effects state.
  48. * @param {ModuleGraph} moduleGraph the module graph
  49. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  50. */
  51. getModuleEvaluationSideEffectsState(moduleGraph) {
  52. return false;
  53. }
  54. /**
  55. * Serializes this instance into the provided serializer context.
  56. * @param {ObjectSerializerContext} context context
  57. */
  58. serialize(context) {
  59. const { write } = context;
  60. write(this.id);
  61. write(this.name);
  62. super.serialize(context);
  63. }
  64. /**
  65. * Restores this instance from the provided deserializer context.
  66. * @param {ObjectDeserializerContext} context context
  67. */
  68. deserialize(context) {
  69. const { read } = context;
  70. this.id = read();
  71. this.name = read();
  72. super.deserialize(context);
  73. }
  74. }
  75. makeSerializable(
  76. HarmonyExportSpecifierDependency,
  77. "webpack/lib/dependencies/HarmonyExportSpecifierDependency"
  78. );
  79. HarmonyExportSpecifierDependency.Template = class HarmonyExportSpecifierDependencyTemplate extends (
  80. NullDependency.Template
  81. ) {
  82. /**
  83. * Applies the plugin by registering its hooks on the compiler.
  84. * @param {Dependency} dependency the dependency for which the template should be applied
  85. * @param {ReplaceSource} source the current replace source which can be modified
  86. * @param {DependencyTemplateContext} templateContext the context object
  87. * @returns {void}
  88. */
  89. apply(
  90. dependency,
  91. source,
  92. { module, moduleGraph, initFragments, runtime, concatenationScope }
  93. ) {
  94. const dep = /** @type {HarmonyExportSpecifierDependency} */ (dependency);
  95. if (concatenationScope) {
  96. concatenationScope.registerExport(dep.name, dep.id);
  97. return;
  98. }
  99. const used = moduleGraph
  100. .getExportsInfo(module)
  101. .getUsedName(dep.name, runtime);
  102. if (!used) {
  103. /** @type {UnusedExports} */
  104. const set = new Set();
  105. set.add(dep.name || "namespace");
  106. initFragments.push(
  107. new HarmonyExportInitFragment(module.exportsArgument, undefined, set)
  108. );
  109. return;
  110. }
  111. /** @type {ExportMap} */
  112. const map = new Map();
  113. map.set(used, `/* binding */ ${dep.id}`);
  114. initFragments.push(
  115. new HarmonyExportInitFragment(module.exportsArgument, map, undefined)
  116. );
  117. }
  118. };
  119. module.exports = HarmonyExportSpecifierDependency;