diff --git a/packages/core/src/helpers/stats.ts b/packages/core/src/helpers/stats.ts index 0cb1f703a3..9aa50c9654 100644 --- a/packages/core/src/helpers/stats.ts +++ b/packages/core/src/helpers/stats.ts @@ -68,14 +68,23 @@ export const getStatsAssetsOptions = (): Rspack.StatsOptions => ({ groupAssetsByEmitStatus: false, }); -export const getAssetsFromStats = ( - stats: Rspack.Stats, -): Rspack.StatsAsset[] => { - const statsJson = stats.toJson({ - all: false, - ...getStatsAssetsOptions(), - }); - return statsJson.assets || []; +export type RsbuildAsset = { + /** + * The name of the asset. + * @example 'index.html', 'static/js/index.[hash].js' + */ + name: string; + /** + * The size of the asset in bytes. + */ + size: number; +}; + +export const getAssetsFromStats = (stats: Rspack.Stats): RsbuildAsset[] => { + return Object.entries(stats.compilation.assets).map(([name, value]) => ({ + name, + size: value.size(), + })); }; function getStatsOptions( @@ -102,11 +111,6 @@ function getStatsOptions( // 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 77b04eef5a..1c3a7f8552 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, type RsbuildAsset } 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: RsbuildStatsItem, + stats: Rspack.Stats, rootPath: string, distPath: string, environmentName: string, @@ -109,7 +109,7 @@ async function printFileSizes( const exclude = options.exclude ?? excludeAsset; const relativeDistPath = path.relative(rootPath, distPath); - const formatAsset = async (asset: Rspack.StatsAsset) => { + const formatAsset = async (asset: RsbuildAsset) => { const fileName = asset.name.split('?')[0]; const contents = await fs.promises.readFile(path.join(distPath, fileName)); const size = Buffer.byteLength(contents); @@ -135,17 +135,13 @@ async function printFileSizes( }); const getAssets = async () => { - if (!stats.assets) { - return []; - } - - const filteredAssets = stats.assets.filter((asset) => { - const assetInfo = pickAssetInfo(asset); - if (exclude(assetInfo)) { + const assets = getAssetsFromStats(stats); + const filteredAssets = assets.filter((asset) => { + if (exclude(asset)) { return false; } if (options.include) { - return options.include(assetInfo); + return options.include(asset); } return true; }); @@ -294,8 +290,8 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({ name: 'rsbuild:file-size', setup(api) { - api.onAfterBuild(async ({ environments, isFirstCompile }) => { - const { stats, hasErrors } = context.buildState; + api.onAfterBuild(async ({ stats, environments, isFirstCompile }) => { + const { hasErrors } = context.buildState; // No need to print file sizes if there is any compilation error if (!stats || hasErrors || !isFirstCompile) { return; @@ -326,7 +322,7 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({ ...printFileSize, }; - const statsItem = 'children' in stats ? stats.children[index] : stats; + const statsItem = 'stats' in stats ? stats.stats[index] : stats; const statsLogs = await printFileSizes( mergedConfig, statsItem, diff --git a/packages/core/src/server/middlewares.ts b/packages/core/src/server/middlewares.ts index b32981c927..7ee1d77190 100644 --- a/packages/core/src/server/middlewares.ts +++ b/packages/core/src/server/middlewares.ts @@ -339,7 +339,7 @@ export const viewingServedFilesMiddleware: (params: { for (const asset of assets) { list.push( - `