RuntimeRequirementsDependency.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("./NullDependency").RawRuntimeRequirements} RawRuntimeRequirements */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  15. /** @typedef {import("../util/Hash")} Hash */
  16. class RuntimeRequirementsDependency extends NullDependency {
  17. /**
  18. * Creates an instance of RuntimeRequirementsDependency.
  19. * @param {RawRuntimeRequirements} runtimeRequirements runtime requirements
  20. */
  21. constructor(runtimeRequirements) {
  22. super();
  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. const { write } = context;
  45. write(this.runtimeRequirements);
  46. super.serialize(context);
  47. }
  48. /**
  49. * Restores this instance from the provided deserializer context.
  50. * @param {ObjectDeserializerContext} context context
  51. */
  52. deserialize(context) {
  53. const { read } = context;
  54. this.runtimeRequirements = read();
  55. super.deserialize(context);
  56. }
  57. }
  58. makeSerializable(
  59. RuntimeRequirementsDependency,
  60. "webpack/lib/dependencies/RuntimeRequirementsDependency"
  61. );
  62. RuntimeRequirementsDependency.Template = class RuntimeRequirementsDependencyTemplate extends (
  63. NullDependency.Template
  64. ) {
  65. /**
  66. * Applies the plugin by registering its hooks on the compiler.
  67. * @param {Dependency} dependency the dependency for which the template should be applied
  68. * @param {ReplaceSource} source the current replace source which can be modified
  69. * @param {DependencyTemplateContext} templateContext the context object
  70. * @returns {void}
  71. */
  72. apply(dependency, source, { runtimeRequirements }) {
  73. const dep = /** @type {RuntimeRequirementsDependency} */ (dependency);
  74. for (const req of dep.runtimeRequirements) {
  75. runtimeRequirements.add(req);
  76. }
  77. }
  78. };
  79. module.exports = RuntimeRequirementsDependency;