-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupHooks.js
More file actions
113 lines (92 loc) · 3.26 KB
/
setupHooks.js
File metadata and controls
113 lines (92 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/** @typedef {import("@rspack/core").Configuration} Configuration */
/** @typedef {import("@rspack/core").Compiler} Compiler */
/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */
/** @typedef {import("@rspack/core").Stats} Stats */
/** @typedef {import("@rspack/core").MultiStats} MultiStats */
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
/** @typedef {Configuration["stats"]} StatsOptions */
/** @typedef {{ children: Configuration["stats"][] }} MultiStatsOptions */
/** @typedef {Exclude<Configuration["stats"], boolean | string | undefined>} StatsObjectOptions */
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("../index.js").WithOptional<import("../index.js").Context<Request, Response>, "watching" | "outputFileSystem">} context context
*/
function setupHooks(context) {
/**
* @returns {void}
*/
function invalid() {
if (context.state) {
context.logger.log("Compilation starting...");
}
// We are now in invalid state
context.state = false;
context.stats = undefined;
}
/**
* @param {Stats | MultiStats} stats stats
*/
function done(stats) {
// We are now on valid state
context.state = true;
context.stats = stats;
// Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
process.nextTick(() => {
const { compiler, logger, options, state, callbacks } = context;
// Check if still in valid state
if (!state) {
return;
}
logger.log("Compilation finished");
const isMultiCompilerMode = Boolean(
/** @type {MultiCompiler} */
(compiler).compilers,
);
/**
* @type {StatsOptions | MultiStatsOptions | undefined}
*/
let statsOptions;
if (typeof options.stats !== "undefined") {
statsOptions = isMultiCompilerMode
? {
children:
/** @type {MultiCompiler} */
(compiler).compilers.map(() => options.stats),
}
: options.stats;
} else {
statsOptions = isMultiCompilerMode
? {
children:
/** @type {MultiCompiler} */
(compiler).compilers.map((child) => child.options.stats),
}
: /** @type {Compiler} */ (compiler).options.stats;
}
const printedStats = stats.toString(
/** @type {StatsObjectOptions} */
(statsOptions),
);
// Avoid extra empty line when `stats: 'none'`
if (printedStats) {
// eslint-disable-next-line no-console
console.log(printedStats);
}
context.callbacks = [];
// Execute callback that are delayed
for (const callback of callbacks) {
callback(stats);
}
});
}
// eslint-disable-next-line prefer-destructuring
const compiler =
/** @type {import("../index.js").Context<Request, Response>} */
(context).compiler;
compiler.hooks.watchRun.tap("webpack-dev-middleware", invalid);
compiler.hooks.invalid.tap("webpack-dev-middleware", invalid);
compiler.hooks.done.tap("webpack-dev-middleware", done);
}
module.exports = setupHooks;