CachedConstDependency.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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("../Dependency")} Dependency */
  12. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  13. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. class CachedConstDependency extends NullDependency {
  19. /**
  20. * @param {string} expression expression
  21. * @param {Range | null} range range
  22. * @param {string} identifier identifier
  23. * @param {number=} place place where we inject the expression
  24. */
  25. constructor(
  26. expression,
  27. range,
  28. identifier,
  29. place = CachedConstDependency.PLACE_MODULE
  30. ) {
  31. super();
  32. this.expression = expression;
  33. this.range = range;
  34. this.identifier = identifier;
  35. this.place = place;
  36. this._hashUpdate = undefined;
  37. }
  38. /**
  39. * @returns {string} hash update
  40. */
  41. _createHashUpdate() {
  42. return `${this.place}${this.identifier}${this.range}${this.expression}`;
  43. }
  44. /**
  45. * Update the hash
  46. * @param {Hash} hash hash to be updated
  47. * @param {UpdateHashContext} context context
  48. * @returns {void}
  49. */
  50. updateHash(hash, context) {
  51. if (this._hashUpdate === undefined) {
  52. this._hashUpdate = this._createHashUpdate();
  53. }
  54. hash.update(this._hashUpdate);
  55. }
  56. /**
  57. * @param {ObjectSerializerContext} context context
  58. */
  59. serialize(context) {
  60. const { write } = context;
  61. write(this.expression);
  62. write(this.range);
  63. write(this.identifier);
  64. write(this.place);
  65. super.serialize(context);
  66. }
  67. /**
  68. * @param {ObjectDeserializerContext} context context
  69. */
  70. deserialize(context) {
  71. const { read } = context;
  72. this.expression = read();
  73. this.range = read();
  74. this.identifier = read();
  75. this.place = read();
  76. super.deserialize(context);
  77. }
  78. }
  79. CachedConstDependency.PLACE_MODULE = 10;
  80. CachedConstDependency.PLACE_CHUNK = 20;
  81. makeSerializable(
  82. CachedConstDependency,
  83. "webpack/lib/dependencies/CachedConstDependency"
  84. );
  85. CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
  86. DependencyTemplate
  87. ) {
  88. /**
  89. * @param {Dependency} dependency the dependency for which the template should be applied
  90. * @param {ReplaceSource} source the current replace source which can be modified
  91. * @param {DependencyTemplateContext} templateContext the context object
  92. * @returns {void}
  93. */
  94. apply(dependency, source, { initFragments, chunkInitFragments }) {
  95. const dep = /** @type {CachedConstDependency} */ (dependency);
  96. (dep.place === CachedConstDependency.PLACE_MODULE
  97. ? initFragments
  98. : chunkInitFragments
  99. ).push(
  100. new InitFragment(
  101. `var ${dep.identifier} = ${dep.expression};\n`,
  102. InitFragment.STAGE_CONSTANTS,
  103. // For a chunk we inject expression after imports
  104. dep.place === CachedConstDependency.PLACE_MODULE ? 0 : 10,
  105. `const ${dep.identifier}`
  106. )
  107. );
  108. if (typeof dep.range === "number") {
  109. source.insert(dep.range, dep.identifier);
  110. } else if (dep.range !== null) {
  111. source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
  112. }
  113. }
  114. };
  115. module.exports = CachedConstDependency;