UNPKG

2.93 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const DependenciesBlock = require("./DependenciesBlock");
9const makeSerializable = require("./util/makeSerializable");
10
11/** @typedef {import("./ChunkGraph")} ChunkGraph */
12/** @typedef {import("./ChunkGroup")} ChunkGroup */
13/** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
14/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
15/** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
16/** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
17/** @typedef {import("./Module")} Module */
18/** @typedef {import("./util/Hash")} Hash */
19
20class AsyncDependenciesBlock extends DependenciesBlock {
21 /**
22 * @param {ChunkGroupOptions & { entryOptions?: EntryOptions }} groupOptions options for the group
23 * @param {DependencyLocation=} loc the line of code
24 * @param {string=} request the request
25 */
26 constructor(groupOptions, loc, request) {
27 super();
28 if (typeof groupOptions === "string") {
29 groupOptions = { name: groupOptions };
30 } else if (!groupOptions) {
31 groupOptions = { name: undefined };
32 }
33 this.groupOptions = groupOptions;
34 this.loc = loc;
35 this.request = request;
36 this._stringifiedGroupOptions = undefined;
37 }
38
39 /**
40 * @returns {string} The name of the chunk
41 */
42 get chunkName() {
43 return this.groupOptions.name;
44 }
45
46 /**
47 * @param {string} value The new chunk name
48 * @returns {void}
49 */
50 set chunkName(value) {
51 if (this.groupOptions.name !== value) {
52 this.groupOptions.name = value;
53 this._stringifiedGroupOptions = undefined;
54 }
55 }
56
57 /**
58 * @param {Hash} hash the hash used to track dependencies
59 * @param {UpdateHashContext} context context
60 * @returns {void}
61 */
62 updateHash(hash, context) {
63 const { chunkGraph } = context;
64 if (this._stringifiedGroupOptions === undefined) {
65 this._stringifiedGroupOptions = JSON.stringify(this.groupOptions);
66 }
67 const chunkGroup = chunkGraph.getBlockChunkGroup(this);
68 hash.update(
69 `${this._stringifiedGroupOptions}${chunkGroup ? chunkGroup.id : ""}`
70 );
71 super.updateHash(hash, context);
72 }
73
74 serialize(context) {
75 const { write } = context;
76 write(this.groupOptions);
77 write(this.loc);
78 write(this.request);
79 super.serialize(context);
80 }
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
91makeSerializable(AsyncDependenciesBlock, "webpack/lib/AsyncDependenciesBlock");
92
93Object.defineProperty(AsyncDependenciesBlock.prototype, "module", {
94 get() {
95 throw new Error(
96 "module property was removed from AsyncDependenciesBlock (it's not needed)"
97 );
98 },
99 set() {
100 throw new Error(
101 "module property was removed from AsyncDependenciesBlock (it's not needed)"
102 );
103 }
104});
105
106module.exports = AsyncDependenciesBlock;