ProvidedDependency.js 4.5 KB

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