BuildTimeConstDependency.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const ConstDependency = require("./ConstDependency");
  8. const NullDependency = require("./NullDependency");
  9. /** @import { ReplaceSource } from "webpack-sources" */
  10. /** @import Dependency, { UpdateHashContext } from "../Dependency" */
  11. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  12. /** @import { Range } from "../javascript/JavascriptParser" */
  13. /**
  14. * @import {
  15. * ObjectDeserializerContext,
  16. * ObjectSerializerContext
  17. * } from "../serialization/ObjectMiddleware"
  18. */
  19. /** @import Hash from "../util/Hash" */
  20. /**
  21. * A constant replacement whose source is only valid in emitted output (e.g.
  22. * native `import.meta`), with a separate expression for build-time execution.
  23. */
  24. class BuildTimeConstDependency extends ConstDependency {
  25. /**
  26. * Creates an instance of BuildTimeConstDependency.
  27. * @param {string} expression the expression
  28. * @param {string} buildTimeExpression the expression for build-time execution
  29. * @param {Range} range the source range
  30. * @param {(string[] | null)=} runtimeRequirements runtime requirements
  31. */
  32. constructor(expression, buildTimeExpression, range, runtimeRequirements) {
  33. super(expression, range, runtimeRequirements);
  34. /** @type {string} */
  35. this.buildTimeExpression = buildTimeExpression;
  36. }
  37. /**
  38. * Updates the hash with the data contributed by this instance.
  39. * @param {Hash} hash hash to be updated
  40. * @param {UpdateHashContext} context context
  41. * @returns {void}
  42. */
  43. updateHash(hash, context) {
  44. super.updateHash(hash, context);
  45. hash.update(this.buildTimeExpression);
  46. }
  47. /**
  48. * Serializes this instance into the provided serializer context.
  49. * @param {ObjectSerializerContext} context context
  50. */
  51. serialize(context) {
  52. context.write(this.buildTimeExpression);
  53. super.serialize(context);
  54. }
  55. /**
  56. * Restores this instance from the provided deserializer context.
  57. * @param {ObjectDeserializerContext} context context
  58. */
  59. deserialize(context) {
  60. this.buildTimeExpression = context.read();
  61. super.deserialize(context.rest);
  62. }
  63. }
  64. makeSerializable(
  65. BuildTimeConstDependency,
  66. "webpack/lib/dependencies/BuildTimeConstDependency"
  67. );
  68. BuildTimeConstDependency.Template = class BuildTimeConstDependencyTemplate extends (
  69. NullDependency.Template
  70. ) {
  71. /**
  72. * Applies the plugin by registering its hooks on the compiler.
  73. * @param {Dependency} dependency the dependency for which the template should be applied
  74. * @param {ReplaceSource} source the current replace source which can be modified
  75. * @param {DependencyTemplateContext} templateContext the context object
  76. * @returns {void}
  77. */
  78. apply(dependency, source, templateContext) {
  79. const dep = /** @type {BuildTimeConstDependency} */ (dependency);
  80. if (dep.runtimeRequirements) {
  81. for (const req of dep.runtimeRequirements) {
  82. templateContext.runtimeRequirements.add(req);
  83. }
  84. }
  85. const expression = templateContext.chunkGraph.buildTimeExecution
  86. ? dep.buildTimeExpression
  87. : dep.expression;
  88. const range = /** @type {Range} */ (dep.range);
  89. source.replace(range[0], range[1] - 1, expression);
  90. }
  91. };
  92. module.exports = BuildTimeConstDependency;