ProvidedDependency.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * Returns the converted path.
  24. * @param {string[] | null} path the property path array
  25. * @returns {string} the converted path
  26. */
  27. const pathToString = (path) =>
  28. path !== null && path.length > 0
  29. ? path.map((part) => `[${JSON.stringify(part)}]`).join("")
  30. : "";
  31. class ProvidedDependency extends ModuleDependency {
  32. /**
  33. * Creates an instance of ProvidedDependency.
  34. * @param {string} request request
  35. * @param {string} identifier identifier
  36. * @param {ExportInfoName[]} ids ids
  37. * @param {Range} range range
  38. */
  39. constructor(request, identifier, ids, range) {
  40. super(request);
  41. this.identifier = identifier;
  42. this.ids = ids;
  43. this.range = range;
  44. /** @type {undefined | string} */
  45. this._hashUpdate = undefined;
  46. }
  47. get type() {
  48. return "provided";
  49. }
  50. get category() {
  51. return "esm";
  52. }
  53. /**
  54. * Returns list of exports referenced by this dependency
  55. * @param {ModuleGraph} moduleGraph module graph
  56. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  57. * @returns {ReferencedExports} referenced exports
  58. */
  59. getReferencedExports(moduleGraph, runtime) {
  60. const ids = this.ids;
  61. if (ids.length === 0) return Dependency.EXPORTS_OBJECT_REFERENCED;
  62. return [ids];
  63. }
  64. /**
  65. * Updates the hash with the data contributed by this instance.
  66. * @param {Hash} hash hash to be updated
  67. * @param {UpdateHashContext} context context
  68. * @returns {void}
  69. */
  70. updateHash(hash, context) {
  71. if (this._hashUpdate === undefined) {
  72. this._hashUpdate = this.identifier + (this.ids ? this.ids.join(",") : "");
  73. }
  74. hash.update(this._hashUpdate);
  75. }
  76. /**
  77. * Serializes this instance into the provided serializer context.
  78. * @param {ObjectSerializerContext} context context
  79. */
  80. serialize(context) {
  81. const { write } = context;
  82. write(this.identifier);
  83. write(this.ids);
  84. super.serialize(context);
  85. }
  86. /**
  87. * Restores this instance from the provided deserializer context.
  88. * @param {ObjectDeserializerContext} context context
  89. */
  90. deserialize(context) {
  91. const { read } = context;
  92. this.identifier = read();
  93. this.ids = read();
  94. super.deserialize(context);
  95. }
  96. }
  97. makeSerializable(
  98. ProvidedDependency,
  99. "webpack/lib/dependencies/ProvidedDependency"
  100. );
  101. class ProvidedDependencyTemplate extends ModuleDependency.Template {
  102. /**
  103. * Applies the plugin by registering its hooks on the compiler.
  104. * @param {Dependency} dependency the dependency for which the template should be applied
  105. * @param {ReplaceSource} source the current replace source which can be modified
  106. * @param {DependencyTemplateContext} templateContext the context object
  107. * @returns {void}
  108. */
  109. apply(
  110. dependency,
  111. source,
  112. {
  113. runtime,
  114. runtimeTemplate,
  115. moduleGraph,
  116. chunkGraph,
  117. initFragments,
  118. runtimeRequirements
  119. }
  120. ) {
  121. const dep = /** @type {ProvidedDependency} */ (dependency);
  122. const connection =
  123. /** @type {ModuleGraphConnection} */
  124. (moduleGraph.getConnection(dep));
  125. const exportsInfo = moduleGraph.getExportsInfo(connection.module);
  126. const usedName = exportsInfo.getUsedName(dep.ids, runtime);
  127. initFragments.push(
  128. new InitFragment(
  129. `/* provided dependency */ var ${
  130. dep.identifier
  131. } = ${runtimeTemplate.moduleExports({
  132. module: moduleGraph.getModule(dep),
  133. chunkGraph,
  134. request: dep.request,
  135. runtimeRequirements
  136. })}${pathToString(/** @type {string[] | null} */ (usedName))};\n`,
  137. InitFragment.STAGE_PROVIDES,
  138. 1,
  139. `provided ${dep.identifier}`
  140. )
  141. );
  142. source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
  143. }
  144. }
  145. ProvidedDependency.Template = ProvidedDependencyTemplate;
  146. module.exports = ProvidedDependency;