Skip to content

Commit

Permalink
chore: normalize stats with getStatsOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Nov 15, 2024
1 parent 87716db commit 454ad95
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
19 changes: 14 additions & 5 deletions packages/core/src/helpers/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StatsCompilation, StatsValue } from '@rspack/core';
import type { StatsCompilation } from '@rspack/core';
import color from '../../compiled/picocolors/index.js';
import { formatStatsMessages } from '../client/format';
import { logger } from '../logger';
Expand Down Expand Up @@ -128,17 +128,26 @@ export const getAllStatsWarnings = (
};

export function getStatsOptions(
compiler: Parameters<typeof isMultiCompiler>[0],
): StatsValue | undefined {
compiler: Rspack.Compiler | Rspack.MultiCompiler,
): Rspack.StatsOptions {
if (isMultiCompiler(compiler)) {
return {
children: compiler.compilers.map((compiler) =>
compiler.options ? compiler.options.stats : undefined,
),
} as unknown as StatsValue;
} as unknown as Rspack.StatsOptions;
}

return compiler.options ? (compiler.options.stats as StatsValue) : undefined;
const { stats } = compiler.options;

if (typeof stats === 'string') {
return { preset: stats };
}
if (typeof stats === 'object') {
return stats;
}

return {};
}

export function formatStats(
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/provider/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
children: true,
// get the compilation time
timings: true,
...(typeof statsOptions === 'string'
? { preset: statsOptions }
: { preset: 'errors-warnings' }),
...(typeof statsOptions === 'object' ? statsOptions : {}),
preset: 'errors-warnings',
...statsOptions,
});

const printTime = (c: StatsCompilation, index: number) => {
Expand Down
10 changes: 1 addition & 9 deletions packages/core/src/server/socketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,7 @@ export class SocketServer {
};

const statsOptions = getStatsOptions(curStats.compilation.compiler);

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

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

// determine what message should send by stats
Expand Down

0 comments on commit 454ad95

Please sign in to comment.