HarmonyExportSpecifierDependency.js 3.0 KB

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