AsyncDependenciesBlock.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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("./ChunkGraph")} ChunkGraph */
  9. /** @typedef {import("./ChunkGroup")} ChunkGroup */
  10. /** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
  11. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  12. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  13. /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
  14. /** @typedef {import("./Module")} Module */
  15. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("./util/Hash")} Hash */
  18. /** @typedef {(ChunkGroupOptions & { entryOptions?: EntryOptions }) | string} GroupOptions */
  19. class AsyncDependenciesBlock extends DependenciesBlock {
  20. /**
  21. * @param {GroupOptions | 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. this.groupOptions = groupOptions;
  33. this.loc = loc;
  34. this.request = request;
  35. this._stringifiedGroupOptions = undefined;
  36. }
  37. /**
  38. * @returns {string | null | undefined} The name of the chunk
  39. */
  40. get chunkName() {
  41. return this.groupOptions.name;
  42. }
  43. /**
  44. * @param {string | undefined} value The new chunk name
  45. * @returns {void}
  46. */
  47. set chunkName(value) {
  48. if (this.groupOptions.name !== value) {
  49. this.groupOptions.name = value;
  50. this._stringifiedGroupOptions = undefined;
  51. }
  52. }
  53. /**
  54. * @param {Hash} hash the hash used to track dependencies
  55. * @param {UpdateHashContext} context context
  56. * @returns {void}
  57. */
  58. updateHash(hash, context) {
  59. const { chunkGraph } = context;
  60. if (this._stringifiedGroupOptions === undefined) {
  61. this._stringifiedGroupOptions = JSON.stringify(this.groupOptions);
  62. }
  63. const chunkGroup = chunkGraph.getBlockChunkGroup(this);
  64. hash.update(
  65. `${this._stringifiedGroupOptions}${chunkGroup ? chunkGroup.id : ""}`
  66. );
  67. super.updateHash(hash, context);
  68. }
  69. /**
  70. * @param {ObjectSerializerContext} context context
  71. */
  72. serialize(context) {
  73. const { write } = context;
  74. write(this.groupOptions);
  75. write(this.loc);
  76. write(this.request);
  77. super.serialize(context);
  78. }
  79. /**
  80. * @param {ObjectDeserializerContext} context context
  81. */
  82. deserialize(context) {
  83. const { read } = context;
  84. this.groupOptions = read();
  85. this.loc = read();
  86. this.request = read();
  87. super.deserialize(context);
  88. }
  89. }
  90. makeSerializable(AsyncDependenciesBlock, "webpack/lib/AsyncDependenciesBlock");
  91. Object.defineProperty(AsyncDependenciesBlock.prototype, "module", {
  92. get() {
  93. throw new Error(
  94. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  95. );
  96. },
  97. set() {
  98. throw new Error(
  99. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  100. );
  101. }
  102. });
  103. module.exports = AsyncDependenciesBlock;