@@ -42,11 +42,12 @@ const noop = () => {};
4242 */
4343
4444/**
45- * @typedef {ReturnType<Compiler ["watch"]> } MultiWatching
45+ * @typedef {ReturnType<MultiCompiler ["watch"]> } MultiWatching
4646 */
4747
48+ // TODO fix me after the next webpack release
4849/**
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
5051 */
5152
5253/** @typedef {ReturnType<Compiler["getInfrastructureLogger"]> } Logger */
@@ -82,15 +83,23 @@ const noop = () => {};
8283 * @property {Callback[] } callbacks
8384 * @property {Options<RequestInternal, ResponseInternal> } options
8485 * @property {Compiler | MultiCompiler } compiler
85- * @property {Watching | MultiWatching } watching
86+ * @property {Watching | MultiWatching | undefined } watching
8687 * @property {Logger } logger
8788 * @property {OutputFileSystem } outputFileSystem
8889 */
8990
9091/**
9192 * @template {IncomingMessage} RequestInternal
9293 * @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
94103 */
95104
96105/**
@@ -161,6 +170,18 @@ const noop = () => {};
161170 * @typedef {Middleware<RequestInternal, ResponseInternal> & AdditionalMethods<RequestInternal, ResponseInternal> } API
162171 */
163172
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+
164185/**
165186 * @template {IncomingMessage} RequestInternal
166187 * @template {ServerResponse} ResponseInternal
@@ -186,7 +207,7 @@ function wdm(compiler, options = {}) {
186207 }
187208
188209 /**
189- * @type {Context<RequestInternal, ResponseInternal> }
210+ * @type {WithOptional< Context<RequestInternal, ResponseInternal>, "watching" | "outputFileSystem" > }
190211 */
191212 const context = {
192213 state : false ,
@@ -195,13 +216,7 @@ function wdm(compiler, options = {}) {
195216 callbacks : [ ] ,
196217 options,
197218 compiler,
198- // @ts -ignore
199- // eslint-disable-next-line no-undefined
200- watching : undefined ,
201219 logger : compiler . getInfrastructureLogger ( "webpack-dev-middleware" ) ,
202- // @ts -ignore
203- // eslint-disable-next-line no-undefined
204- outputFileSystem : undefined ,
205220 } ;
206221
207222 setupHooks ( context ) ;
@@ -216,11 +231,6 @@ function wdm(compiler, options = {}) {
216231 if ( /** @type {Compiler } */ ( context . compiler ) . watching ) {
217232 context . watching = /** @type {Compiler } */ ( context . compiler ) . watching ;
218233 } else {
219- /**
220- * @type {WatchOptions | WatchOptions[] }
221- */
222- let watchOptions ;
223-
224234 /**
225235 * @param {Error | null | undefined } error
226236 */
@@ -237,69 +247,47 @@ function wdm(compiler, options = {}) {
237247 if (
238248 Array . isArray ( /** @type {MultiCompiler } */ ( context . compiler ) . compilers )
239249 ) {
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 ) ;
259256 } else {
260- watchOptions =
261- /** @type { Compiler } */ ( context . compiler ) . options . watchOptions || { } ;
257+ const compiler = /** @type { Compiler } */ ( context . compiler ) ;
258+ const watchOptions = compiler . options . watchOptions || { } ;
262259
263- context . watching = /** @type {Watching } */ (
264- context . compiler . watch ( watchOptions , errorHandler )
265- ) ;
260+ context . watching = compiler . watch ( watchOptions , errorHandler ) ;
266261 }
267262 }
268263
264+ const filledContext =
265+ /** @type {FilledContext<RequestInternal, ResponseInternal> } */
266+ ( context ) ;
267+
269268 const instance =
270269 /** @type {API<RequestInternal, ResponseInternal> } */
271- ( middleware ( context ) ) ;
270+ ( middleware ( filledContext ) ) ;
272271
273272 // 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 ) ;
277275
278- /** @type {API<RequestInternal, ResponseInternal> } */
279- ( instance ) . waitUntilValid = ( callback = noop ) => {
280- ready ( context , callback ) ;
276+ instance . waitUntilValid = ( callback = noop ) => {
277+ ready ( filledContext , callback ) ;
281278 } ;
282279
283- /** @type {API<RequestInternal, ResponseInternal> } */
284- ( instance ) . invalidate = ( callback = noop ) => {
285- ready ( context , callback ) ;
280+ instance . invalidate = ( callback = noop ) => {
281+ ready ( filledContext , callback ) ;
286282
287- /**
288- * @type {NonNullable<Context<RequestInternal, ResponseInternal>["watching"]> }
289- */
290- ( context . watching ) . invalidate ( ) ;
283+ filledContext . watching . invalidate ( ) ;
291284 } ;
292285
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 ) ;
299288 } ;
300289
301- /** @type {API<RequestInternal, ResponseInternal> } */
302- ( instance ) . context = context ;
290+ instance . context = filledContext ;
303291
304292 return instance ;
305293}
0 commit comments