From 6e124ddd9ea97021f0fd38607214f6c42ef56cf7 Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 13 Oct 2025 13:17:48 +0800 Subject: [PATCH] perf(build): merge stats.toJson calls --- packages/core/src/helpers/stats.ts | 37 +++++++++++++++++---------- packages/core/src/plugins/fileSize.ts | 20 +++++++++------ packages/core/src/types/rsbuild.ts | 2 +- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/packages/core/src/helpers/stats.ts b/packages/core/src/helpers/stats.ts index defbe9d2ce..0cb1f703a3 100644 --- a/packages/core/src/helpers/stats.ts +++ b/packages/core/src/helpers/stats.ts @@ -58,20 +58,23 @@ export const getStatsWarnings = ({ return []; }; +export const getStatsAssetsOptions = (): Rspack.StatsOptions => ({ + assets: true, + cachedAssets: true, + groupAssetsByInfo: false, + groupAssetsByPath: false, + groupAssetsByChunk: false, + groupAssetsByExtension: false, + groupAssetsByEmitStatus: false, +}); + export const getAssetsFromStats = ( stats: Rspack.Stats, ): Rspack.StatsAsset[] => { const statsJson = stats.toJson({ all: false, - assets: true, - cachedAssets: true, - groupAssetsByInfo: false, - groupAssetsByPath: false, - groupAssetsByChunk: false, - groupAssetsByExtension: false, - groupAssetsByEmitStatus: false, + ...getStatsAssetsOptions(), }); - return statsJson.assets || []; }; @@ -79,7 +82,7 @@ function getStatsOptions( compiler: Rspack.Compiler | Rspack.MultiCompiler, action?: ActionType, ): Rspack.StatsOptions { - const defaultOptions: Rspack.StatsOptions = { + let defaultOptions: Rspack.StatsOptions = { all: false, // for displaying the build time timings: true, @@ -92,10 +95,18 @@ function getStatsOptions( }; if (action === 'dev') { - // for HMR to compare the hash - defaultOptions.hash = true; - // for HMR to compare the entrypoints - defaultOptions.entrypoints = true; + defaultOptions = { + ...defaultOptions, + // for HMR to compare the hash + hash: true, + // for HMR to compare the entrypoints + entrypoints: true, + }; + } else if (action === 'build') { + defaultOptions = { + ...defaultOptions, + ...getStatsAssetsOptions(), + }; } if (isMultiCompiler(compiler)) { diff --git a/packages/core/src/plugins/fileSize.ts b/packages/core/src/plugins/fileSize.ts index a0e70ffa94..77b04eef5a 100644 --- a/packages/core/src/plugins/fileSize.ts +++ b/packages/core/src/plugins/fileSize.ts @@ -8,13 +8,13 @@ import { promisify } from 'node:util'; import zlib from 'node:zlib'; import { JS_REGEX } from '../constants'; import { color } from '../helpers'; -import { getAssetsFromStats } from '../helpers/stats'; import { logger } from '../logger'; import type { InternalContext, PrintFileSizeAsset, PrintFileSizeOptions, RsbuildPlugin, + RsbuildStatsItem, Rspack, } from '../types'; @@ -93,7 +93,7 @@ const isCompressible = (assetName: string) => async function printFileSizes( options: PrintFileSizeOptions, - stats: Rspack.Stats, + stats: RsbuildStatsItem, rootPath: string, distPath: string, environmentName: string, @@ -135,7 +135,11 @@ async function printFileSizes( }); const getAssets = async () => { - const filteredAssets = getAssetsFromStats(stats).filter((asset) => { + if (!stats.assets) { + return []; + } + + const filteredAssets = stats.assets.filter((asset) => { const assetInfo = pickAssetInfo(asset); if (exclude(assetInfo)) { return false; @@ -290,9 +294,10 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({ name: 'rsbuild:file-size', setup(api) { - api.onAfterBuild(async ({ stats, environments, isFirstCompile }) => { + api.onAfterBuild(async ({ environments, isFirstCompile }) => { + const { stats, hasErrors } = context.buildState; // No need to print file sizes if there is any compilation error - if (!stats || context.buildState.hasErrors || !isFirstCompile) { + if (!stats || hasErrors || !isFirstCompile) { return; } @@ -306,8 +311,6 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({ return; } - const multiStats = 'stats' in stats ? stats.stats : [stats]; - const defaultConfig: PrintFileSizeOptions = { total: true, detail: true, @@ -323,9 +326,10 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({ ...printFileSize, }; + const statsItem = 'children' in stats ? stats.children[index] : stats; const statsLogs = await printFileSizes( mergedConfig, - multiStats[index], + statsItem, api.context.rootPath, environment.distPath, environment.name, diff --git a/packages/core/src/types/rsbuild.ts b/packages/core/src/types/rsbuild.ts index 77cf038c96..c55ae4cc46 100644 --- a/packages/core/src/types/rsbuild.ts +++ b/packages/core/src/types/rsbuild.ts @@ -368,7 +368,7 @@ export type RsbuildMode = 'development' | 'production' | 'none'; export type RsbuildStatsItem = Pick< Rspack.StatsCompilation, - 'errors' | 'warnings' | 'time' | 'entrypoints' | 'hash' + 'errors' | 'warnings' | 'time' | 'entrypoints' | 'hash' | 'assets' >; /**