AsyncDependenciesBlock.js 2.9 KB

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