AsyncDependenciesBlock.js 3.3 KB

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