CachedConstDependency.js 3.4 KB

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