CachedConstDependency.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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("../util/Hash")} Hash */
  19. class CachedConstDependency extends NullDependency {
  20. constructor(expression, range, identifier) {
  21. super();
  22. this.expression = expression;
  23. this.range = range;
  24. this.identifier = identifier;
  25. this._hashUpdate = undefined;
  26. }
  27. /**
  28. * Update the hash
  29. * @param {Hash} hash hash to be updated
  30. * @param {UpdateHashContext} context context
  31. * @returns {void}
  32. */
  33. updateHash(hash, context) {
  34. if (this._hashUpdate === undefined)
  35. this._hashUpdate = "" + this.identifier + this.range + this.expression;
  36. hash.update(this._hashUpdate);
  37. }
  38. serialize(context) {
  39. const { write } = context;
  40. write(this.expression);
  41. write(this.range);
  42. write(this.identifier);
  43. super.serialize(context);
  44. }
  45. deserialize(context) {
  46. const { read } = context;
  47. this.expression = read();
  48. this.range = read();
  49. this.identifier = read();
  50. super.deserialize(context);
  51. }
  52. }
  53. makeSerializable(
  54. CachedConstDependency,
  55. "webpack/lib/dependencies/CachedConstDependency"
  56. );
  57. CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
  58. DependencyTemplate
  59. ) {
  60. /**
  61. * @param {Dependency} dependency the dependency for which the template should be applied
  62. * @param {ReplaceSource} source the current replace source which can be modified
  63. * @param {DependencyTemplateContext} templateContext the context object
  64. * @returns {void}
  65. */
  66. apply(
  67. dependency,
  68. source,
  69. { runtimeTemplate, dependencyTemplates, initFragments }
  70. ) {
  71. const dep = /** @type {CachedConstDependency} */ (dependency);
  72. initFragments.push(
  73. new InitFragment(
  74. `var ${dep.identifier} = ${dep.expression};\n`,
  75. InitFragment.STAGE_CONSTANTS,
  76. 0,
  77. `const ${dep.identifier}`
  78. )
  79. );
  80. if (typeof dep.range === "number") {
  81. source.insert(dep.range, dep.identifier);
  82. return;
  83. }
  84. source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
  85. }
  86. };
  87. module.exports = CachedConstDependency;