RuntimeRequirementsDependency.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const NullDependency = require("./NullDependency");
  8. /** @import { ReplaceSource } from "webpack-sources" */
  9. /** @import { RawRuntimeRequirements } from "./NullDependency" */
  10. /** @import Dependency, { UpdateHashContext } from "../Dependency" */
  11. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Set<string>]>} ObjectDeserializerContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Set<string>]>} ObjectSerializerContext */
  14. /** @import Hash from "../util/Hash" */
  15. class RuntimeRequirementsDependency extends NullDependency {
  16. /**
  17. * Creates an instance of RuntimeRequirementsDependency.
  18. * @param {RawRuntimeRequirements} runtimeRequirements runtime requirements
  19. */
  20. constructor(runtimeRequirements) {
  21. super();
  22. /** @type {Set<string>} */
  23. this.runtimeRequirements = new Set(runtimeRequirements);
  24. /** @type {undefined | string} */
  25. this._hashUpdate = undefined;
  26. }
  27. /**
  28. * Updates the hash with the data contributed by this instance.
  29. * @param {Hash} hash hash to be updated
  30. * @param {UpdateHashContext} context context
  31. * @returns {void}
  32. */
  33. updateHash(hash, context) {
  34. if (this._hashUpdate === undefined) {
  35. this._hashUpdate = `${[...this.runtimeRequirements].join()}`;
  36. }
  37. hash.update(this._hashUpdate);
  38. }
  39. /**
  40. * Serializes this instance into the provided serializer context.
  41. * @param {ObjectSerializerContext} context context
  42. */
  43. serialize(context) {
  44. context.write(this.runtimeRequirements);
  45. super.serialize(context);
  46. }
  47. /**
  48. * Restores this instance from the provided deserializer context.
  49. * @param {ObjectDeserializerContext} context context
  50. */
  51. deserialize(context) {
  52. this.runtimeRequirements = context.read();
  53. super.deserialize(context.rest);
  54. }
  55. }
  56. makeSerializable(
  57. RuntimeRequirementsDependency,
  58. "webpack/lib/dependencies/RuntimeRequirementsDependency"
  59. );
  60. RuntimeRequirementsDependency.Template = class RuntimeRequirementsDependencyTemplate extends (
  61. NullDependency.Template
  62. ) {
  63. /**
  64. * Applies the plugin by registering its hooks on the compiler.
  65. * @param {Dependency} dependency the dependency for which the template should be applied
  66. * @param {ReplaceSource} source the current replace source which can be modified
  67. * @param {DependencyTemplateContext} templateContext the context object
  68. * @returns {void}
  69. */
  70. apply(dependency, source, { runtimeRequirements }) {
  71. const dep = /** @type {RuntimeRequirementsDependency} */ (dependency);
  72. for (const req of dep.runtimeRequirements) {
  73. runtimeRequirements.add(req);
  74. }
  75. }
  76. };
  77. module.exports = RuntimeRequirementsDependency;