Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions packages/core/src/helpers/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,31 @@ 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 || [];
};

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,
Expand All @@ -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)) {
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/plugins/fileSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -93,7 +93,7 @@ const isCompressible = (assetName: string) =>

async function printFileSizes(
options: PrintFileSizeOptions,
stats: Rspack.Stats,
stats: RsbuildStatsItem,
rootPath: string,
distPath: string,
environmentName: string,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/rsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
>;

/**
Expand Down
Loading