ConstDependency.js 4.0 KB

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