AsyncDependenciesBlock.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DependenciesBlock = require("./DependenciesBlock");
  7. const makeSerializable = require("./util/makeSerializable");
  8. /** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
  9. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  10. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  11. /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
  12. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  13. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  14. /** @typedef {import("./util/Hash")} Hash */
  15. /** @typedef {(ChunkGroupOptions & { entryOptions?: EntryOptions } & { circular?: boolean })} GroupOptions */
  16. class AsyncDependenciesBlock extends DependenciesBlock {
  17. /**
  18. * @param {GroupOptions | string | null} groupOptions options for the group
  19. * @param {(DependencyLocation | null)=} loc the line of code
  20. * @param {(string | null)=} request the request
  21. */
  22. constructor(groupOptions, loc, request) {
  23. super();
  24. if (typeof groupOptions === "string") {
  25. groupOptions = { name: groupOptions };
  26. } else if (!groupOptions) {
  27. groupOptions = { name: undefined };
  28. }
  29. if (typeof groupOptions.circular !== "boolean") {
  30. // default allow circular references
  31. groupOptions.circular = true;
  32. }
  33. /** @type {GroupOptions} */
  34. this.groupOptions = groupOptions;
  35. /** @type {DependencyLocation | null | undefined} */
  36. this.loc = loc;
  37. /** @type {string | null | undefined} */
  38. this.request = request;
  39. /** @type {undefined | string} */
  40. this._stringifiedGroupOptions = undefined;
  41. }
  42. /**
  43. * @returns {ChunkGroupOptions["name"]} The name of the chunk
  44. */
  45. get chunkName() {
  46. return this.groupOptions.name;
  47. }
  48. /**
  49. * @param {string | undefined} value The new chunk name
  50. * @returns {void}
  51. */
  52. set chunkName(value) {
  53. if (this.groupOptions.name !== value) {
  54. this.groupOptions.name = value;
  55. this._stringifiedGroupOptions = undefined;
  56. }
  57. }
  58. /**
  59. * @returns {boolean} Whether circular references are allowed
  60. */
  61. get circular() {
  62. return Boolean(this.groupOptions.circular);
  63. }
  64. /**
  65. * Updates the hash with the data contributed by this instance.
  66. * @param {Hash} hash the hash used to track dependencies
  67. * @param {UpdateHashContext} context context
  68. * @returns {void}
  69. */
  70. updateHash(hash, context) {
  71. const { chunkGraph } = context;
  72. if (this._stringifiedGroupOptions === undefined) {
  73. this._stringifiedGroupOptions = JSON.stringify(this.groupOptions);
  74. }
  75. const chunkGroup = chunkGraph.getBlockChunkGroup(this);
  76. hash.update(
  77. `${this._stringifiedGroupOptions}${chunkGroup ? chunkGroup.id : ""}`
  78. );
  79. super.updateHash(hash, context);
  80. }
  81. /**
  82. * Serializes this instance into the provided serializer context.
  83. * @param {ObjectSerializerContext} context context
  84. */
  85. serialize(context) {
  86. const { write } = context;
  87. write(this.groupOptions);
  88. write(this.loc);
  89. write(this.request);
  90. super.serialize(context);
  91. }
  92. /**
  93. * Restores this instance from the provided deserializer context.
  94. * @param {ObjectDeserializerContext} context context
  95. */
  96. deserialize(context) {
  97. const { read } = context;
  98. this.groupOptions = read();
  99. this.loc = read();
  100. this.request = read();
  101. super.deserialize(context);
  102. }
  103. }
  104. makeSerializable(AsyncDependenciesBlock, "webpack/lib/AsyncDependenciesBlock");
  105. Object.defineProperty(AsyncDependenciesBlock.prototype, "module", {
  106. get() {
  107. throw new Error(
  108. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  109. );
  110. },
  111. set() {
  112. throw new Error(
  113. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  114. );
  115. }
  116. });
  117. module.exports = AsyncDependenciesBlock;