CachedConstDependency.js 4.0 KB

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