22/** @typedef {import("webpack").MultiCompiler } MultiCompiler */
33/** @typedef {import("webpack").Stats } Stats */
44/** @typedef {import("webpack").MultiStats } MultiStats */
5+ /** @typedef {import("webpack").StatsCompilation } StatsCompilation */
6+ /** @typedef {import("webpack").StatsError } StatsError */
7+ /** @typedef {import("webpack").StatsModule } StatsModule */
58/** @typedef {import("./index.js").IncomingMessage } IncomingMessage */
69/** @typedef {import("./index.js").ServerResponse } ServerResponse */
710
8- // eslint-disable-next-line jsdoc/reject-any-type
9- /** @typedef {any } EXPECTED_ANY */
11+ /** @typedef {NonNullable<import("webpack").Configuration["stats"]> } StatsOptions */
1012
1113/**
1214 * @typedef {object } HotOptions
1315 * @property {string= } path the path the SSE endpoint is served at
1416 * @property {number= } heartbeat heartbeat interval in milliseconds
1517 * @property {((message: string) => void) | false= } log logger
16- * @property {EXPECTED_ANY = } statsOptions webpack stats options used when serializing compilation results
18+ * @property {StatsOptions = } statsOptions webpack stats options used when serializing compilation results
1719 */
1820
1921/**
@@ -78,16 +80,15 @@ function createEventStream(heartbeat) {
7880 } , heartbeat ) ;
7981
8082 // Don't block process exit on the heartbeat timer.
81- if ( typeof ( /** @type {EXPECTED_ANY } */ ( interval ) . unref ) === "function" ) {
82- /** @type {EXPECTED_ANY } */
83- ( interval ) . unref ( ) ;
83+ if ( typeof interval . unref === "function" ) {
84+ interval . unref ( ) ;
8485 }
8586
8687 return {
8788 close ( ) {
8889 clearInterval ( interval ) ;
8990 everyClient ( ( client ) => {
90- if ( ! ( /** @type { EXPECTED_ANY } */ ( client ) . writableEnded ) ) {
91+ if ( ! client . writableEnded ) {
9192 client . end ( ) ;
9293 }
9394 } ) ;
@@ -104,7 +105,7 @@ function createEventStream(heartbeat) {
104105 "X-Accel-Buffering" : "no" ,
105106 } ;
106107
107- const { httpVersion, socket } = /** @type { EXPECTED_ANY } */ ( req ) ;
108+ const { httpVersion, socket } = req ;
108109 const isHttp1 = ! ( Number . parseInt ( httpVersion , 10 ) >= 2 ) ;
109110
110111 if ( isHttp1 ) {
@@ -121,7 +122,7 @@ function createEventStream(heartbeat) {
121122 clients . set ( id , res ) ;
122123
123124 req . on ( "close" , ( ) => {
124- if ( ! ( /** @type { EXPECTED_ANY } */ ( res ) . writableEnded ) ) {
125+ if ( ! res . writableEnded ) {
125126 res . end ( ) ;
126127 }
127128 clients . delete ( id ) ;
@@ -136,7 +137,7 @@ function createEventStream(heartbeat) {
136137}
137138
138139/**
139- * @param {EXPECTED_ANY [] } errors errors or warnings
140+ * @param {(string | StatsError) [] } errors errors or warnings
140141 * @returns {string[] } flat strings
141142 */
142143function formatErrors ( errors ) {
@@ -148,7 +149,7 @@ function formatErrors(errors) {
148149 return /** @type {string[] } */ ( errors ) ;
149150 }
150151
151- return errors . map ( ( error ) => {
152+ return /** @type { StatsError[] } */ ( errors ) . map ( ( error ) => {
152153 const moduleName = error . moduleName || "" ;
153154 const loc = error . loc || "" ;
154155
@@ -157,9 +158,9 @@ function formatErrors(errors) {
157158}
158159
159160/**
160- * @param {EXPECTED_ANY } stats stats
161- * @param {EXPECTED_ANY } statsOptions stats options
162- * @returns {EXPECTED_ANY } json stats with compilation reference attached
161+ * @param {Stats } stats stats
162+ * @param {StatsOptions } statsOptions stats options
163+ * @returns {StatsCompilation } json stats with compilation reference attached
163164 */
164165function normalizeStats ( stats , statsOptions ) {
165166 const statsJson = stats . toJson ( statsOptions ) ;
@@ -172,8 +173,8 @@ function normalizeStats(stats, statsOptions) {
172173}
173174
174175/**
175- * @param {EXPECTED_ANY } stats normalized stats
176- * @returns {EXPECTED_ANY [] } extracted bundles
176+ * @param {StatsCompilation } stats normalized stats
177+ * @returns {StatsCompilation [] } extracted bundles
177178 */
178179function extractBundles ( stats ) {
179180 if ( stats . modules ) {
@@ -188,15 +189,17 @@ function extractBundles(stats) {
188189}
189190
190191/**
191- * @param {EXPECTED_ANY [] } modules modules
192+ * @param {StatsModule [] } modules modules
192193 * @returns {Record<string, string> } module id to name map
193194 */
194195function buildModuleMap ( modules ) {
195196 /** @type {Record<string, string> } */
196197 const map = { } ;
197198
198199 for ( const item of modules ) {
199- map [ item . id ] = item . name ;
200+ map [ /** @type {string | number } */ ( item . id ) ] = /** @type {string } */ (
201+ item . name
202+ ) ;
200203 }
201204
202205 return map ;
@@ -207,7 +210,7 @@ function buildModuleMap(modules) {
207210 * @param {Stats | MultiStats } statsResult stats result
208211 * @param {EventStream } eventStream event stream
209212 * @param {((message: string) => void) | false } log logger or false to disable
210- * @param {EXPECTED_ANY } statsOptions stats options
213+ * @param {StatsOptions | undefined } statsOptions stats options
211214 */
212215function publishStats ( action , statsResult , eventStream , log , statsOptions ) {
213216 const resultStatsOptions = {
@@ -222,17 +225,13 @@ function publishStats(action, statsResult, eventStream, log, statsOptions) {
222225 ...( statsOptions && typeof statsOptions === "object" ? statsOptions : { } ) ,
223226 } ;
224227
225- /** @type {EXPECTED_ANY [] } */
226- let bundles = [ ] ;
228+ /** @type {StatsCompilation [] } */
229+ let bundles ;
227230
228231 // Multi-compiler stats have stats for each child compiler.
229- if ( /** @type {EXPECTED_ANY } */ ( statsResult ) . stats ) {
230- bundles = /** @type {EXPECTED_ANY } */ ( statsResult ) . stats . flatMap (
231- /**
232- * @param {EXPECTED_ANY } stats stats
233- * @returns {EXPECTED_ANY[] } extracted bundles
234- */
235- ( stats ) => extractBundles ( normalizeStats ( stats , resultStatsOptions ) ) ,
232+ if ( "stats" in statsResult ) {
233+ bundles = statsResult . stats . flatMap ( ( stats ) =>
234+ extractBundles ( normalizeStats ( stats , resultStatsOptions ) ) ,
236235 ) ;
237236 } else {
238237 bundles = extractBundles ( normalizeStats ( statsResult , resultStatsOptions ) ) ;
@@ -259,7 +258,7 @@ function publishStats(action, statsResult, eventStream, log, statsOptions) {
259258 hash : stats . hash ,
260259 warnings : formatErrors ( stats . warnings || [ ] ) ,
261260 errors : formatErrors ( stats . errors || [ ] ) ,
262- modules : buildModuleMap ( stats . modules ) ,
261+ modules : buildModuleMap ( stats . modules || [ ] ) ,
263262 } ) ;
264263 }
265264}
0 commit comments