Skip to content

Commit 8da699e

Browse files
committed
feat: print Rsbuild-style URL logs
1 parent a6a9a6e commit 8da699e

2 files changed

Lines changed: 37 additions & 115 deletions

File tree

src/server.ts

Lines changed: 29 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ import type {
7474
Headers,
7575
} from './types';
7676

77+
const { styleText } = util;
78+
7779
export interface Configuration<
7880
A extends BasicApplication = ExpressApplication,
7981
S extends HTTPServer = HTTPServer,
@@ -186,7 +188,7 @@ class Server<
186188

187189
constructor(options: DevServer, compiler: Compiler | MultiCompiler) {
188190
this.compiler = compiler;
189-
this.logger = this.compiler.getInfrastructureLogger('webpack-dev-server');
191+
this.logger = this.compiler.getInfrastructureLogger('rspack-dev-server');
190192
this.options = options as unknown as Configuration<A, S>;
191193
this.staticWatchers = [];
192194
this.listeners = [];
@@ -637,6 +639,12 @@ class Server<
637639
return (this.compiler as Compiler).options;
638640
}
639641

642+
shouldLogInfrastructureInfo() {
643+
const compilerOptions = this.getCompilerOptions();
644+
const { level = 'info' } = compilerOptions.infrastructureLogging;
645+
return level === 'info' || level === 'log' || level === 'verbose';
646+
}
647+
640648
async normalizeOptions() {
641649
const { options } = this;
642650
const compilerOptions = this.getCompilerOptions();
@@ -1577,7 +1585,7 @@ class Server<
15771585
}
15781586

15791587
this.logger.info(
1580-
'Gracefully shutting down. To force exit, press ^C again. Please wait...',
1588+
'Gracefully shutting down. Press ^C again to force exit...',
15811589
);
15821590

15831591
needForceShutdown = true;
@@ -2400,10 +2408,10 @@ class Server<
24002408

24012409
this.bonjour = new Bonjour();
24022410
this.bonjour?.publish({
2403-
name: `Webpack Dev Server ${os.hostname()}:${this.options.port}`,
2411+
name: `Rspack Dev Server ${os.hostname()}:${this.options.port}`,
24042412
port: this.options.port as number,
24052413
type,
2406-
subtypes: ['webpack'],
2414+
subtypes: ['rspack'],
24072415
...(this.options.bonjour as Partial<BonjourOptions>),
24082416
});
24092417
}
@@ -2419,43 +2427,6 @@ class Server<
24192427
}
24202428

24212429
async logStatus() {
2422-
const { cyan, isColorSupported, red } = require('colorette');
2423-
2424-
const getColorsOption = (compilerOptions: Compiler['options']): boolean => {
2425-
let colorsEnabled: boolean;
2426-
2427-
if (
2428-
compilerOptions.stats &&
2429-
typeof (compilerOptions.stats as unknown as StatsOptions).colors !==
2430-
'undefined'
2431-
) {
2432-
colorsEnabled = (compilerOptions.stats as unknown as StatsOptions)
2433-
.colors as boolean;
2434-
} else {
2435-
colorsEnabled = isColorSupported as boolean;
2436-
}
2437-
2438-
return colorsEnabled;
2439-
};
2440-
2441-
const colors = {
2442-
info(useColor: boolean, msg: string): string {
2443-
if (useColor) {
2444-
return cyan(msg);
2445-
}
2446-
2447-
return msg;
2448-
},
2449-
error(useColor: boolean, msg: string): string {
2450-
if (useColor) {
2451-
return red(msg);
2452-
}
2453-
2454-
return msg;
2455-
},
2456-
};
2457-
const useColor = getColorsOption(this.getCompilerOptions());
2458-
24592430
const server = this.server as S;
24602431

24612432
if (this.options.ipc) {
@@ -2470,29 +2441,14 @@ class Server<
24702441
const prettyPrintURL = (newHostname: string): string =>
24712442
url.format({ protocol, hostname: newHostname, port, pathname: '/' });
24722443

2473-
let host: string | undefined;
24742444
let localhost: string | undefined;
24752445
let loopbackIPv4: string | undefined;
24762446
let loopbackIPv6: string | undefined;
24772447
let networkUrlIPv4: string | undefined;
24782448
let networkUrlIPv6: string | undefined;
24792449

2480-
if (this.options.host) {
2481-
if (this.options.host === 'localhost') {
2482-
localhost = prettyPrintURL('localhost');
2483-
} else {
2484-
let isIP: IPv6 | ipaddr.IPv4 | null | undefined;
2485-
2486-
try {
2487-
isIP = ipaddr.parse(this.options.host) as IPv6 | ipaddr.IPv4;
2488-
} catch {
2489-
// Ignore
2490-
}
2491-
2492-
if (!isIP) {
2493-
host = prettyPrintURL(this.options.host);
2494-
}
2495-
}
2450+
if (this.options.host === 'localhost') {
2451+
localhost = prettyPrintURL('localhost');
24962452
}
24972453

24982454
const parsedIP = ipaddr.parse(address);
@@ -2529,40 +2485,27 @@ class Server<
25292485
}
25302486
}
25312487

2532-
this.logger.info('Project is running at:');
2533-
2534-
if (host) {
2535-
this.logger.info(`Server: ${colors.info(useColor, host)}`);
2536-
}
2537-
2538-
if (localhost || loopbackIPv4 || loopbackIPv6) {
2539-
const loopbacks = [];
2540-
2541-
if (localhost) {
2542-
loopbacks.push([colors.info(useColor, localhost)]);
2543-
}
2544-
2545-
if (loopbackIPv4) {
2546-
loopbacks.push([colors.info(useColor, loopbackIPv4)]);
2547-
}
2548-
2549-
if (loopbackIPv6) {
2550-
loopbacks.push([colors.info(useColor, loopbackIPv6)]);
2551-
}
2488+
const urlLogs = [];
25522489

2553-
this.logger.info(`Loopback: ${loopbacks.join(', ')}`);
2490+
const local = localhost || loopbackIPv4 || loopbackIPv6;
2491+
if (local) {
2492+
urlLogs.push(
2493+
` ${styleText('white', '➜')} ${styleText(['white', 'dim'], 'Local:')} ${styleText('cyan', local)}`,
2494+
);
25542495
}
25552496

25562497
if (networkUrlIPv4) {
2557-
this.logger.info(
2558-
`On Your Network (IPv4): ${colors.info(useColor, networkUrlIPv4)}`,
2498+
urlLogs.push(
2499+
` ${styleText('white', '➜')} ${styleText(['white', 'dim'], 'Network:')} ${styleText('cyan', networkUrlIPv4)}`,
2500+
);
2501+
} else if (networkUrlIPv6) {
2502+
urlLogs.push(
2503+
` ${styleText('white', '➜')} ${styleText(['white', 'dim'], 'Network:')} ${styleText('cyan', networkUrlIPv6)}`,
25592504
);
25602505
}
25612506

2562-
if (networkUrlIPv6) {
2563-
this.logger.info(
2564-
`On Your Network (IPv6): ${colors.info(useColor, networkUrlIPv6)}`,
2565-
);
2507+
if (urlLogs.length && this.shouldLogInfrastructureInfo()) {
2508+
console.log(`${urlLogs.join('\n')}\n`);
25662509
}
25672510

25682511
if ((this.options.open as NormalizedOpen[])?.length > 0) {
@@ -2578,27 +2521,6 @@ class Server<
25782521
}
25792522
}
25802523

2581-
if ((this.options.static as NormalizedStatic[])?.length > 0) {
2582-
this.logger.info(
2583-
`Content not from webpack is served from '${colors.info(
2584-
useColor,
2585-
(this.options.static as NormalizedStatic[])
2586-
.map((staticOption) => staticOption.directory)
2587-
.join(', '),
2588-
)}' directory`,
2589-
);
2590-
}
2591-
2592-
if (this.options.historyApiFallback) {
2593-
this.logger.info(
2594-
`404s will fallback to '${colors.info(
2595-
useColor,
2596-
(this.options.historyApiFallback as ConnectHistoryApiFallbackOptions)
2597-
.index || '/index.html',
2598-
)}'`,
2599-
);
2600-
}
2601-
26022524
if (this.options.bonjour) {
26032525
const bonjourProtocol =
26042526
(this.options.bonjour as BonjourOptions | undefined)?.type ||
@@ -2607,7 +2529,7 @@ class Server<
26072529
: 'http';
26082530

26092531
this.logger.info(
2610-
`Broadcasting "${bonjourProtocol}" with subtype of "webpack" via ZeroConf DNS (Bonjour)`,
2532+
`Broadcasting "${bonjourProtocol}" with subtype of "rspack" via ZeroConf DNS (Bonjour)`,
26112533
);
26122534
}
26132535
}

tests/e2e/bonjour.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ describe('bonjour option', () => {
7676
expect(mockPublish).toHaveBeenCalledTimes(1);
7777

7878
expect(mockPublish).toHaveBeenCalledWith({
79-
name: `Webpack Dev Server ${os.hostname()}:${port}`,
79+
name: `Rspack Dev Server ${os.hostname()}:${port}`,
8080
port,
8181
type: 'http',
82-
subtypes: ['webpack'],
82+
subtypes: ['rspack'],
8383
});
8484

8585
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
@@ -149,10 +149,10 @@ describe('bonjour option', () => {
149149
expect(mockPublish).toHaveBeenCalledTimes(1);
150150

151151
expect(mockPublish).toHaveBeenCalledWith({
152-
name: `Webpack Dev Server ${os.hostname()}:${port}`,
152+
name: `Rspack Dev Server ${os.hostname()}:${port}`,
153153
port,
154154
type: 'https',
155-
subtypes: ['webpack'],
155+
subtypes: ['rspack'],
156156
});
157157

158158
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
@@ -231,11 +231,11 @@ describe('bonjour option', () => {
231231
expect(mockPublish).toHaveBeenCalledTimes(1);
232232

233233
expect(mockPublish).toHaveBeenCalledWith({
234-
name: `Webpack Dev Server ${os.hostname()}:${port}`,
234+
name: `Rspack Dev Server ${os.hostname()}:${port}`,
235235
port,
236236
type: 'https',
237237
protocol: 'udp',
238-
subtypes: ['webpack'],
238+
subtypes: ['rspack'],
239239
});
240240

241241
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
@@ -317,11 +317,11 @@ describe('bonjour option', () => {
317317
expect(mockPublish).toHaveBeenCalledTimes(1);
318318

319319
expect(mockPublish).toHaveBeenCalledWith({
320-
name: `Webpack Dev Server ${os.hostname()}:${port}`,
320+
name: `Rspack Dev Server ${os.hostname()}:${port}`,
321321
port,
322322
type: 'http',
323323
protocol: 'udp',
324-
subtypes: ['webpack'],
324+
subtypes: ['rspack'],
325325
});
326326

327327
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);

0 commit comments

Comments
 (0)