ProvidedDependency.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Florent Cailhol @ooflorent
  4. */
  5. "use strict";
  6. const InitFragment = require("../InitFragment");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const ModuleDependency = require("./ModuleDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  11. /** @typedef {import("../Dependency")} Dependency */
  12. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  13. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
  15. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  16. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. /**
  19. * @param {string[]|null} path the property path array
  20. * @returns {string} the converted path
  21. */
  22. const pathToString = path =>
  23. path !== null && path.length > 0
  24. ? path.map(part => `[${JSON.stringify(part)}]`).join("")
  25. : "";
  26. class ProvidedDependency extends ModuleDependency {
  27. constructor(request, identifier, path, range) {
  28. super(request);
  29. this.identifier = identifier;
  30. this.path = path;
  31. this.range = range;
  32. this._hashUpdate = undefined;
  33. }
  34. get type() {
  35. return "provided";
  36. }
  37. get category() {
  38. return "esm";
  39. }
  40. /**
  41. * Update the hash
  42. * @param {Hash} hash hash to be updated
  43. * @param {UpdateHashContext} context context
  44. * @returns {void}
  45. */
  46. updateHash(hash, context) {
  47. if (this._hashUpdate === undefined) {
  48. this._hashUpdate =
  49. this.identifier + (this.path ? this.path.join(",") : "null");
  50. }
  51. hash.update(this._hashUpdate);
  52. }
  53. serialize(context) {
  54. const { write } = context;
  55. write(this.identifier);
  56. write(this.path);
  57. super.serialize(context);
  58. }
  59. deserialize(context) {
  60. const { read } = context;
  61. this.identifier = read();
  62. this.path = read();
  63. super.deserialize(context);
  64. }
  65. }
  66. makeSerializable(
  67. ProvidedDependency,
  68. "webpack/lib/dependencies/ProvidedDependency"
  69. );
  70. class ProvidedDependencyTemplate extends ModuleDependency.Template {
  71. /**
  72. * @param {Dependency} dependency the dependency for which the template should be applied
  73. * @param {ReplaceSource} source the current replace source which can be modified
  74. * @param {DependencyTemplateContext} templateContext the context object
  75. * @returns {void}
  76. */
  77. apply(
  78. dependency,
  79. source,
  80. {
  81. runtimeTemplate,
  82. moduleGraph,
  83. chunkGraph,
  84. initFragments,
  85. runtimeRequirements
  86. }
  87. ) {
  88. const dep = /** @type {ProvidedDependency} */ (dependency);
  89. initFragments.push(
  90. new InitFragment(
  91. `/* provided dependency */ var ${
  92. dep.identifier
  93. } = ${runtimeTemplate.moduleExports({
  94. module: moduleGraph.getModule(dep),
  95. chunkGraph,
  96. request: dep.request,
  97. runtimeRequirements
  98. })}${pathToString(dep.path)};\n`,
  99. InitFragment.STAGE_PROVIDES,
  100. 1,
  101. `provided ${dep.identifier}`
  102. )
  103. );
  104. source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
  105. }
  106. }
  107. ProvidedDependency.Template = ProvidedDependencyTemplate;
  108. module.exports = ProvidedDependency;