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
30 changes: 17 additions & 13 deletions packages/core/src/helpers/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -102,11 +111,6 @@ function getStatsOptions(
// for HMR to compare the entrypoints
entrypoints: true,
};
} else if (action === 'build') {
defaultOptions = {
...defaultOptions,
...getStatsAssetsOptions(),
};
}

if (isMultiCompiler(compiler)) {
Expand Down
24 changes: 10 additions & 14 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, type RsbuildAsset } 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: RsbuildStatsItem,
stats: Rspack.Stats,
rootPath: string,
distPath: string,
environmentName: string,
Expand All @@ -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);
Expand All @@ -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;
});
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/server/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export const viewingServedFilesMiddleware: (params: {

for (const asset of assets) {
list.push(
`<li><a target="_blank" href="${asset?.name}">${asset?.name}</a></li>`,
`<li><a target="_blank" href="${asset.name}">${asset.name}</a></li>`,
);
}

Expand Down
13 changes: 2 additions & 11 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
import type RspackChain from '../../compiled/rspack-chain';
import type { FileDescriptor } from '../../compiled/rspack-manifest-plugin';
import type { BundleAnalyzerPlugin } from '../../compiled/webpack-bundle-analyzer/index.js';
import type { RsbuildAsset } from '../helpers/stats.js';
import type { RsbuildDevServer } from '../server/devServer';
import type {
EnvironmentContext,
Expand Down Expand Up @@ -598,17 +599,7 @@ export type BuildCacheOptions = {
buildDependencies?: string[];
};

export type PrintFileSizeAsset = {
/**
* 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 type PrintFileSizeAsset = RsbuildAsset;

export type PrintFileSizeOptions = {
/**
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' | 'assets'
'errors' | 'warnings' | 'time' | 'entrypoints' | 'hash'
>;

/**
Expand Down
Loading