CachedConstDependency.js 3.9 KB

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