| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const DependenciesBlock = require("./DependenciesBlock");
|
|---|
| 9 | const makeSerializable = require("./util/makeSerializable");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
|
|---|
| 12 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
|---|
| 13 | /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 14 | /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
|
|---|
| 15 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 16 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 17 | /** @typedef {import("./util/Hash")} Hash */
|
|---|
| 18 |
|
|---|
| 19 | /** @typedef {(ChunkGroupOptions & { entryOptions?: EntryOptions } & { circular?: boolean })} GroupOptions */
|
|---|
| 20 |
|
|---|
| 21 | class AsyncDependenciesBlock extends DependenciesBlock {
|
|---|
| 22 | /**
|
|---|
| 23 | * @param {GroupOptions | string | null} groupOptions options for the group
|
|---|
| 24 | * @param {(DependencyLocation | null)=} loc the line of code
|
|---|
| 25 | * @param {(string | null)=} request the request
|
|---|
| 26 | */
|
|---|
| 27 | constructor(groupOptions, loc, request) {
|
|---|
| 28 | super();
|
|---|
| 29 | if (typeof groupOptions === "string") {
|
|---|
| 30 | groupOptions = { name: groupOptions };
|
|---|
| 31 | } else if (!groupOptions) {
|
|---|
| 32 | groupOptions = { name: undefined };
|
|---|
| 33 | }
|
|---|
| 34 | if (typeof groupOptions.circular !== "boolean") {
|
|---|
| 35 | // default allow circular references
|
|---|
| 36 | groupOptions.circular = true;
|
|---|
| 37 | }
|
|---|
| 38 | /** @type {GroupOptions} */
|
|---|
| 39 | this.groupOptions = groupOptions;
|
|---|
| 40 | /** @type {DependencyLocation | null | undefined} */
|
|---|
| 41 | this.loc = loc;
|
|---|
| 42 | /** @type {string | null | undefined} */
|
|---|
| 43 | this.request = request;
|
|---|
| 44 | /** @type {undefined | string} */
|
|---|
| 45 | this._stringifiedGroupOptions = undefined;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * @returns {ChunkGroupOptions["name"]} The name of the chunk
|
|---|
| 50 | */
|
|---|
| 51 | get chunkName() {
|
|---|
| 52 | return this.groupOptions.name;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * @param {string | undefined} value The new chunk name
|
|---|
| 57 | * @returns {void}
|
|---|
| 58 | */
|
|---|
| 59 | set chunkName(value) {
|
|---|
| 60 | if (this.groupOptions.name !== value) {
|
|---|
| 61 | this.groupOptions.name = value;
|
|---|
| 62 | this._stringifiedGroupOptions = undefined;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * @returns {boolean} Whether circular references are allowed
|
|---|
| 68 | */
|
|---|
| 69 | get circular() {
|
|---|
| 70 | return Boolean(this.groupOptions.circular);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Updates the hash with the data contributed by this instance.
|
|---|
| 75 | * @param {Hash} hash the hash used to track dependencies
|
|---|
| 76 | * @param {UpdateHashContext} context context
|
|---|
| 77 | * @returns {void}
|
|---|
| 78 | */
|
|---|
| 79 | updateHash(hash, context) {
|
|---|
| 80 | const { chunkGraph } = context;
|
|---|
| 81 | if (this._stringifiedGroupOptions === undefined) {
|
|---|
| 82 | this._stringifiedGroupOptions = JSON.stringify(this.groupOptions);
|
|---|
| 83 | }
|
|---|
| 84 | const chunkGroup = chunkGraph.getBlockChunkGroup(this);
|
|---|
| 85 | hash.update(
|
|---|
| 86 | `${this._stringifiedGroupOptions}${chunkGroup ? chunkGroup.id : ""}`
|
|---|
| 87 | );
|
|---|
| 88 | super.updateHash(hash, context);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Serializes this instance into the provided serializer context.
|
|---|
| 93 | * @param {ObjectSerializerContext} context context
|
|---|
| 94 | */
|
|---|
| 95 | serialize(context) {
|
|---|
| 96 | const { write } = context;
|
|---|
| 97 | write(this.groupOptions);
|
|---|
| 98 | write(this.loc);
|
|---|
| 99 | write(this.request);
|
|---|
| 100 | super.serialize(context);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Restores this instance from the provided deserializer context.
|
|---|
| 105 | * @param {ObjectDeserializerContext} context context
|
|---|
| 106 | */
|
|---|
| 107 | deserialize(context) {
|
|---|
| 108 | const { read } = context;
|
|---|
| 109 | this.groupOptions = read();
|
|---|
| 110 | this.loc = read();
|
|---|
| 111 | this.request = read();
|
|---|
| 112 | super.deserialize(context);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | makeSerializable(AsyncDependenciesBlock, "webpack/lib/AsyncDependenciesBlock");
|
|---|
| 117 |
|
|---|
| 118 | Object.defineProperty(AsyncDependenciesBlock.prototype, "module", {
|
|---|
| 119 | get() {
|
|---|
| 120 | throw new Error(
|
|---|
| 121 | "module property was removed from AsyncDependenciesBlock (it's not needed)"
|
|---|
| 122 | );
|
|---|
| 123 | },
|
|---|
| 124 | set() {
|
|---|
| 125 | throw new Error(
|
|---|
| 126 | "module property was removed from AsyncDependenciesBlock (it's not needed)"
|
|---|
| 127 | );
|
|---|
| 128 | }
|
|---|
| 129 | });
|
|---|
| 130 |
|
|---|
| 131 | module.exports = AsyncDependenciesBlock;
|
|---|