AsyncDependenciesBlock.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @param {Hash} hash the hash used to track dependencies
  66. * @param {UpdateHashContext} context context
  67. * @returns {void}
  68. */
  69. updateHash(hash, context) {
  70. const { chunkGraph } = context;
  71. if (this._stringifiedGroupOptions === undefined) {
  72. this._stringifiedGroupOptions = JSON.stringify(this.groupOptions);
  73. }
  74. const chunkGroup = chunkGraph.getBlockChunkGroup(this);
  75. hash.update(
  76. `${this._stringifiedGroupOptions}${chunkGroup ? chunkGroup.id : ""}`
  77. );
  78. super.updateHash(hash, context);
  79. }
  80. /**
  81. * @param {ObjectSerializerContext} context context
  82. */
  83. serialize(context) {
  84. const { write } = context;
  85. write(this.groupOptions);
  86. write(this.loc);
  87. write(this.request);
  88. super.serialize(context);
  89. }
  90. /**
  91. * @param {ObjectDeserializerContext} context context
  92. */
  93. deserialize(context) {
  94. const { read } = context;
  95. this.groupOptions = read();
  96. this.loc = read();
  97. this.request = read();
  98. super.deserialize(context);
  99. }
  100. }
  101. makeSerializable(AsyncDependenciesBlock, "webpack/lib/AsyncDependenciesBlock");
  102. Object.defineProperty(AsyncDependenciesBlock.prototype, "module", {
  103. get() {
  104. throw new Error(
  105. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  106. );
  107. },
  108. set() {
  109. throw new Error(
  110. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  111. );
  112. }
  113. });
  114. module.exports = AsyncDependenciesBlock;