ConstDependency.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /** @import { ReplaceSource } from "webpack-sources" */
  9. /** @import { RawRuntimeRequirements } from "./NullDependency" */
  10. /** @import Dependency, { UpdateHashContext } from "../Dependency" */
  11. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  12. /** @import ModuleGraph from "../ModuleGraph" */
  13. /** @import { ConnectionState } from "../ModuleGraphConnection" */
  14. /** @import { Range } from "../javascript/JavascriptParser" */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, number | Range, Set<string> | null]>} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, number | Range, Set<string> | null]>} ObjectSerializerContext */
  17. /** @import Hash from "../util/Hash" */
  18. class ConstDependency extends NullDependency {
  19. /**
  20. * Creates an instance of ConstDependency.
  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. /** @type {string} */
  28. this.expression = expression;
  29. this.range = range;
  30. /** @type {Set<string> | null} */
  31. this.runtimeRequirements = runtimeRequirements
  32. ? new Set(runtimeRequirements)
  33. : null;
  34. /** @type {undefined | string} */
  35. this._hashUpdate = undefined;
  36. }
  37. /**
  38. * Updates the hash with the data contributed by this instance.
  39. * @param {Hash} hash hash to be updated
  40. * @param {UpdateHashContext} context context
  41. * @returns {void}
  42. */
  43. updateHash(hash, context) {
  44. if (this._hashUpdate === undefined) {
  45. let hashUpdate = `${this.range}|${this.expression}`;
  46. if (this.runtimeRequirements) {
  47. for (const item of this.runtimeRequirements) {
  48. hashUpdate += "|";
  49. hashUpdate += item;
  50. }
  51. }
  52. this._hashUpdate = hashUpdate;
  53. }
  54. hash.update(this._hashUpdate);
  55. }
  56. /**
  57. * Gets module evaluation side effects state.
  58. * @param {ModuleGraph} moduleGraph the module graph
  59. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  60. */
  61. getModuleEvaluationSideEffectsState(moduleGraph) {
  62. return false;
  63. }
  64. /**
  65. * Serializes this instance into the provided serializer context.
  66. * @param {ObjectSerializerContext} context context
  67. */
  68. serialize(context) {
  69. // chain writes so each position is type-checked against the tuple
  70. context
  71. .write(this.expression)
  72. .write(this.range)
  73. .write(this.runtimeRequirements);
  74. super.serialize(context);
  75. }
  76. /**
  77. * Restores this instance from the provided deserializer context.
  78. * @param {ObjectDeserializerContext} context context
  79. */
  80. deserialize(context) {
  81. // advance the cursor via `rest` so each read is type-checked by position
  82. this.expression = context.read();
  83. const c1 = context.rest;
  84. this.range = c1.read();
  85. const c2 = c1.rest;
  86. this.runtimeRequirements = c2.read();
  87. super.deserialize(c2.rest);
  88. }
  89. }
  90. makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
  91. ConstDependency.Template = class ConstDependencyTemplate extends (
  92. NullDependency.Template
  93. ) {
  94. /**
  95. * Applies the plugin by registering its hooks on the compiler.
  96. * @param {Dependency} dependency the dependency for which the template should be applied
  97. * @param {ReplaceSource} source the current replace source which can be modified
  98. * @param {DependencyTemplateContext} templateContext the context object
  99. * @returns {void}
  100. */
  101. apply(dependency, source, templateContext) {
  102. const dep = /** @type {ConstDependency} */ (dependency);
  103. if (dep.runtimeRequirements) {
  104. for (const req of dep.runtimeRequirements) {
  105. templateContext.runtimeRequirements.add(req);
  106. }
  107. }
  108. if (typeof dep.range === "number") {
  109. source.insert(dep.range, dep.expression);
  110. return;
  111. }
  112. source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
  113. }
  114. };
  115. module.exports = ConstDependency;