RuntimeRequirementsDependency.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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("../ChunkGraph")} ChunkGraph */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  16. /** @typedef {import("../util/Hash")} Hash */
  17. class RuntimeRequirementsDependency extends NullDependency {
  18. /**
  19. * @param {string[]} runtimeRequirements runtime requirements
  20. */
  21. constructor(runtimeRequirements) {
  22. super();
  23. this.runtimeRequirements = new Set(runtimeRequirements);
  24. this._hashUpdate = undefined;
  25. }
  26. /**
  27. * Update the hash
  28. * @param {Hash} hash hash to be updated
  29. * @param {UpdateHashContext} context context
  30. * @returns {void}
  31. */
  32. updateHash(hash, context) {
  33. if (this._hashUpdate === undefined) {
  34. this._hashUpdate = Array.from(this.runtimeRequirements).join() + "";
  35. }
  36. hash.update(this._hashUpdate);
  37. }
  38. /**
  39. * @param {ObjectSerializerContext} context context
  40. */
  41. serialize(context) {
  42. const { write } = context;
  43. write(this.runtimeRequirements);
  44. super.serialize(context);
  45. }
  46. /**
  47. * @param {ObjectDeserializerContext} context context
  48. */
  49. deserialize(context) {
  50. const { read } = context;
  51. this.runtimeRequirements = read();
  52. super.deserialize(context);
  53. }
  54. }
  55. makeSerializable(
  56. RuntimeRequirementsDependency,
  57. "webpack/lib/dependencies/RuntimeRequirementsDependency"
  58. );
  59. RuntimeRequirementsDependency.Template = class RuntimeRequirementsDependencyTemplate extends (
  60. NullDependency.Template
  61. ) {
  62. /**
  63. * @param {Dependency} dependency the dependency for which the template should be applied
  64. * @param {ReplaceSource} source the current replace source which can be modified
  65. * @param {DependencyTemplateContext} templateContext the context object
  66. * @returns {void}
  67. */
  68. apply(dependency, source, { runtimeRequirements }) {
  69. const dep = /** @type {RuntimeRequirementsDependency} */ (dependency);
  70. for (const req of dep.runtimeRequirements) {
  71. runtimeRequirements.add(req);
  72. }
  73. }
  74. };
  75. module.exports = RuntimeRequirementsDependency;