AsyncDependenciesBlock.js 3.6 KB

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