ConstDependency.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const NullDependency = require("./NullDependency");
  8. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  15. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  18. /** @typedef {import("../util/Hash")} Hash */
  19. class ConstDependency extends NullDependency {
  20. /**
  21. * @param {string} expression the expression
  22. * @param {number | Range} range the source range
  23. * @param {(string[] | null)=} runtimeRequirements runtime requirements
  24. */
  25. constructor(expression, range, runtimeRequirements) {
  26. super();
  27. this.expression = expression;
  28. this.range = range;
  29. this.runtimeRequirements = runtimeRequirements
  30. ? new Set(runtimeRequirements)
  31. : null;
  32. this._hashUpdate = undefined;
  33. }
  34. /**
  35. * Update the hash
  36. * @param {Hash} hash hash to be updated
  37. * @param {UpdateHashContext} context context
  38. * @returns {void}
  39. */
  40. updateHash(hash, context) {
  41. if (this._hashUpdate === undefined) {
  42. let hashUpdate = "" + this.range + "|" + this.expression;
  43. if (this.runtimeRequirements) {
  44. for (const item of this.runtimeRequirements) {
  45. hashUpdate += "|";
  46. hashUpdate += item;
  47. }
  48. }
  49. this._hashUpdate = hashUpdate;
  50. }
  51. hash.update(this._hashUpdate);
  52. }
  53. /**
  54. * @param {ModuleGraph} moduleGraph the module graph
  55. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  56. */
  57. getModuleEvaluationSideEffectsState(moduleGraph) {
  58. return false;
  59. }
  60. /**
  61. * @param {ObjectSerializerContext} context context
  62. */
  63. serialize(context) {
  64. const { write } = context;
  65. write(this.expression);
  66. write(this.range);
  67. write(this.runtimeRequirements);
  68. super.serialize(context);
  69. }
  70. /**
  71. * @param {ObjectDeserializerContext} context context
  72. */
  73. deserialize(context) {
  74. const { read } = context;
  75. this.expression = read();
  76. this.range = read();
  77. this.runtimeRequirements = read();
  78. super.deserialize(context);
  79. }
  80. }
  81. makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
  82. ConstDependency.Template = class ConstDependencyTemplate extends (
  83. NullDependency.Template
  84. ) {
  85. /**
  86. * @param {Dependency} dependency the dependency for which the template should be applied
  87. * @param {ReplaceSource} source the current replace source which can be modified
  88. * @param {DependencyTemplateContext} templateContext the context object
  89. * @returns {void}
  90. */
  91. apply(dependency, source, templateContext) {
  92. const dep = /** @type {ConstDependency} */ (dependency);
  93. if (dep.runtimeRequirements) {
  94. for (const req of dep.runtimeRequirements) {
  95. templateContext.runtimeRequirements.add(req);
  96. }
  97. }
  98. if (typeof dep.range === "number") {
  99. source.insert(dep.range, dep.expression);
  100. return;
  101. }
  102. source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
  103. }
  104. };
  105. module.exports = ConstDependency;