CachedConstDependency.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * 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. this._hashUpdate = "" + this.identifier + this.range + this.expression;
  44. hash.update(this._hashUpdate);
  45. }
  46. /**
  47. * @param {ObjectSerializerContext} context context
  48. */
  49. serialize(context) {
  50. const { write } = context;
  51. write(this.expression);
  52. write(this.range);
  53. write(this.identifier);
  54. super.serialize(context);
  55. }
  56. /**
  57. * @param {ObjectDeserializerContext} context context
  58. */
  59. deserialize(context) {
  60. const { read } = context;
  61. this.expression = read();
  62. this.range = read();
  63. this.identifier = read();
  64. super.deserialize(context);
  65. }
  66. }
  67. makeSerializable(
  68. CachedConstDependency,
  69. "webpack/lib/dependencies/CachedConstDependency"
  70. );
  71. CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
  72. DependencyTemplate
  73. ) {
  74. /**
  75. * @param {Dependency} dependency the dependency for which the template should be applied
  76. * @param {ReplaceSource} source the current replace source which can be modified
  77. * @param {DependencyTemplateContext} templateContext the context object
  78. * @returns {void}
  79. */
  80. apply(
  81. dependency,
  82. source,
  83. { runtimeTemplate, dependencyTemplates, initFragments }
  84. ) {
  85. const dep = /** @type {CachedConstDependency} */ (dependency);
  86. initFragments.push(
  87. new InitFragment(
  88. `var ${dep.identifier} = ${dep.expression};\n`,
  89. InitFragment.STAGE_CONSTANTS,
  90. 0,
  91. `const ${dep.identifier}`
  92. )
  93. );
  94. if (typeof dep.range === "number") {
  95. source.insert(dep.range, dep.identifier);
  96. return;
  97. }
  98. source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
  99. }
  100. };
  101. module.exports = CachedConstDependency;