RuntimeRequirementsDependency.js 2.3 KB

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