Skip to content

Commit d3fa033

Browse files
authored
refactor: extract types and convert logger modules to TypeScript (#70)
1 parent 3c6140a commit d3fa033

File tree

18 files changed

+532
-644
lines changed

18 files changed

+532
-644
lines changed

client-src/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,8 @@ const formatURL = (objURL: {
585585
hash = `#${hash}`;
586586
}
587587

588-
pathname = pathname.replace(
589-
/[?#]/g,
590-
/**
591-
* @param {string} match
592-
* @returns {string}
593-
*/
594-
(match) => encodeURIComponent(match),
588+
pathname = pathname.replace(/[?#]/g, (match: string): string =>
589+
encodeURIComponent(match),
595590
);
596591
search = search.replace('#', '%23');
597592

Lines changed: 36 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,54 @@
88
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
99
*/
1010

11-
// @ts-nocheck
12-
13-
'use strict';
14-
15-
const LogType = Object.freeze({
16-
error: /** @type {"error"} */ ('error'), // message, c style arguments
17-
warn: /** @type {"warn"} */ ('warn'), // message, c style arguments
18-
info: /** @type {"info"} */ ('info'), // message, c style arguments
19-
log: /** @type {"log"} */ ('log'), // message, c style arguments
20-
debug: /** @type {"debug"} */ ('debug'), // message, c style arguments
21-
22-
trace: /** @type {"trace"} */ ('trace'), // no arguments
23-
24-
group: /** @type {"group"} */ ('group'), // [label]
25-
groupCollapsed: /** @type {"groupCollapsed"} */ ('groupCollapsed'), // [label]
26-
groupEnd: /** @type {"groupEnd"} */ ('groupEnd'), // [label]
27-
28-
profile: /** @type {"profile"} */ ('profile'), // [profileName]
29-
profileEnd: /** @type {"profileEnd"} */ ('profileEnd'), // [profileName]
30-
31-
time: /** @type {"time"} */ ('time'), // name, time as [seconds, nanoseconds]
32-
33-
clear: /** @type {"clear"} */ ('clear'), // no arguments
34-
status: /** @type {"status"} */ ('status'), // message, arguments
35-
});
36-
37-
module.exports.LogType = LogType;
38-
39-
/** @typedef {typeof LogType[keyof typeof LogType]} LogTypeEnum */
40-
/** @typedef {Map<string | undefined, [number, number]>} TimersMap */
11+
import {
12+
LogType,
13+
type Args,
14+
type EXPECTED_ANY,
15+
type LogTypeEnum,
16+
type TimersMap,
17+
} from '../types';
4118

4219
const LOG_SYMBOL = Symbol('webpack logger raw log method');
4320
const TIMERS_SYMBOL = Symbol('webpack logger times');
4421
const TIMERS_AGGREGATES_SYMBOL = Symbol('webpack logger aggregated times');
4522

46-
/** @typedef {EXPECTED_ANY[]} Args */
47-
4823
class WebpackLogger {
49-
/**
50-
* @param {(type: LogTypeEnum, args?: Args) => void} log log function
51-
* @param {(name: string | (() => string)) => WebpackLogger} getChildLogger function to create child logger
52-
*/
53-
constructor(log, getChildLogger) {
24+
private [LOG_SYMBOL]: (type: LogTypeEnum, args?: Args) => void;
25+
private [TIMERS_SYMBOL]: TimersMap = new Map();
26+
private [TIMERS_AGGREGATES_SYMBOL]: TimersMap = new Map();
27+
// @ts-ignore
28+
private getChildLogger: (name: string | (() => string)) => WebpackLogger;
29+
30+
constructor(
31+
log: (type: LogTypeEnum, args?: Args) => void,
32+
getChildLogger: (name: string | (() => string)) => WebpackLogger,
33+
) {
5434
this[LOG_SYMBOL] = log;
5535
this.getChildLogger = getChildLogger;
5636
}
5737

58-
/**
59-
* @param {Args} args args
60-
*/
61-
error(...args) {
38+
error(...args: Args) {
6239
this[LOG_SYMBOL](LogType.error, args);
6340
}
6441

65-
/**
66-
* @param {Args} args args
67-
*/
68-
warn(...args) {
42+
warn(...args: Args) {
6943
this[LOG_SYMBOL](LogType.warn, args);
7044
}
7145

72-
/**
73-
* @param {Args} args args
74-
*/
75-
info(...args) {
46+
info(...args: Args) {
7647
this[LOG_SYMBOL](LogType.info, args);
7748
}
7849

79-
/**
80-
* @param {Args} args args
81-
*/
82-
log(...args) {
50+
log(...args: Args) {
8351
this[LOG_SYMBOL](LogType.log, args);
8452
}
8553

86-
/**
87-
* @param {Args} args args
88-
*/
89-
debug(...args) {
54+
debug(...args: Args) {
9055
this[LOG_SYMBOL](LogType.debug, args);
9156
}
9257

93-
/**
94-
* @param {EXPECTED_ANY} assertion assertion
95-
* @param {Args} args args
96-
*/
97-
assert(assertion, ...args) {
58+
assert(assertion: EXPECTED_ANY, ...args: Args) {
9859
if (!assertion) {
9960
this[LOG_SYMBOL](LogType.error, args);
10061
}
@@ -108,58 +69,36 @@ class WebpackLogger {
10869
this[LOG_SYMBOL](LogType.clear);
10970
}
11071

111-
/**
112-
* @param {Args} args args
113-
*/
114-
status(...args) {
72+
status(...args: Args) {
11573
this[LOG_SYMBOL](LogType.status, args);
11674
}
11775

118-
/**
119-
* @param {Args} args args
120-
*/
121-
group(...args) {
76+
group(...args: Args) {
12277
this[LOG_SYMBOL](LogType.group, args);
12378
}
12479

125-
/**
126-
* @param {Args} args args
127-
*/
128-
groupCollapsed(...args) {
80+
groupCollapsed(...args: Args) {
12981
this[LOG_SYMBOL](LogType.groupCollapsed, args);
13082
}
13183

13284
groupEnd() {
13385
this[LOG_SYMBOL](LogType.groupEnd);
13486
}
13587

136-
/**
137-
* @param {string=} label label
138-
*/
139-
profile(label) {
88+
profile(label?: string) {
14089
this[LOG_SYMBOL](LogType.profile, [label]);
14190
}
14291

143-
/**
144-
* @param {string=} label label
145-
*/
146-
profileEnd(label) {
92+
profileEnd(label?: string) {
14793
this[LOG_SYMBOL](LogType.profileEnd, [label]);
14894
}
14995

150-
/**
151-
* @param {string} label label
152-
*/
153-
time(label) {
154-
/** @type {TimersMap} */
96+
time(label: string) {
15597
this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
15698
this[TIMERS_SYMBOL].set(label, process.hrtime());
15799
}
158100

159-
/**
160-
* @param {string=} label label
161-
*/
162-
timeLog(label) {
101+
timeLog(label?: string) {
163102
const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
164103
if (!prev) {
165104
throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
@@ -168,34 +107,26 @@ class WebpackLogger {
168107
this[LOG_SYMBOL](LogType.time, [label, ...time]);
169108
}
170109

171-
/**
172-
* @param {string=} label label
173-
*/
174-
timeEnd(label) {
110+
timeEnd(label?: string) {
175111
const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
176112
if (!prev) {
177113
throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
178114
}
179115
const time = process.hrtime(prev);
180116
/** @type {TimersMap} */
181-
(this[TIMERS_SYMBOL]).delete(label);
117+
this[TIMERS_SYMBOL].delete(label);
182118
this[LOG_SYMBOL](LogType.time, [label, ...time]);
183119
}
184120

185-
/**
186-
* @param {string=} label label
187-
*/
188-
timeAggregate(label) {
121+
timeAggregate(label?: string) {
189122
const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
190123
if (!prev) {
191124
throw new Error(
192125
`No such label '${label}' for WebpackLogger.timeAggregate()`,
193126
);
194127
}
195128
const time = process.hrtime(prev);
196-
/** @type {TimersMap} */
197-
(this[TIMERS_SYMBOL]).delete(label);
198-
/** @type {TimersMap} */
129+
this[TIMERS_SYMBOL].delete(label);
199130
this[TIMERS_AGGREGATES_SYMBOL] =
200131
this[TIMERS_AGGREGATES_SYMBOL] || new Map();
201132
const current = this[TIMERS_AGGREGATES_SYMBOL].get(label);
@@ -211,10 +142,7 @@ class WebpackLogger {
211142
this[TIMERS_AGGREGATES_SYMBOL].set(label, time);
212143
}
213144

214-
/**
215-
* @param {string=} label label
216-
*/
217-
timeAggregateEnd(label) {
145+
timeAggregateEnd(label?: string) {
218146
if (this[TIMERS_AGGREGATES_SYMBOL] === undefined) return;
219147
const time = this[TIMERS_AGGREGATES_SYMBOL].get(label);
220148
if (time === undefined) return;
@@ -223,4 +151,4 @@ class WebpackLogger {
223151
}
224152
}
225153

226-
module.exports.Logger = WebpackLogger;
154+
export { WebpackLogger as Logger };

client-src/modules/logger/createConsoleLogger.js renamed to client-src/modules/logger/createConsoleLogger.ts

Lines changed: 25 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,19 @@
88
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
99
*/
1010

11-
// @ts-nocheck
12-
/*
13-
MIT License http://www.opensource.org/licenses/mit-license.php
14-
Author Tobias Koppers @sokra
15-
*/
11+
import {
12+
LogType,
13+
type Args,
14+
type FilterFunction,
15+
type FilterItemTypes,
16+
type LoggerOptions,
17+
type LoggingFunction,
18+
type LogTypeEnum,
19+
} from '../types';
1620

17-
'use strict';
18-
19-
const { LogType } = require('./Logger');
20-
21-
/** @typedef {import("../../declarations/WebpackOptions").FilterItemTypes} FilterItemTypes */
22-
/** @typedef {import("../../declarations/WebpackOptions").FilterTypes} FilterTypes */
23-
/** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
24-
/** @typedef {import("./Logger").Args} Args */
25-
26-
/** @typedef {(item: string) => boolean} FilterFunction */
27-
/** @typedef {(value: string, type: LogTypeEnum, args?: Args) => void} LoggingFunction */
28-
29-
/**
30-
* @typedef {object} LoggerConsole
31-
* @property {() => void} clear
32-
* @property {() => void} trace
33-
* @property {(...args: Args) => void} info
34-
* @property {(...args: Args) => void} log
35-
* @property {(...args: Args) => void} warn
36-
* @property {(...args: Args) => void} error
37-
* @property {(...args: Args) => void=} debug
38-
* @property {(...args: Args) => void=} group
39-
* @property {(...args: Args) => void=} groupCollapsed
40-
* @property {(...args: Args) => void=} groupEnd
41-
* @property {(...args: Args) => void=} status
42-
* @property {(...args: Args) => void=} profile
43-
* @property {(...args: Args) => void=} profileEnd
44-
* @property {(...args: Args) => void=} logTime
45-
*/
46-
47-
/**
48-
* @typedef {object} LoggerOptions
49-
* @property {false | true | "none" | "error" | "warn" | "info" | "log" | "verbose"} level loglevel
50-
* @property {FilterTypes | boolean} debug filter for debug logging
51-
* @property {LoggerConsole} console the console to log to
52-
*/
53-
54-
/**
55-
* @param {FilterItemTypes} item an input item
56-
* @returns {FilterFunction | undefined} filter function
57-
*/
58-
const filterToFunction = (item) => {
21+
const filterToFunction = (
22+
item: FilterItemTypes,
23+
): FilterFunction | undefined => {
5924
if (typeof item === 'string') {
6025
const regExp = new RegExp(
6126
`[\\\\/]${item.replace(/[-[\]{}()*+?.\\^$|]/g, '\\$&')}([\\\\/]|$|!|\\?)`,
@@ -73,10 +38,7 @@ const filterToFunction = (item) => {
7338
}
7439
};
7540

76-
/**
77-
* @enum {number}
78-
*/
79-
const LogLevel = {
41+
const LogLevel: Record<string, number> = {
8042
none: 6,
8143
false: 6,
8244
error: 5,
@@ -87,29 +49,19 @@ const LogLevel = {
8749
verbose: 1,
8850
};
8951

90-
/**
91-
* @param {LoggerOptions} options options object
92-
* @returns {LoggingFunction} logging function
93-
*/
94-
module.exports = ({ level = 'info', debug = false, console }) => {
95-
const debugFilters =
96-
/** @type {FilterFunction[]} */
97-
(
98-
typeof debug === 'boolean'
99-
? [() => debug]
100-
: /** @type {FilterItemTypes[]} */ ([
101-
...(Array.isArray(debug) ? debug : [debug]),
102-
]).map(filterToFunction)
103-
);
52+
export default ({
53+
level = 'info',
54+
debug = false,
55+
console,
56+
}: LoggerOptions): LoggingFunction => {
57+
const debugFilters = (
58+
typeof debug === 'boolean'
59+
? [() => debug]
60+
: [...(Array.isArray(debug) ? debug : [debug])].map(filterToFunction)
61+
) as FilterFunction[];
10462
const loglevel = LogLevel[`${level}`] || 0;
10563

106-
/**
107-
* @param {string} name name of the logger
108-
* @param {LogTypeEnum} type type of the log entry
109-
* @param {Args=} args arguments of the log entry
110-
* @returns {void}
111-
*/
112-
const logger = (name, type, args) => {
64+
const logger = (name: string, type: LogTypeEnum, args?: Args): void => {
11365
const labeledArgs = () => {
11466
if (Array.isArray(args)) {
11567
if (args.length > 0 && typeof args[0] === 'string') {
@@ -176,9 +128,7 @@ module.exports = ({ level = 'info', debug = false, console }) => {
176128
break;
177129
case LogType.time: {
178130
if (!debug && loglevel > LogLevel.log) return;
179-
const [label, start, end] =
180-
/** @type {[string, number, number]} */
181-
(args);
131+
const [label, start, end] = args as [string, number, number];
182132
const ms = start * 1000 + end / 1000000;
183133
const msg = `[${name}] ${label}: ${ms} ms`;
184134
if (typeof console.logTime === 'function') {
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
99
*/
1010

11-
// @ts-nocheck
12-
// @ts-expect-error
1311
export { default } from './runtime';

0 commit comments

Comments
 (0)