1
1
/* global __resourceQuery, __webpack_hash__ */
2
2
/// <reference types="webpack/module" />
3
+
4
+ // @ts -expect-error
3
5
import hotEmitter from "webpack/hot/emitter.js" ;
6
+ // @ts -expect-error
4
7
import webpackHotLog from "webpack/hot/log.js" ;
5
8
import { createOverlay , formatProblem } from "./overlay.js" ;
6
9
import { defineProgressElement , isProgressSupported } from "./progress.js" ;
@@ -11,21 +14,31 @@ import sendMessage from "./utils/sendMessage.js";
11
14
// eslint-disable-next-line jsdoc/no-restricted-syntax
12
15
/** @typedef {any } EXPECTED_ANY */
13
16
17
+ /**
18
+ * @typedef {object } RawOverlayOptions
19
+ * @property {string= } warnings warnings
20
+ * @property {string= } errors errors
21
+ * @property {string= } runtimeErrors runtime errors
22
+ * @property {string= } trustedTypesPolicyName trusted types policy name
23
+ */
24
+
14
25
/**
15
26
* @typedef {object } OverlayOptions
16
- * @property {(boolean | (error: Error) => boolean)= } warnings warnings
17
- * @property {(boolean | (error: Error) => boolean)= } errors errors
18
- * @property {(boolean | (error: Error) => boolean)= } runtimeErrors runtime errors
27
+ * @property {(boolean | (( error: Error) => boolean) )= } warnings warnings
28
+ * @property {(boolean | (( error: Error) => boolean) )= } errors errors
29
+ * @property {(boolean | (( error: Error) => boolean) )= } runtimeErrors runtime errors
19
30
* @property {string= } trustedTypesPolicyName trusted types policy name
20
31
*/
21
32
33
+ /** @typedef {false | true | "none" | "error" | "warn" | "info" | "log" | "verbose" } LogLevel */
34
+
22
35
/**
23
36
* @typedef {object } Options
24
37
* @property {boolean } hot true when hot enabled, otherwise false
25
38
* @property {boolean } liveReload true when live reload enabled, otherwise false
26
39
* @property {boolean } progress true when need to show progress, otherwise false
27
40
* @property {boolean | OverlayOptions } overlay overlay options
28
- * @property {string = } logging logging level
41
+ * @property {LogLevel = } logging logging level
29
42
* @property {number= } reconnect count of allowed reconnection
30
43
*/
31
44
@@ -37,21 +50,28 @@ import sendMessage from "./utils/sendMessage.js";
37
50
*/
38
51
39
52
/**
40
- * @param {boolean | { warnings?: boolean | string, errors?: boolean | string, runtimeErrors?: boolean | string } } overlayOptions overlay options
53
+ * @param {boolean | RawOverlayOptions | OverlayOptions } overlayOptions overlay options
41
54
*/
42
55
const decodeOverlayOptions = ( overlayOptions ) => {
43
56
if ( typeof overlayOptions === "object" ) {
44
- for ( const property of [ "warnings" , "errors" , "runtimeErrors" ] ) {
57
+ for ( const property_ of [ "warnings" , "errors" , "runtimeErrors" ] ) {
58
+ const property =
59
+ /** @type {keyof Omit<RawOverlayOptions, "trustedTypesPolicyName"> } */
60
+ ( property_ ) ;
61
+
45
62
if ( typeof overlayOptions [ property ] === "string" ) {
46
63
const overlayFilterFunctionString = decodeURIComponent (
47
64
overlayOptions [ property ] ,
48
65
) ;
49
66
50
- // eslint-disable-next-line no-new-func
51
- overlayOptions [ property ] = new Function (
52
- "message" ,
53
- `var callback = ${ overlayFilterFunctionString }
67
+ /** @type {OverlayOptions } */
68
+ ( overlayOptions ) [ property ] = /** @type {(error: Error) => boolean } */ (
69
+ // eslint-disable-next-line no-new-func
70
+ new Function (
71
+ "message" ,
72
+ `var callback = ${ overlayFilterFunctionString }
54
73
return callback(message)` ,
74
+ )
55
75
) ;
56
76
}
57
77
}
@@ -73,7 +93,7 @@ const getCurrentScriptSource = () => {
73
93
// `document.currentScript` is the most accurate way to find the current script,
74
94
// but is not supported in all browsers.
75
95
if ( document . currentScript ) {
76
- return document . currentScript . getAttribute ( "src" ) ;
96
+ return /** @type { string } */ ( document . currentScript . getAttribute ( "src" ) ) ;
77
97
}
78
98
79
99
// Fallback to getting all scripts running in the document.
@@ -94,12 +114,15 @@ const getCurrentScriptSource = () => {
94
114
throw new Error ( "[webpack-dev-server] Failed to get current script source." ) ;
95
115
} ;
96
116
117
+ /** @typedef {{ hot?: string, ["live-reload"]?: string, progress?: string, reconnect?: string, logging?: LogLevel, overlay?: string, fromCurrentScript?: boolean } } AdditionalParsedURL */
118
+ /** @typedef {Partial<URL> & AdditionalParsedURL } ParsedURL */
119
+
97
120
/**
98
121
* @param {string } resourceQuery resource query
99
- * @returns {{ [key: string]: string | boolean } } parsed URL
122
+ * @returns {ParsedURL } parsed URL
100
123
*/
101
124
const parseURL = ( resourceQuery ) => {
102
- /** @type {{ [key: string]: string } } */
125
+ /** @type {ParsedURL } */
103
126
let result = { } ;
104
127
105
128
if ( typeof resourceQuery === "string" && resourceQuery !== "" ) {
@@ -108,7 +131,8 @@ const parseURL = (resourceQuery) => {
108
131
for ( let i = 0 ; i < searchParams . length ; i ++ ) {
109
132
const pair = searchParams [ i ] . split ( "=" ) ;
110
133
111
- result [ pair [ 0 ] ] = decodeURIComponent ( pair [ 1 ] ) ;
134
+ /** @type {EXPECTED_ANY } */
135
+ ( result ) [ pair [ 0 ] ] = decodeURIComponent ( pair [ 1 ] ) ;
112
136
}
113
137
} else {
114
138
// Else, get the url from the <script> this file was called with.
@@ -137,6 +161,9 @@ const parseURL = (resourceQuery) => {
137
161
138
162
const parsedResourceQuery = parseURL ( __resourceQuery ) ;
139
163
164
+ /** @typedef {{ ["Hot Module Replacement"]: boolean, ["Live Reloading"]: boolean, Progress: boolean, Overlay: boolean } } Features */
165
+
166
+ /** @type {Features } */
140
167
const enabledFeatures = {
141
168
"Hot Module Replacement" : false ,
142
169
"Live Reloading" : false ,
@@ -197,7 +224,7 @@ if (typeof parsedResourceQuery.reconnect !== "undefined") {
197
224
}
198
225
199
226
/**
200
- * @param {string } level level
227
+ * @param {false | true | "none" | "error" | "warn" | "info" | "log" | "verbose" } level level
201
228
*/
202
229
const setAllLogLevel = ( level ) => {
203
230
// This is needed because the HMR logger operate separately from dev server logger
@@ -211,6 +238,9 @@ if (options.logging) {
211
238
setAllLogLevel ( options . logging ) ;
212
239
}
213
240
241
+ /**
242
+ * @param {Features } features features
243
+ */
214
244
const logEnabledFeatures = ( features ) => {
215
245
const listEnabledFeatures = Object . keys ( features ) ;
216
246
if ( ! features || listEnabledFeatures . length === 0 ) {
@@ -221,7 +251,7 @@ const logEnabledFeatures = (features) => {
221
251
222
252
// Server started: Hot Module Replacement enabled, Live Reloading enabled, Overlay disabled.
223
253
for ( let i = 0 ; i < listEnabledFeatures . length ; i ++ ) {
224
- const key = listEnabledFeatures [ i ] ;
254
+ const key = /** @type { keyof Features } */ ( listEnabledFeatures [ i ] ) ;
225
255
logString += ` ${ key } ${ features [ key ] ? "enabled" : "disabled" } ,` ;
226
256
}
227
257
// replace last comma with a period
@@ -297,6 +327,7 @@ const reloadApp = ({ hot, liveReload }, currentStatus) => {
297
327
}
298
328
// allow refreshing the page only if liveReload isn't disabled
299
329
else if ( liveReload && allowToLiveReload ) {
330
+ /** @type {Window } */
300
331
let rootWindow = self ;
301
332
302
333
// use parent window for reload (in case we're in an iframe with no valid src)
@@ -400,7 +431,7 @@ const onSocketMessage = {
400
431
options . progress = value ;
401
432
} ,
402
433
/**
403
- * @param {{ pluginName?: string, percent: number , msg: string } } data date with progress
434
+ * @param {{ pluginName?: string, percent: string , msg: string } } data date with progress
404
435
*/
405
436
"progress-update" : function progressUpdate ( data ) {
406
437
if ( options . progress ) {
@@ -625,7 +656,7 @@ const formatURL = (objURL) => {
625
656
} ;
626
657
627
658
/**
628
- * @param {URL & { fromCurrentScript?: boolean } } parsedURL parsed URL
659
+ * @param {ParsedURL } parsedURL parsed URL
629
660
* @returns {string } socket URL
630
661
*/
631
662
const createSocketURL = ( parsedURL ) => {
0 commit comments