Skip to content

Commit 6f23494

Browse files
authored
feat: improve build time logs with error state (#6608)
1 parent b37faa8 commit 6f23494

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { rspackTest } from '@e2e/helper';
2+
3+
const EXPECTED_LOG = /build failed in [\d.]+ s/;
4+
5+
rspackTest('should print build failed time in dev', async ({ devOnly }) => {
6+
const rsbuild = await devOnly();
7+
await rsbuild.expectLog(EXPECTED_LOG);
8+
});
9+
10+
rspackTest('should print build failed time in build', async ({ build }) => {
11+
const rsbuild = await build({
12+
catchBuildError: true,
13+
});
14+
await rsbuild.expectLog(EXPECTED_LOG);
15+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './not-exist.js';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { rspackTest } from '@e2e/helper';
2+
3+
const EXPECTED_LOG = /built in [\d.]+ s/;
4+
5+
rspackTest('should print build time in dev', async ({ devOnly }) => {
6+
const rsbuild = await devOnly();
7+
await rsbuild.expectLog(EXPECTED_LOG);
8+
});
9+
10+
rspackTest('should print build time in build', async ({ build }) => {
11+
const rsbuild = await build();
12+
await rsbuild.expectLog(EXPECTED_LOG);
13+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

packages/compat/webpack/src/progress/helpers/nonTty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function createNonTTYLogger() {
2727

2828
prevPercentage = 100;
2929
if (hasErrors) {
30-
logger.error(`built failed in ${compileTime} ${suffix}`);
30+
logger.error(`build failed in ${compileTime} ${suffix}`);
3131
} else {
3232
logger.ready(`built in ${compileTime} ${suffix}`);
3333
}

packages/core/src/provider/createCompiler.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
186186
});
187187
}
188188

189-
const printTime = (index: number) => {
189+
const printTime = (index: number, hasErrors: boolean) => {
190190
if (startTime === null) {
191191
return;
192192
}
@@ -197,13 +197,19 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
197197

198198
// When using multiple compilers, print the name to distinguish different environments
199199
const suffix = isMultiCompiler ? color.dim(` (${name})`) : '';
200-
logger.ready(`built in ${prettyTime(time / 1000)}${suffix}`);
200+
const timeStr = `${prettyTime(time / 1000)}${suffix}`;
201+
202+
if (hasErrors) {
203+
logger.error(`build failed in ${timeStr}`);
204+
} else {
205+
logger.ready(`built in ${timeStr}`);
206+
}
201207
};
202208

203209
if (isMultiCompiler) {
204210
(compiler as Rspack.MultiCompiler).compilers.forEach((item, index) => {
205-
item.hooks.done.tap(HOOK_NAME, () => {
206-
printTime(index);
211+
item.hooks.done.tap(HOOK_NAME, (stats) => {
212+
printTime(index, stats.hasErrors());
207213
});
208214
});
209215
}
@@ -219,10 +225,6 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
219225
context.buildState.hasErrors = hasErrors;
220226
context.socketServer?.onBuildDone();
221227

222-
if (!isMultiCompiler) {
223-
printTime(0);
224-
}
225-
226228
const { message, level } = formatStats(stats, hasErrors);
227229

228230
if (level === 'error') {
@@ -231,6 +233,9 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
231233
if (level === 'warning') {
232234
logger.warn(message);
233235
}
236+
if (!isMultiCompiler) {
237+
printTime(0, hasErrors);
238+
}
234239

235240
isCompiling = false;
236241
},

0 commit comments

Comments
 (0)