Skip to content

Commit 8f02c3f

Browse files
feat: added types
1 parent f4fb15f commit 8f02c3f

25 files changed

+6500
-655
lines changed

.github/workflows/nodejs.yml

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ jobs:
8383
if: matrix.os == 'windows-latest'
8484
run: npm i -g npm
8585

86+
- name: Use latest NPM on windows
87+
if: matrix.webpack-version == 4
88+
run: sed -i'.original' 's/"build:types"/"_unused"/g' package.json
89+
8690
- name: Install dependencies
8791
run: npm ci
8892

bin/process-arguments.js

+87-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,50 @@ const path = require("path");
55
// Based on https://github.com/webpack/webpack/blob/master/lib/cli.js
66
// Please do not modify it
77

8+
/** @typedef {"unknown-argument" | "unexpected-non-array-in-path" | "unexpected-non-object-in-path" | "multiple-values-unexpected" | "invalid-value"} ProblemType */
9+
10+
/**
11+
* @typedef {Object} Problem
12+
* @property {ProblemType} type
13+
* @property {string} path
14+
* @property {string} argument
15+
* @property {any=} value
16+
* @property {number=} index
17+
* @property {string=} expected
18+
*/
19+
20+
/**
21+
* @typedef {Object} LocalProblem
22+
* @property {ProblemType} type
23+
* @property {string} path
24+
* @property {string=} expected
25+
*/
26+
27+
/**
28+
* @typedef {Object} ArgumentConfig
29+
* @property {string} description
30+
* @property {string} path
31+
* @property {boolean} multiple
32+
* @property {"enum"|"string"|"path"|"number"|"boolean"|"RegExp"|"reset"} type
33+
* @property {any[]=} values
34+
*/
35+
36+
/**
37+
* @typedef {Object} Argument
38+
* @property {string} description
39+
* @property {"string"|"number"|"boolean"} simpleType
40+
* @property {boolean} multiple
41+
* @property {ArgumentConfig[]} configs
42+
*/
43+
844
const cliAddedItems = new WeakMap();
945

46+
/**
47+
* @param {any} config configuration
48+
* @param {string} schemaPath path in the config
49+
* @param {number | undefined} index index of value when multiple values are provided, otherwise undefined
50+
* @returns {{ problem?: LocalProblem, object?: any, property?: string | number, value?: any }} problem or object with property and value
51+
*/
1052
const getObjectAndProperty = (config, schemaPath, index = 0) => {
1153
if (!schemaPath) {
1254
return { value: config };
@@ -81,10 +123,10 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
81123
i++;
82124
}
83125

84-
const value = current[property];
126+
const value = current[/** @type {string} */ (property)];
85127

86-
if (property.endsWith("[]")) {
87-
const name = property.slice(0, -2);
128+
if (/** @type {string} */ (property).endsWith("[]")) {
129+
const name = /** @type {string} */ (property).slice(0, -2);
88130
// eslint-disable-next-line no-shadow
89131
const value = current[name];
90132

@@ -140,6 +182,11 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
140182
return { object: current, property, value };
141183
};
142184

185+
/**
186+
* @param {ArgumentConfig} argConfig processing instructions
187+
* @param {any} value the value
188+
* @returns {any | undefined} parsed value
189+
*/
143190
const parseValueForArgumentConfig = (argConfig, value) => {
144191
// eslint-disable-next-line default-case
145192
switch (argConfig.type) {
@@ -194,11 +241,11 @@ const parseValueForArgumentConfig = (argConfig, value) => {
194241

195242
break;
196243
case "enum":
197-
if (argConfig.values.includes(value)) {
244+
if (/** @type {any[]} */ (argConfig.values).includes(value)) {
198245
return value;
199246
}
200247

201-
for (const item of argConfig.values) {
248+
for (const item of /** @type {any[]} */ (argConfig.values)) {
202249
if (`${item}` === value) return item;
203250
}
204251

@@ -212,6 +259,10 @@ const parseValueForArgumentConfig = (argConfig, value) => {
212259
}
213260
};
214261

262+
/**
263+
* @param {ArgumentConfig} argConfig processing instructions
264+
* @returns {string | undefined} expected message
265+
*/
215266
const getExpectedValue = (argConfig) => {
216267
switch (argConfig.type) {
217268
default:
@@ -221,12 +272,21 @@ const getExpectedValue = (argConfig) => {
221272
case "RegExp":
222273
return "regular expression (example: /ab?c*/)";
223274
case "enum":
224-
return argConfig.values.map((v) => `${v}`).join(" | ");
275+
return /** @type {any[]} */ (argConfig.values)
276+
.map((v) => `${v}`)
277+
.join(" | ");
225278
case "reset":
226279
return "true (will reset the previous value to an empty array)";
227280
}
228281
};
229282

283+
/**
284+
* @param {any} config configuration
285+
* @param {string} schemaPath path in the config
286+
* @param {any} value parsed value
287+
* @param {number | undefined} index index of value when multiple values are provided, otherwise undefined
288+
* @returns {LocalProblem | null} problem or null for success
289+
*/
230290
const setValue = (config, schemaPath, value, index) => {
231291
const { problem, object, property } = getObjectAndProperty(
232292
config,
@@ -238,11 +298,18 @@ const setValue = (config, schemaPath, value, index) => {
238298
return problem;
239299
}
240300

241-
object[property] = value;
301+
object[/** @type {string} */ (property)] = value;
242302

243303
return null;
244304
};
245305

306+
/**
307+
* @param {ArgumentConfig} argConfig processing instructions
308+
* @param {any} config configuration
309+
* @param {any} value the value
310+
* @param {number | undefined} index the index if multiple values provided
311+
* @returns {LocalProblem | null} a problem if any
312+
*/
246313
const processArgumentConfig = (argConfig, config, value, index) => {
247314
// eslint-disable-next-line no-undefined
248315
if (index !== undefined && !argConfig.multiple) {
@@ -272,7 +339,16 @@ const processArgumentConfig = (argConfig, config, value, index) => {
272339
return null;
273340
};
274341

342+
/**
343+
* @param {Record<string, Argument>} args object of arguments
344+
* @param {any} config configuration
345+
* @param {Record<string, string | number | boolean | RegExp | (string | number | boolean | RegExp)[]>} values object with values
346+
* @returns {Problem[] | null} problems or null for success
347+
*/
275348
const processArguments = (args, config, values) => {
349+
/**
350+
* @type {Problem[]}
351+
*/
276352
const problems = [];
277353

278354
for (const key of Object.keys(values)) {
@@ -289,6 +365,10 @@ const processArguments = (args, config, values) => {
289365
continue;
290366
}
291367

368+
/**
369+
* @param {any} value
370+
* @param {number | undefined} i
371+
*/
292372
const processValue = (value, i) => {
293373
const currentProblems = [];
294374

bin/webpack-dev-server.js

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ if (!cli.installed) {
108108

109109
console.error(notify);
110110

111+
/**
112+
* @type {string}
113+
*/
111114
let packageManager;
112115

113116
if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) {

client-src/clients/SockJSClient.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,41 @@ import SockJS from "../modules/sockjs-client/index.js";
22
import { log } from "../utils/log.js";
33

44
export default class SockJSClient {
5+
/**
6+
* @param {string} url
7+
*/
58
constructor(url) {
69
// SockJS requires `http` and `https` protocols
710
this.sock = new SockJS(
811
url.replace(/^ws:/i, "http:").replace(/^wss:/i, "https:")
912
);
10-
this.sock.onerror = (error) => {
11-
log.error(error);
12-
};
13+
this.sock.onerror =
14+
/**
15+
* @param {Error} error
16+
*/
17+
(error) => {
18+
log.error(error);
19+
};
1320
}
1421

22+
/**
23+
* @param {(...args: any[]) => void} f
24+
*/
1525
onOpen(f) {
1626
this.sock.onopen = f;
1727
}
1828

29+
/**
30+
* @param {(...args: any[]) => void} f
31+
*/
1932
onClose(f) {
2033
this.sock.onclose = f;
2134
}
2235

2336
// call f with the message string as the first argument
37+
/**
38+
* @param {(...args: any[]) => void} f
39+
*/
2440
onMessage(f) {
2541
this.sock.onmessage = (e) => {
2642
f(e.data);

client-src/clients/WebSocketClient.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
import { log } from "../utils/log.js";
22

33
export default class WebSocketClient {
4+
/**
5+
* @param {string} url
6+
*/
47
constructor(url) {
58
this.client = new WebSocket(url);
69
this.client.onerror = (error) => {
710
log.error(error);
811
};
912
}
1013

14+
/**
15+
* @param {(...args: any[]) => void} f
16+
*/
1117
onOpen(f) {
1218
this.client.onopen = f;
1319
}
1420

21+
/**
22+
* @param {(...args: any[]) => void} f
23+
*/
1524
onClose(f) {
1625
this.client.onclose = f;
1726
}
1827

1928
// call f with the message string as the first argument
29+
/**
30+
* @param {(...args: any[]) => void} f
31+
*/
2032
onMessage(f) {
2133
this.client.onmessage = (e) => {
2234
f(e.data);

client-src/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ if (typeof parsedResourceQuery.reconnect !== "undefined") {
4545
options.reconnect = Number(parsedResourceQuery.reconnect);
4646
}
4747

48+
/**
49+
* @param {string} level
50+
*/
4851
function setAllLogLevel(level) {
4952
// This is needed because the HMR logger operate separately from dev server logger
5053
webpackHotLog.setLogLevel(
@@ -90,6 +93,9 @@ const onSocketMessage = {
9093

9194
sendMessage("Invalid");
9295
},
96+
/**
97+
* @param {string} hash
98+
*/
9399
hash(hash) {
94100
status.previousHash = status.currentHash;
95101
status.currentHash = hash;
@@ -160,6 +166,10 @@ const onSocketMessage = {
160166

161167
self.location.reload();
162168
},
169+
/**
170+
* @param {Error[]} warnings
171+
* @param {any} params
172+
*/
163173
warnings(warnings, params) {
164174
log.warn("Warnings while compiling.");
165175

@@ -190,6 +200,9 @@ const onSocketMessage = {
190200

191201
reloadApp(options, status);
192202
},
203+
/**
204+
* @param {Error[]} errors
205+
*/
193206
errors(errors) {
194207
log.error("Errors while compiling. Reload prevented.");
195208

@@ -214,6 +227,9 @@ const onSocketMessage = {
214227
show("error", errors);
215228
}
216229
},
230+
/**
231+
* @param {Error} error
232+
*/
217233
error(error) {
218234
log.error(error);
219235
},

client-src/utils/log.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const name = "webpack-dev-server";
55
// to be set by the CLI or API
66
const defaultLevel = "info";
77

8+
// options new options, merge with old options
9+
/**
10+
* @param {false | true | "none" | "error" | "warn" | "info" | "log" | "verbose"} level
11+
* @returns {void}
12+
*/
813
function setLogLevel(level) {
914
logger.configureDefaultLogger({ level });
1015
}

client-src/utils/sendMessage.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/* global __resourceQuery WorkerGlobalScope */
22

33
// Send messages to the outside, so plugins can consume it.
4+
/**
5+
* @param {string} type
6+
* @param {any} data
7+
*/
48
function sendMsg(type, data) {
59
if (
610
typeof self !== "undefined" &&

client-src/webpack.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require("path");
44
const webpack = require("webpack");
55
const { merge } = require("webpack-merge");
66

7+
// @ts-ignore
78
const library = webpack.webpack
89
? {
910
library: {
@@ -27,6 +28,7 @@ const baseForModules = {
2728
optimization: {
2829
minimize: false,
2930
},
31+
// @ts-ignore
3032
target: webpack.webpack ? ["web", "es5"] : "web",
3133
module: {
3234
rules: [

0 commit comments

Comments
 (0)