ProvidedDependency.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Florent Cailhol @ooflorent
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const InitFragment = require("../InitFragment");
  8. const makeSerializable = require("../util/makeSerializable");
  9. const ModuleDependency = require("./ModuleDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  12. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  13. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  15. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  16. /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
  17. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  20. /** @typedef {import("../util/Hash")} Hash */
  21. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  22. /**
  23. * @param {string[] | null} path the property path array
  24. * @returns {string} the converted path
  25. */
  26. const pathToString = (path) =>
  27. path !== null && path.length > 0
  28. ? path.map((part) => `[${JSON.stringify(part)}]`).join("")
  29. : "";
  30. class ProvidedDependency extends ModuleDependency {
  31. /**
  32. * @param {string} request request
  33. * @param {string} identifier identifier
  34. * @param {ExportInfoName[]} ids ids
  35. * @param {Range} range range
  36. */
  37. constructor(request, identifier, ids, range) {
  38. super(request);
  39. this.identifier = identifier;
  40. this.ids = ids;
  41. this.range = range;
  42. /** @type {undefined | string} */
  43. this._hashUpdate = undefined;
  44. }
  45. get type() {
  46. return "provided";
  47. }
  48. get category() {
  49. return "esm";
  50. }
  51. /**
  52. * Returns list of exports referenced by this dependency
  53. * @param {ModuleGraph} moduleGraph module graph
  54. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  55. * @returns {ReferencedExports} referenced exports
  56. */
  57. getReferencedExports(moduleGraph, runtime) {
  58. const ids = this.ids;
  59. if (ids.length === 0) return Dependency.EXPORTS_OBJECT_REFERENCED;
  60. return [ids];
  61. }
  62. /**
  63. * Update the hash
  64. * @param {Hash} hash hash to be updated
  65. * @param {UpdateHashContext} context context
  66. * @returns {void}
  67. */
  68. updateHash(hash, context) {
  69. if (this._hashUpdate === undefined) {
  70. this._hashUpdate = this.identifier + (this.ids ? this.ids.join(",") : "");
  71. }
  72. hash.update(this._hashUpdate);
  73. }
  74. /**
  75. * @param {ObjectSerializerContext} context context
  76. */
  77. serialize(context) {
  78. const { write } = context;
  79. write(this.identifier);
  80. write(this.ids);
  81. super.serialize(context);
  82. }
  83. /**
  84. * @param {ObjectDeserializerContext} context context
  85. */
  86. deserialize(context) {
  87. const { read } = context;
  88. this.identifier = read();
  89. this.ids = read();
  90. super.deserialize(context);
  91. }
  92. }
  93. makeSerializable(
  94. ProvidedDependency,
  95. "webpack/lib/dependencies/ProvidedDependency"
  96. );
  97. class ProvidedDependencyTemplate extends ModuleDependency.Template {
  98. /**
  99. * @param {Dependency} dependency the dependency for which the template should be applied
  100. * @param {ReplaceSource} source the current replace source which can be modified
  101. * @param {DependencyTemplateContext} templateContext the context object
  102. * @returns {void}
  103. */
  104. apply(
  105. dependency,
  106. source,
  107. {
  108. runtime,
  109. runtimeTemplate,
  110. moduleGraph,
  111. chunkGraph,
  112. initFragments,
  113. runtimeRequirements
  114. }
  115. ) {
  116. const dep = /** @type {ProvidedDependency} */ (dependency);
  117. const connection =
  118. /** @type {ModuleGraphConnection} */
  119. (moduleGraph.getConnection(dep));
  120. const exportsInfo = moduleGraph.getExportsInfo(connection.module);
  121. const usedName = exportsInfo.getUsedName(dep.ids, runtime);
  122. initFragments.push(
  123. new InitFragment(
  124. `/* provided dependency */ var ${
  125. dep.identifier
  126. } = ${runtimeTemplate.moduleExports({
  127. module: moduleGraph.getModule(dep),
  128. chunkGraph,
  129. request: dep.request,
  130. runtimeRequirements
  131. })}${pathToString(/** @type {string[] | null} */ (usedName))};\n`,
  132. InitFragment.STAGE_PROVIDES,
  133. 1,
  134. `provided ${dep.identifier}`
  135. )
  136. );
  137. source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
  138. }
  139. }
  140. ProvidedDependency.Template = ProvidedDependencyTemplate;
  141. module.exports = ProvidedDependency;