ProvideSharedDependency.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const makeSerializable = require("../util/makeSerializable");
  8. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, string, string, string | false, boolean]>} ObjectDeserializerContext */
  9. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, string, string, string | false, boolean]>} ObjectSerializerContext */
  10. class ProvideSharedDependency extends Dependency {
  11. /**
  12. * Creates an instance of ProvideSharedDependency.
  13. * @param {string} shareScope share scope
  14. * @param {string} name module name
  15. * @param {string | false} version version
  16. * @param {string} request request
  17. * @param {boolean} eager true, if this is an eager dependency
  18. */
  19. constructor(shareScope, name, version, request, eager) {
  20. super();
  21. /** @type {string} */
  22. this.shareScope = shareScope;
  23. /** @type {string} */
  24. this.name = name;
  25. /** @type {string | false} */
  26. this.version = version;
  27. /** @type {string} */
  28. this.request = request;
  29. /** @type {boolean} */
  30. this.eager = eager;
  31. }
  32. get type() {
  33. return "provide shared module";
  34. }
  35. /**
  36. * Returns an identifier to merge equal requests.
  37. * @returns {string | null} an identifier to merge equal requests
  38. */
  39. getResourceIdentifier() {
  40. return `provide module (${this.shareScope}) ${this.request} as ${
  41. this.name
  42. } @ ${this.version}${this.eager ? " (eager)" : ""}`;
  43. }
  44. /**
  45. * Serializes this instance into the provided serializer context.
  46. * @param {ObjectSerializerContext} context context
  47. */
  48. serialize(context) {
  49. context
  50. .write(this.shareScope)
  51. .write(this.name)
  52. .write(this.request)
  53. .write(this.version)
  54. .write(this.eager);
  55. super.serialize(context);
  56. }
  57. /**
  58. * Restores this instance from the provided deserializer context.
  59. * @param {ObjectDeserializerContext} context context
  60. * @returns {ProvideSharedDependency} deserialize fallback dependency
  61. */
  62. static deserialize(context) {
  63. // wire order is shareScope, name, request, version, eager;
  64. // reorder into the constructor's (…, version, request, …) signature
  65. const shareScope = context.read();
  66. const c1 = context.rest;
  67. const name = c1.read();
  68. const c2 = c1.rest;
  69. const request = c2.read();
  70. const c3 = c2.rest;
  71. const version = c3.read();
  72. const c4 = c3.rest;
  73. const eager = c4.read();
  74. const obj = new ProvideSharedDependency(
  75. shareScope,
  76. name,
  77. version,
  78. request,
  79. eager
  80. );
  81. obj.deserialize(c4.rest);
  82. return obj;
  83. }
  84. }
  85. makeSerializable(
  86. ProvideSharedDependency,
  87. "webpack/lib/sharing/ProvideSharedDependency"
  88. );
  89. module.exports = ProvideSharedDependency;