ConstDependency.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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("../util/Hash")} Hash */
  16. class ConstDependency extends NullDependency {
  17. /**
  18. * @param {string} expression the expression
  19. * @param {number|[number, number]} range the source range
  20. * @param {string[]=} runtimeRequirements runtime requirements
  21. */
  22. constructor(expression, range, runtimeRequirements) {
  23. super();
  24. this.expression = expression;
  25. this.range = range;
  26. this.runtimeRequirements = runtimeRequirements
  27. ? new Set(runtimeRequirements)
  28. : null;
  29. this._hashUpdate = undefined;
  30. }
  31. /**
  32. * Update the hash
  33. * @param {Hash} hash hash to be updated
  34. * @param {UpdateHashContext} context context
  35. * @returns {void}
  36. */
  37. updateHash(hash, context) {
  38. if (this._hashUpdate === undefined) {
  39. let hashUpdate = "" + this.range + "|" + this.expression;
  40. if (this.runtimeRequirements) {
  41. for (const item of this.runtimeRequirements) {
  42. hashUpdate += "|";
  43. hashUpdate += item;
  44. }
  45. }
  46. this._hashUpdate = hashUpdate;
  47. }
  48. hash.update(this._hashUpdate);
  49. }
  50. /**
  51. * @param {ModuleGraph} moduleGraph the module graph
  52. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  53. */
  54. getModuleEvaluationSideEffectsState(moduleGraph) {
  55. return false;
  56. }
  57. serialize(context) {
  58. const { write } = context;
  59. write(this.expression);
  60. write(this.range);
  61. write(this.runtimeRequirements);
  62. super.serialize(context);
  63. }
  64. deserialize(context) {
  65. const { read } = context;
  66. this.expression = read();
  67. this.range = read();
  68. this.runtimeRequirements = read();
  69. super.deserialize(context);
  70. }
  71. }
  72. makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
  73. ConstDependency.Template = class ConstDependencyTemplate extends (
  74. NullDependency.Template
  75. ) {
  76. /**
  77. * @param {Dependency} dependency the dependency for which the template should be applied
  78. * @param {ReplaceSource} source the current replace source which can be modified
  79. * @param {DependencyTemplateContext} templateContext the context object
  80. * @returns {void}
  81. */
  82. apply(dependency, source, templateContext) {
  83. const dep = /** @type {ConstDependency} */ (dependency);
  84. if (dep.runtimeRequirements) {
  85. for (const req of dep.runtimeRequirements) {
  86. templateContext.runtimeRequirements.add(req);
  87. }
  88. }
  89. if (typeof dep.range === "number") {
  90. source.insert(dep.range, dep.expression);
  91. return;
  92. }
  93. source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
  94. }
  95. };
  96. module.exports = ConstDependency;