Skip to content

Commit

Permalink
perf: reduce stats.toJson calls (#3565)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Sep 26, 2024
1 parent 74d3554 commit 85ad68e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
18 changes: 12 additions & 6 deletions packages/compat/webpack/src/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ export async function createCompiler(options: InitConfigsOptions) {
| Rspack.Compiler
| Rspack.MultiCompiler;

const done = (stats: unknown) => {
const { message, level } = formatStats(
stats as Rspack.Stats,
getStatsOptions(compiler),
);
const done = (stats: Rspack.Stats) => {
const statsOptions = getStatsOptions(compiler);
const statsJson = stats.toJson({
children: true,
...(typeof statsOptions === 'string'
? { preset: statsOptions }
: { preset: 'errors-warnings' }),
...(typeof statsOptions === 'object' ? statsOptions : {}),
});

const { message, level } = formatStats(statsJson, stats.hasErrors());

if (level === 'error') {
logger.error(message);
Expand All @@ -36,7 +42,7 @@ export async function createCompiler(options: InitConfigsOptions) {
};

compiler.hooks.done.tap('rsbuild:done', (stats: unknown) => {
done(stats);
done(stats as Rspack.Stats);
});

if (context.normalizedConfig?.mode === 'development') {
Expand Down
42 changes: 21 additions & 21 deletions packages/core/src/helpers/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,38 @@ export function getStatsOptions(
}

export function formatStats(
stats: Rspack.Stats | Rspack.MultiStats,
options: StatsValue = {},
statsData: Rspack.StatsCompilation,
hasErrors: boolean,
): {
message?: string;
level?: string;
} {
const statsData = stats.toJson(
typeof options === 'object'
? {
preset: 'errors-warnings',
children: true,
...options,
}
: options,
);

const { errors, warnings } = formatStatsMessages(
{
errors: getAllStatsErrors(statsData),
warnings: getAllStatsWarnings(statsData),
},
// display verbose messages in debug mode
logger.level === 'verbose',
);
// display verbose messages in debug mode
const verbose = logger.level === 'verbose';

if (hasErrors) {
const { errors } = formatStatsMessages(
{
errors: getAllStatsErrors(statsData),
warnings: [],
},
verbose,
);

if (stats.hasErrors()) {
return {
message: formatErrorMessage(errors),
level: 'error',
};
}

const { warnings } = formatStatsMessages(
{
errors: [],
warnings: getAllStatsWarnings(statsData),
},
verbose,
);

if (warnings.length) {
const title = color.bold(color.yellow('Compile Warning: \n'));

Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/provider/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
}

const done = (stats: Rspack.Stats | Rspack.MultiStats) => {
const statsOptions = getStatsOptions(compiler);
const statsJson = stats.toJson({
all: false,
children: true,
// get the compilation time
timings: true,
...(typeof statsOptions === 'string'
? { preset: statsOptions }
: { preset: 'errors-warnings' }),
...(typeof statsOptions === 'object' ? statsOptions : {}),
});

const printTime = (c: StatsCompilation, index: number) => {
Expand All @@ -76,8 +82,10 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
}
};

if (!stats.hasErrors()) {
if (statsJson.children) {
const hasErrors = stats.hasErrors();

if (!hasErrors) {
if (statsJson.children && statsJson.children.length > 0) {
statsJson.children.forEach((c, index) => {
printTime(c, index);
});
Expand All @@ -86,7 +94,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
}
}

const { message, level } = formatStats(stats, getStatsOptions(compiler));
const { message, level } = formatStats(statsJson, hasErrors);

if (level === 'error') {
logger.error(message);
Expand Down

0 comments on commit 85ad68e

Please sign in to comment.