ConstDependency.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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("./NullDependency").RawRuntimeRequirements} RawRuntimeRequirements */
  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 {RawRuntimeRequirements | 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. /** @type {undefined | string} */
  33. this._hashUpdate = undefined;
  34. }
  35. /**
  36. * Update the hash
  37. * @param {Hash} hash hash to be updated
  38. * @param {UpdateHashContext} context context
  39. * @returns {void}
  40. */
  41. updateHash(hash, context) {
  42. if (this._hashUpdate === undefined) {
  43. let hashUpdate = `${this.range}|${this.expression}`;
  44. if (this.runtimeRequirements) {
  45. for (const item of this.runtimeRequirements) {
  46. hashUpdate += "|";
  47. hashUpdate += item;
  48. }
  49. }
  50. this._hashUpdate = hashUpdate;
  51. }
  52. hash.update(this._hashUpdate);
  53. }
  54. /**
  55. * @param {ModuleGraph} moduleGraph the module graph
  56. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  57. */
  58. getModuleEvaluationSideEffectsState(moduleGraph) {
  59. return false;
  60. }
  61. /**
  62. * @param {ObjectSerializerContext} context context
  63. */
  64. serialize(context) {
  65. const { write } = context;
  66. write(this.expression);
  67. write(this.range);
  68. write(this.runtimeRequirements);
  69. super.serialize(context);
  70. }
  71. /**
  72. * @param {ObjectDeserializerContext} context context
  73. */
  74. deserialize(context) {
  75. const { read } = context;
  76. this.expression = read();
  77. this.range = read();
  78. this.runtimeRequirements = read();
  79. super.deserialize(context);
  80. }
  81. }
  82. makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
  83. ConstDependency.Template = class ConstDependencyTemplate extends (
  84. NullDependency.Template
  85. ) {
  86. /**
  87. * @param {Dependency} dependency the dependency for which the template should be applied
  88. * @param {ReplaceSource} source the current replace source which can be modified
  89. * @param {DependencyTemplateContext} templateContext the context object
  90. * @returns {void}
  91. */
  92. apply(dependency, source, templateContext) {
  93. const dep = /** @type {ConstDependency} */ (dependency);
  94. if (dep.runtimeRequirements) {
  95. for (const req of dep.runtimeRequirements) {
  96. templateContext.runtimeRequirements.add(req);
  97. }
  98. }
  99. if (typeof dep.range === "number") {
  100. source.insert(dep.range, dep.expression);
  101. return;
  102. }
  103. source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
  104. }
  105. };
  106. module.exports = ConstDependency;