HarmonyExportSpecifierDependency.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. class HarmonyExportSpecifierDependency extends NullDependency {
  18. /**
  19. * @param {TODO} id id
  20. * @param {TODO} name name
  21. */
  22. constructor(id, name) {
  23. super();
  24. this.id = id;
  25. this.name = name;
  26. }
  27. get type() {
  28. return "harmony export specifier";
  29. }
  30. /**
  31. * Returns the exported names
  32. * @param {ModuleGraph} moduleGraph module graph
  33. * @returns {ExportsSpec | undefined} export names
  34. */
  35. getExports(moduleGraph) {
  36. return {
  37. exports: [this.name],
  38. priority: 1,
  39. terminalBinding: true,
  40. dependencies: undefined
  41. };
  42. }
  43. /**
  44. * @param {ModuleGraph} moduleGraph the module graph
  45. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  46. */
  47. getModuleEvaluationSideEffectsState(moduleGraph) {
  48. return false;
  49. }
  50. /**
  51. * @param {ObjectSerializerContext} context context
  52. */
  53. serialize(context) {
  54. const { write } = context;
  55. write(this.id);
  56. write(this.name);
  57. super.serialize(context);
  58. }
  59. /**
  60. * @param {ObjectDeserializerContext} context context
  61. */
  62. deserialize(context) {
  63. const { read } = context;
  64. this.id = read();
  65. this.name = read();
  66. super.deserialize(context);
  67. }
  68. }
  69. makeSerializable(
  70. HarmonyExportSpecifierDependency,
  71. "webpack/lib/dependencies/HarmonyExportSpecifierDependency"
  72. );
  73. HarmonyExportSpecifierDependency.Template = class HarmonyExportSpecifierDependencyTemplate extends (
  74. NullDependency.Template
  75. ) {
  76. /**
  77. * @param {Dependency} dependency the dependency for which the template should be applied
  78. * @param {ReplaceSource} source the current replace source which can be modified
  79. * @param {DependencyTemplateContext} templateContext the context object
  80. * @returns {void}
  81. */
  82. apply(
  83. dependency,
  84. source,
  85. { module, moduleGraph, initFragments, runtime, concatenationScope }
  86. ) {
  87. const dep = /** @type {HarmonyExportSpecifierDependency} */ (dependency);
  88. if (concatenationScope) {
  89. concatenationScope.registerExport(dep.name, dep.id);
  90. return;
  91. }
  92. const used = moduleGraph
  93. .getExportsInfo(module)
  94. .getUsedName(dep.name, runtime);
  95. if (!used) {
  96. const set = new Set();
  97. set.add(dep.name || "namespace");
  98. initFragments.push(
  99. new HarmonyExportInitFragment(module.exportsArgument, undefined, set)
  100. );
  101. return;
  102. }
  103. const map = new Map();
  104. map.set(used, `/* binding */ ${dep.id}`);
  105. initFragments.push(
  106. new HarmonyExportInitFragment(module.exportsArgument, map, undefined)
  107. );
  108. }
  109. };
  110. module.exports = HarmonyExportSpecifierDependency;