ProvidedDependency.js 4.0 KB

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