|
2 | 2 |
|
3 | 3 | import fs from 'node:fs' |
4 | 4 | import { promises as fsp } from 'node:fs' |
| 5 | +import { createRequire } from 'node:module' |
5 | 6 | import path from 'node:path' |
6 | | -import { fileURLToPath } from 'node:url' |
| 7 | +import { fileURLToPath, pathToFileURL } from 'node:url' |
7 | 8 | import { parseArgs as parseNodeArgs } from 'node:util' |
8 | 9 | import { brotliCompressSync, gzipSync } from 'node:zlib' |
9 | 10 | import { execSync } from 'node:child_process' |
@@ -69,6 +70,24 @@ const SCENARIOS = [ |
69 | 70 | packageName: '@tanstack/react-start', |
70 | 71 | case: 'full', |
71 | 72 | }, |
| 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 | + }, |
72 | 91 | { |
73 | 92 | id: 'solid-start.minimal', |
74 | 93 | dir: 'solid-start-minimal', |
@@ -329,6 +348,153 @@ async function resolveManifestAndEntry(outDir, scenarioId) { |
329 | 348 | ) |
330 | 349 | } |
331 | 350 |
|
| 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 | + |
332 | 498 | function getCurrentSha(providedSha) { |
333 | 499 | if (providedSha) { |
334 | 500 | return providedSha |
@@ -417,51 +583,23 @@ async function main() { |
417 | 583 |
|
418 | 584 | for (const scenario of SCENARIOS) { |
419 | 585 | 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) |
447 | 587 |
|
448 | | - const manifestInfo = await resolveManifestAndEntry(outDir, scenario.id) |
| 588 | + await buildScenario({ root, outDir, scenario }) |
449 | 589 |
|
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) |
455 | 592 |
|
456 | 593 | metrics.push({ |
457 | 594 | id: scenario.id, |
458 | 595 | scenarioDir: scenario.dir, |
| 596 | + toolchain: scenario.toolchain || 'vite', |
459 | 597 | framework: scenario.framework, |
460 | 598 | packageName: scenario.packageName, |
461 | 599 | 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, |
465 | 603 | ...sizes, |
466 | 604 | }) |
467 | 605 | } |
|
0 commit comments