Skip to content

Commit

Permalink
feat: support print error module traces (#3986)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Nov 15, 2024
1 parent e1342f8 commit 87716db
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
17 changes: 17 additions & 0 deletions e2e/cases/config/stats-module-trace/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { build, proxyConsole } from '@e2e/helper';
import { expect, test } from '@playwright/test';

test('should log error module trace when enable moduleTrace', async () => {
const { restore, logs } = proxyConsole();

await expect(
build({
cwd: __dirname,
rsbuildConfig: {},
}),
).rejects.toThrowError('build failed');

expect(logs.some((log) => log.includes('@ ./src/index.tsx'))).toBeTruthy();

restore();
});
14 changes: 14 additions & 0 deletions e2e/cases/config/stats-module-trace/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
tools: {
rspack: {
stats: {
moduleTrace: true,
},
},
webpack: {
stats: {
moduleTrace: true,
},
},
},
};
1 change: 1 addition & 0 deletions e2e/cases/config/stats-module-trace/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './test';
1 change: 1 addition & 0 deletions e2e/cases/config/stats-module-trace/src/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './test1';
2 changes: 1 addition & 1 deletion e2e/cases/config/stats-options/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('should log warning by default', async () => {
logs.some((log) =>
log.includes('Using / for division outside of calc() is deprecated'),
),
);
).toBeTruthy();

restore();
});
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/client/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ function resolveFileName(stats: StatsError) {
return file ? `File: ${file}\n` : '';
}

function resolveModuleTrace(stats: StatsError) {
let traceStr = '';
if (stats.moduleTrace) {
for (const trace of stats.moduleTrace) {
if (trace.originName) {
// TODO: missing moduleTrace.dependencies[].loc in rspack
traceStr += `\n @ ${trace.originName}`;
}
}
}

return traceStr;
}

function hintUnknownFiles(message: string): string {
const hint = 'You may need an appropriate loader to handle this file type.';

Expand Down Expand Up @@ -61,8 +75,9 @@ function formatMessage(stats: StatsError | string, verbose?: boolean) {
const details =
verbose && stats.details ? `\nDetails: ${stats.details}\n` : '';
const stack = verbose && stats.stack ? `\n${stats.stack}` : '';
const moduleTrace = resolveModuleTrace(stats);

message = `${fileName}${mainMessage}${details}${stack}`;
message = `${fileName}${mainMessage}${details}${stack}${moduleTrace}`;
} else {
message = stats;
}
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/server/socketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import type { IncomingMessage } from 'node:http';
import type { Socket } from 'node:net';
import { parse } from 'node:querystring';
import type Ws from 'ws';
import { getAllStatsErrors, getAllStatsWarnings } from '../helpers';
import {
getAllStatsErrors,
getAllStatsWarnings,
getStatsOptions,
} from '../helpers';
import { logger } from '../logger';
import type { DevConfig, Rspack } from '../types';
import { getCompilationId } from './helper';
Expand Down Expand Up @@ -203,7 +207,16 @@ export class SocketServer {
children: true,
};

return curStats.toJson(defaultStats);
const statsOptions = getStatsOptions(curStats.compilation.compiler);

const userOptions =
typeof statsOptions === 'string'
? { preset: statsOptions }
: typeof statsOptions === 'object'
? statsOptions
: {};

return curStats.toJson({ ...defaultStats, ...userOptions });
}

// determine what message should send by stats
Expand Down

0 comments on commit 87716db

Please sign in to comment.