@@ -42,11 +42,12 @@ const noop = () => {};
42
42
*/
43
43
44
44
/**
45
- * @typedef {ReturnType<Compiler ["watch"]> } MultiWatching
45
+ * @typedef {ReturnType<MultiCompiler ["watch"]> } MultiWatching
46
46
*/
47
47
48
+ // TODO fix me after the next webpack release
48
49
/**
49
- * @typedef {Compiler["outputFileSystem"] & { createReadStream?: import("fs").createReadStream, statSync?: import("fs").statSync, lstat?: import("fs").lstat, readFileSync?: import("fs").readFileSync } } OutputFileSystem
50
+ * @typedef {Object & { createReadStream?: import("fs").createReadStream, statSync?: import("fs").statSync, lstat?: import("fs").lstat, readFileSync?: import("fs").readFileSync } } OutputFileSystem
50
51
*/
51
52
52
53
/** @typedef {ReturnType<Compiler["getInfrastructureLogger"]> } Logger */
@@ -82,15 +83,23 @@ const noop = () => {};
82
83
* @property {Callback[] } callbacks
83
84
* @property {Options<RequestInternal, ResponseInternal> } options
84
85
* @property {Compiler | MultiCompiler } compiler
85
- * @property {Watching | MultiWatching } watching
86
+ * @property {Watching | MultiWatching | undefined } watching
86
87
* @property {Logger } logger
87
88
* @property {OutputFileSystem } outputFileSystem
88
89
*/
89
90
90
91
/**
91
92
* @template {IncomingMessage} RequestInternal
92
93
* @template {ServerResponse} ResponseInternal
93
- * @typedef {Record<string, string | number> | Array<{ key: string, value: number | string }> | ((req: RequestInternal, res: ResponseInternal, context: Context<RequestInternal, ResponseInternal>) => void | undefined | Record<string, string | number>) | undefined } Headers
94
+ * @typedef {WithoutUndefined<Context<RequestInternal, ResponseInternal>, "watching"> } FilledContext
95
+ */
96
+
97
+ /** @typedef {Record<string, string | number> | Array<{ key: string, value: number | string }> } NormalizedHeaders */
98
+
99
+ /**
100
+ * @template {IncomingMessage} RequestInternal
101
+ * @template {ServerResponse} ResponseInternal
102
+ * @typedef {NormalizedHeaders | ((req: RequestInternal, res: ResponseInternal, context: Context<RequestInternal, ResponseInternal>) => void | undefined | NormalizedHeaders) | undefined } Headers
94
103
*/
95
104
96
105
/**
@@ -161,6 +170,18 @@ const noop = () => {};
161
170
* @typedef {Middleware<RequestInternal, ResponseInternal> & AdditionalMethods<RequestInternal, ResponseInternal> } API
162
171
*/
163
172
173
+ /**
174
+ * @template T
175
+ * @template {keyof T} K
176
+ * @typedef {Omit<T, K> & Partial<T> } WithOptional
177
+ */
178
+
179
+ /**
180
+ * @template T
181
+ * @template {keyof T} K
182
+ * @typedef {T & { [P in K]: NonNullable<T[P]> } } WithoutUndefined
183
+ */
184
+
164
185
/**
165
186
* @template {IncomingMessage} RequestInternal
166
187
* @template {ServerResponse} ResponseInternal
@@ -186,7 +207,7 @@ function wdm(compiler, options = {}) {
186
207
}
187
208
188
209
/**
189
- * @type {Context<RequestInternal, ResponseInternal> }
210
+ * @type {WithOptional< Context<RequestInternal, ResponseInternal>, "watching" | "outputFileSystem" > }
190
211
*/
191
212
const context = {
192
213
state : false ,
@@ -195,13 +216,7 @@ function wdm(compiler, options = {}) {
195
216
callbacks : [ ] ,
196
217
options,
197
218
compiler,
198
- // @ts -ignore
199
- // eslint-disable-next-line no-undefined
200
- watching : undefined ,
201
219
logger : compiler . getInfrastructureLogger ( "webpack-dev-middleware" ) ,
202
- // @ts -ignore
203
- // eslint-disable-next-line no-undefined
204
- outputFileSystem : undefined ,
205
220
} ;
206
221
207
222
setupHooks ( context ) ;
@@ -216,11 +231,6 @@ function wdm(compiler, options = {}) {
216
231
if ( /** @type {Compiler } */ ( context . compiler ) . watching ) {
217
232
context . watching = /** @type {Compiler } */ ( context . compiler ) . watching ;
218
233
} else {
219
- /**
220
- * @type {WatchOptions | WatchOptions[] }
221
- */
222
- let watchOptions ;
223
-
224
234
/**
225
235
* @param {Error | null | undefined } error
226
236
*/
@@ -237,69 +247,47 @@ function wdm(compiler, options = {}) {
237
247
if (
238
248
Array . isArray ( /** @type {MultiCompiler } */ ( context . compiler ) . compilers )
239
249
) {
240
- watchOptions =
241
- /** @type {MultiCompiler } */
242
- ( context . compiler ) . compilers . map (
243
- /**
244
- * @param {Compiler } childCompiler
245
- * @returns {WatchOptions }
246
- */
247
- ( childCompiler ) => childCompiler . options . watchOptions || { } ,
248
- ) ;
249
-
250
- context . watching =
251
- /** @type {MultiWatching } */
252
- (
253
- context . compiler . watch (
254
- /** @type {WatchOptions }} */
255
- ( watchOptions ) ,
256
- errorHandler ,
257
- )
258
- ) ;
250
+ const compiler = /** @type {MultiCompiler } */ ( context . compiler ) ;
251
+ const watchOptions = compiler . compilers . map (
252
+ ( childCompiler ) => childCompiler . options . watchOptions || { } ,
253
+ ) ;
254
+
255
+ context . watching = compiler . watch ( watchOptions , errorHandler ) ;
259
256
} else {
260
- watchOptions =
261
- /** @type { Compiler } */ ( context . compiler ) . options . watchOptions || { } ;
257
+ const compiler = /** @type { Compiler } */ ( context . compiler ) ;
258
+ const watchOptions = compiler . options . watchOptions || { } ;
262
259
263
- context . watching = /** @type {Watching } */ (
264
- context . compiler . watch ( watchOptions , errorHandler )
265
- ) ;
260
+ context . watching = compiler . watch ( watchOptions , errorHandler ) ;
266
261
}
267
262
}
268
263
264
+ const filledContext =
265
+ /** @type {FilledContext<RequestInternal, ResponseInternal> } */
266
+ ( context ) ;
267
+
269
268
const instance =
270
269
/** @type {API<RequestInternal, ResponseInternal> } */
271
- ( middleware ( context ) ) ;
270
+ ( middleware ( filledContext ) ) ;
272
271
273
272
// API
274
- /** @type {API<RequestInternal, ResponseInternal> } */
275
- ( instance ) . getFilenameFromUrl = ( url , extra ) =>
276
- getFilenameFromUrl ( context , url , extra ) ;
273
+ instance . getFilenameFromUrl = ( url , extra ) =>
274
+ getFilenameFromUrl ( filledContext , url , extra ) ;
277
275
278
- /** @type {API<RequestInternal, ResponseInternal> } */
279
- ( instance ) . waitUntilValid = ( callback = noop ) => {
280
- ready ( context , callback ) ;
276
+ instance . waitUntilValid = ( callback = noop ) => {
277
+ ready ( filledContext , callback ) ;
281
278
} ;
282
279
283
- /** @type {API<RequestInternal, ResponseInternal> } */
284
- ( instance ) . invalidate = ( callback = noop ) => {
285
- ready ( context , callback ) ;
280
+ instance . invalidate = ( callback = noop ) => {
281
+ ready ( filledContext , callback ) ;
286
282
287
- /**
288
- * @type {NonNullable<Context<RequestInternal, ResponseInternal>["watching"]> }
289
- */
290
- ( context . watching ) . invalidate ( ) ;
283
+ filledContext . watching . invalidate ( ) ;
291
284
} ;
292
285
293
- /** @type {API<RequestInternal, ResponseInternal> } */
294
- ( instance ) . close = ( callback = noop ) => {
295
- /**
296
- * @type {NonNullable<Context<RequestInternal, ResponseInternal>["watching"]> }
297
- */
298
- ( context . watching ) . close ( callback ) ;
286
+ instance . close = ( callback = noop ) => {
287
+ filledContext . watching . close ( callback ) ;
299
288
} ;
300
289
301
- /** @type {API<RequestInternal, ResponseInternal> } */
302
- ( instance ) . context = context ;
290
+ instance . context = filledContext ;
303
291
304
292
return instance ;
305
293
}
0 commit comments