提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.2 KiB

  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 }) | string} GroupOptions */
  16. class AsyncDependenciesBlock extends DependenciesBlock {
  17. /**
  18. * @param {GroupOptions | 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. this.groupOptions = groupOptions;
  30. this.loc = loc;
  31. this.request = request;
  32. this._stringifiedGroupOptions = undefined;
  33. }
  34. /**
  35. * @returns {ChunkGroupOptions["name"]} The name of the chunk
  36. */
  37. get chunkName() {
  38. return this.groupOptions.name;
  39. }
  40. /**
  41. * @param {string | undefined} 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. /**
  67. * @param {ObjectSerializerContext} context context
  68. */
  69. serialize(context) {
  70. const { write } = context;
  71. write(this.groupOptions);
  72. write(this.loc);
  73. write(this.request);
  74. super.serialize(context);
  75. }
  76. /**
  77. * @param {ObjectDeserializerContext} context context
  78. */
  79. deserialize(context) {
  80. const { read } = context;
  81. this.groupOptions = read();
  82. this.loc = read();
  83. this.request = read();
  84. super.deserialize(context);
  85. }
  86. }
  87. makeSerializable(AsyncDependenciesBlock, "webpack/lib/AsyncDependenciesBlock");
  88. Object.defineProperty(AsyncDependenciesBlock.prototype, "module", {
  89. get() {
  90. throw new Error(
  91. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  92. );
  93. },
  94. set() {
  95. throw new Error(
  96. "module property was removed from AsyncDependenciesBlock (it's not needed)"
  97. );
  98. }
  99. });
  100. module.exports = AsyncDependenciesBlock;