Skip to content

Commit 8adbe74

Browse files
add new bundlesize measurements for rsbuild (#7256)
Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
1 parent c36bdcc commit 8adbe74

6 files changed

Lines changed: 258 additions & 39 deletions

File tree

‎benchmarks/bundle-size/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This workspace contains deterministic bundle-size fixtures for:
88
- `@tanstack/react-start`
99
- `@tanstack/solid-start`
1010

11-
Each package has two scenarios:
11+
Each package has `minimal` and `full` scenarios:
1212

1313
- `minimal`: Small route app with `__root` + index route that renders `hello world`
1414
- `full`: Same route shape plus a broad root-level harness that imports/uses the full hooks/components surface
@@ -18,7 +18,8 @@ Each package has two scenarios:
1818

1919
- Scenarios use file-based routing as the default app style.
2020
- Router scenarios use `@tanstack/router-plugin/vite` with `autoCodeSplitting: true`.
21-
- Start scenarios use `@tanstack/<framework>-start/plugin/vite` with router code-splitting enabled.
21+
- Start Vite scenarios use `@tanstack/<framework>-start/plugin/vite` with router code-splitting enabled.
22+
- React Start also includes Rsbuild scenarios using `@tanstack/react-start/plugin/rsbuild`.
2223
- Full-surface coverage is manually maintained (no strict export-coverage gate).
2324
- Metrics are measured from initial-load JS graph only and reported as raw/gzip/brotli bytes.
2425
- Gzip is the primary tracking signal for PR deltas and historical charting.

‎benchmarks/bundle-size/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"vue": "^3.5.16"
1818
},
1919
"devDependencies": {
20+
"@rsbuild/core": "^2.0.1",
21+
"@rsbuild/plugin-react": "^2.0.0",
2022
"@tanstack/router-plugin": "workspace:^",
2123
"@types/react": "^19.0.8",
2224
"@types/react-dom": "^19.0.3",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { defineConfig } from '@rsbuild/core'
2+
import { pluginReact } from '@rsbuild/plugin-react'
3+
import { tanstackStart } from '@tanstack/react-start/plugin/rsbuild'
4+
5+
const outDir = process.env.BUNDLE_SIZE_DIST_DIR ?? 'dist-rsbuild'
6+
7+
export default defineConfig({
8+
logLevel: 'silent',
9+
plugins: [pluginReact({ splitChunks: false }), tanstackStart()],
10+
output: {
11+
distPath: {
12+
root: outDir,
13+
},
14+
cleanDistPath: true,
15+
minify: true,
16+
sourceMap: false,
17+
},
18+
performance: {
19+
printFileSize: false,
20+
},
21+
environments: {
22+
client: {
23+
output: {
24+
manifest: {
25+
filename: 'manifest.json',
26+
prefix: false,
27+
},
28+
},
29+
},
30+
ssr: {
31+
output: {
32+
manifest: false,
33+
},
34+
},
35+
},
36+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { defineConfig } from '@rsbuild/core'
2+
import { pluginReact } from '@rsbuild/plugin-react'
3+
import { tanstackStart } from '@tanstack/react-start/plugin/rsbuild'
4+
5+
const outDir = process.env.BUNDLE_SIZE_DIST_DIR ?? 'dist-rsbuild'
6+
7+
export default defineConfig({
8+
logLevel: 'silent',
9+
plugins: [pluginReact({ splitChunks: false }), tanstackStart()],
10+
output: {
11+
distPath: {
12+
root: outDir,
13+
},
14+
cleanDistPath: true,
15+
minify: true,
16+
sourceMap: false,
17+
},
18+
performance: {
19+
printFileSize: false,
20+
},
21+
environments: {
22+
client: {
23+
output: {
24+
manifest: {
25+
filename: 'manifest.json',
26+
prefix: false,
27+
},
28+
},
29+
},
30+
ssr: {
31+
output: {
32+
manifest: false,
33+
},
34+
},
35+
},
36+
})

‎pnpm-lock.yaml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/benchmarks/bundle-size/measure.mjs‎

Lines changed: 175 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import fs from 'node:fs'
44
import { promises as fsp } from 'node:fs'
5+
import { createRequire } from 'node:module'
56
import path from 'node:path'
6-
import { fileURLToPath } from 'node:url'
7+
import { fileURLToPath, pathToFileURL } from 'node:url'
78
import { parseArgs as parseNodeArgs } from 'node:util'
89
import { brotliCompressSync, gzipSync } from 'node:zlib'
910
import { execSync } from 'node:child_process'
@@ -69,6 +70,24 @@ const SCENARIOS = [
6970
packageName: '@tanstack/react-start',
7071
case: 'full',
7172
},
73+
{
74+
id: 'react-start.rsbuild.minimal',
75+
dir: 'react-start-minimal',
76+
outDir: 'react-start-rsbuild-minimal',
77+
toolchain: 'rsbuild',
78+
framework: 'react',
79+
packageName: '@tanstack/react-start',
80+
case: 'minimal',
81+
},
82+
{
83+
id: 'react-start.rsbuild.full',
84+
dir: 'react-start-full',
85+
outDir: 'react-start-rsbuild-full',
86+
toolchain: 'rsbuild',
87+
framework: 'react',
88+
packageName: '@tanstack/react-start',
89+
case: 'full',
90+
},
7291
{
7392
id: 'solid-start.minimal',
7493
dir: 'solid-start-minimal',
@@ -329,6 +348,153 @@ async function resolveManifestAndEntry(outDir, scenarioId) {
329348
)
330349
}
331350

351+
async function importFromRoot(root, specifier) {
352+
const requireFromRoot = createRequire(
353+
path.join(root, 'bundle-size.config.cjs'),
354+
)
355+
return import(pathToFileURL(requireFromRoot.resolve(specifier)).href)
356+
}
357+
358+
async function buildViteScenario({ root, outDir }) {
359+
const configFile = path.join(root, 'vite.config.ts')
360+
361+
await build({
362+
root,
363+
configFile,
364+
logLevel: 'silent',
365+
define: {
366+
'process.env.NODE_ENV': '"production"',
367+
},
368+
build: {
369+
outDir,
370+
emptyOutDir: true,
371+
target: 'es2022',
372+
minify: 'esbuild',
373+
sourcemap: false,
374+
reportCompressedSize: false,
375+
manifest: true,
376+
},
377+
})
378+
}
379+
380+
async function buildRsbuildScenario({ root, outDir }) {
381+
const configFile = path.join(root, 'rsbuild.config.ts')
382+
const { createRsbuild, loadConfig } = await importFromRoot(
383+
root,
384+
'@rsbuild/core',
385+
)
386+
const previousOutDir = process.env.BUNDLE_SIZE_DIST_DIR
387+
388+
process.env.BUNDLE_SIZE_DIST_DIR = outDir
389+
390+
try {
391+
const { content } = await loadConfig({
392+
cwd: root,
393+
path: configFile,
394+
envMode: 'production',
395+
})
396+
const rsbuild = await createRsbuild({
397+
cwd: root,
398+
callerName: 'bundle-size-benchmark',
399+
config: content,
400+
})
401+
const result = await rsbuild.build()
402+
await result.close()
403+
} finally {
404+
if (previousOutDir === undefined) {
405+
delete process.env.BUNDLE_SIZE_DIST_DIR
406+
} else {
407+
process.env.BUNDLE_SIZE_DIST_DIR = previousOutDir
408+
}
409+
}
410+
}
411+
412+
async function buildScenario({ root, outDir, scenario }) {
413+
const previousCwd = process.cwd()
414+
process.chdir(root)
415+
416+
try {
417+
if (scenario.toolchain === 'rsbuild') {
418+
await buildRsbuildScenario({ root, outDir })
419+
return
420+
}
421+
422+
await buildViteScenario({ root, outDir })
423+
} finally {
424+
process.chdir(previousCwd)
425+
}
426+
}
427+
428+
async function resolveRsbuildManifest(outDir, scenarioId) {
429+
const manifestPath = path.join(outDir, 'client', 'manifest.json')
430+
431+
if (!fs.existsSync(manifestPath)) {
432+
throw new Error(
433+
`No Rsbuild manifest file found for scenario: ${scenarioId}`,
434+
)
435+
}
436+
437+
const manifest = readJson(manifestPath)
438+
const entryName = manifest.entries?.index
439+
? 'index'
440+
: Object.keys(manifest.entries || {})[0]
441+
442+
if (!entryName) {
443+
throw new Error(
444+
`Could not determine Rsbuild manifest entry for scenario: ${scenarioId}`,
445+
)
446+
}
447+
448+
return {
449+
manifest,
450+
entryKey: entryName,
451+
manifestPath,
452+
manifestOutDir: path.dirname(manifestPath),
453+
}
454+
}
455+
456+
function collectRsbuildInitialJsFiles(manifest, entryKey) {
457+
const files = manifest.entries?.[entryKey]?.initial?.js
458+
459+
if (!Array.isArray(files)) {
460+
return []
461+
}
462+
463+
return files
464+
.filter(
465+
(file) =>
466+
typeof file === 'string' &&
467+
(file.endsWith('.js') || file.endsWith('.mjs')),
468+
)
469+
.sort()
470+
}
471+
472+
async function resolveBundleFiles({ outDir, scenario }) {
473+
if (scenario.toolchain === 'rsbuild') {
474+
const manifestInfo = await resolveRsbuildManifest(outDir, scenario.id)
475+
const jsFiles = collectRsbuildInitialJsFiles(
476+
manifestInfo.manifest,
477+
manifestInfo.entryKey,
478+
)
479+
480+
return {
481+
...manifestInfo,
482+
jsFiles,
483+
}
484+
}
485+
486+
const manifestInfo = await resolveManifestAndEntry(outDir, scenario.id)
487+
const jsFiles = collectInitialJsFiles(
488+
manifestInfo.manifest,
489+
manifestInfo.entryKey,
490+
)
491+
492+
return {
493+
...manifestInfo,
494+
jsFiles,
495+
}
496+
}
497+
332498
function getCurrentSha(providedSha) {
333499
if (providedSha) {
334500
return providedSha
@@ -417,51 +583,23 @@ async function main() {
417583

418584
for (const scenario of SCENARIOS) {
419585
const root = path.join(scenariosRoot, scenario.dir)
420-
const outDir = path.join(distDir, scenario.dir)
421-
const configFile = path.join(root, 'vite.config.ts')
422-
423-
const previousCwd = process.cwd()
424-
process.chdir(root)
425-
426-
try {
427-
await build({
428-
root,
429-
configFile,
430-
logLevel: 'silent',
431-
define: {
432-
'process.env.NODE_ENV': '"production"',
433-
},
434-
build: {
435-
outDir,
436-
emptyOutDir: true,
437-
target: 'es2022',
438-
minify: 'esbuild',
439-
sourcemap: false,
440-
reportCompressedSize: false,
441-
manifest: true,
442-
},
443-
})
444-
} finally {
445-
process.chdir(previousCwd)
446-
}
586+
const outDir = path.join(distDir, scenario.outDir || scenario.dir)
447587

448-
const manifestInfo = await resolveManifestAndEntry(outDir, scenario.id)
588+
await buildScenario({ root, outDir, scenario })
449589

450-
const jsFiles = collectInitialJsFiles(
451-
manifestInfo.manifest,
452-
manifestInfo.entryKey,
453-
)
454-
const sizes = bytesForFiles(manifestInfo.manifestOutDir, jsFiles)
590+
const bundleInfo = await resolveBundleFiles({ outDir, scenario })
591+
const sizes = bytesForFiles(bundleInfo.manifestOutDir, bundleInfo.jsFiles)
455592

456593
metrics.push({
457594
id: scenario.id,
458595
scenarioDir: scenario.dir,
596+
toolchain: scenario.toolchain || 'vite',
459597
framework: scenario.framework,
460598
packageName: scenario.packageName,
461599
case: scenario.case,
462-
entryKey: manifestInfo.entryKey,
463-
manifestPath: path.relative(outDir, manifestInfo.manifestPath),
464-
jsFiles,
600+
entryKey: bundleInfo.entryKey,
601+
manifestPath: path.relative(outDir, bundleInfo.manifestPath),
602+
jsFiles: bundleInfo.jsFiles,
465603
...sizes,
466604
})
467605
}

0 commit comments

Comments
 (0)