CachedConstDependency.js 3.7 KB

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