Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

measure.mjs

1,038 lines25.5 KB

1#!/usr/bin/env node
2​
3import fs from 'node:fs'
4import { promises as fsp } from 'node:fs'
5import { createRequire } from 'node:module'
6import path from 'node:path'
7import { fileURLToPath, pathToFileURL } from 'node:url'
8import { parseArgs as parseNodeArgs } from 'node:util'
9import vm from 'node:vm'
10import { execFileSync } from 'node:child_process'
11​
12import { build } from 'vite'
13import { writeReport } from './report.mjs'
14import { measureFileSizes } from './compress.mjs'
15import { createTimings } from './timings.mjs'
16​
17const BENCHMARK_NAME = 'Bundle Size (gzip)'
18​
19const SCENARIOS = [
20 {
21 id: 'react-router.minimal',
22 dir: 'react-router-minimal',
23 framework: 'react',
24 packageName: '@tanstack/react-router',
25 case: 'minimal',
26 },
27 {
28 id: 'react-router.full',
29 dir: 'react-router-full',
30 framework: 'react',
31 packageName: '@tanstack/react-router',
32 case: 'full',
33 },
34 {
35 id: 'solid-router.minimal',
36 dir: 'solid-router-minimal',
37 framework: 'solid',
38 packageName: '@tanstack/solid-router',
39 case: 'minimal',
40 },
41 {
42 id: 'solid-router.full',
43 dir: 'solid-router-full',
44 framework: 'solid',
45 packageName: '@tanstack/solid-router',
46 case: 'full',
47 },
48 {
49 id: 'vue-router.minimal',
50 dir: 'vue-router-minimal',
51 framework: 'vue',
52 packageName: '@tanstack/vue-router',
53 case: 'minimal',
54 },
55 {
56 id: 'vue-router.full',
57 dir: 'vue-router-full',
58 framework: 'vue',
59 packageName: '@tanstack/vue-router',
60 case: 'full',
61 },
62 {
63 id: 'react-start.minimal',
64 dir: 'react-start-minimal',
65 framework: 'react',
66 packageName: '@tanstack/react-start',
67 case: 'minimal',
68 },
69 {
70 id: 'react-start.query-integration',
71 dir: 'react-start-query-integration',
72 framework: 'react',
73 packageName: '@tanstack/react-start',
74 buildProjects: ['@tanstack/react-router-ssr-query'],
75 case: 'query-integration',
76 },
77 {
78 id: 'react-start.deferred-hydration',
79 dir: 'react-start-deferred-hydration',
80 framework: 'react',
81 packageName: '@tanstack/react-start',
82 case: 'deferred-hydration',
83 },
84 {
85 id: 'react-start.full',
86 dir: 'react-start-full',
87 framework: 'react',
88 packageName: '@tanstack/react-start',
89 case: 'full',
90 },
91 {
92 id: 'react-start.rsbuild.minimal',
93 dir: 'react-start-minimal',
94 outDir: 'react-start-rsbuild-minimal',
95 toolchain: 'rsbuild',
96 framework: 'react',
97 packageName: '@tanstack/react-start',
98 case: 'minimal',
99 },
100 {
101 id: 'react-start.rsbuild.minimal-iife',
102 dir: 'react-start-minimal',
103 outDir: 'react-start-rsbuild-minimal-iife',
104 toolchain: 'rsbuild',
105 rsbuildClientOutput: 'iife',
106 framework: 'react',
107 packageName: '@tanstack/react-start',
108 case: 'minimal-iife',
109 },
110 {
111 id: 'react-start.rsbuild.full',
112 dir: 'react-start-full',
113 outDir: 'react-start-rsbuild-full',
114 toolchain: 'rsbuild',
115 framework: 'react',
116 packageName: '@tanstack/react-start',
117 case: 'full',
118 },
119 {
120 id: 'solid-start.minimal',
121 dir: 'solid-start-minimal',
122 framework: 'solid',
123 packageName: '@tanstack/solid-start',
124 case: 'minimal',
125 },
126 {
127 id: 'solid-start.deferred-hydration',
128 dir: 'solid-start-deferred-hydration',
129 framework: 'solid',
130 packageName: '@tanstack/solid-start',
131 case: 'deferred-hydration',
132 },
133 {
134 id: 'solid-start.full',
135 dir: 'solid-start-full',
136 framework: 'solid',
137 packageName: '@tanstack/solid-start',
138 case: 'full',
139 },
140 {
141 id: 'vue-start.minimal',
142 dir: 'vue-start-minimal',
143 framework: 'vue',
144 packageName: '@tanstack/vue-start',
145 case: 'minimal',
146 },
147 {
148 id: 'vue-start.full',
149 dir: 'vue-start-full',
150 framework: 'vue',
151 packageName: '@tanstack/vue-start',
152 case: 'full',
153 },
155​
156function parseArgs(argv) {
157 const { values } = parseNodeArgs({
158 args: argv,
159 allowPositionals: false,
160 strict: true,
161 options: {
162 sha: { type: 'string' },
163 'measured-at': { type: 'string' },
164 'append-history': { type: 'string' },
165 'results-dir': { type: 'string' },
166 'dist-dir': { type: 'string' },
167 scenario: { type: 'string' },
168 analysis: { type: 'boolean' },
169 sourcemap: { type: 'boolean' },
170 'skip-package-builds': { type: 'boolean' },
171 'measurements-only': { type: 'boolean' },
172 timings: { type: 'boolean' },
173 },
174 })
175​
176 return {
177 sha: values.sha,
178 measuredAt: values['measured-at'],
179 appendHistory: values['append-history'],
180 resultsDir: values['results-dir'],
181 distDir: values['dist-dir'],
182 scenario: values.scenario,
183 analysis: values.analysis === true,
184 sourcemap: values.sourcemap === true,
185 skipPackageBuilds: values['skip-package-builds'] === true,
186 measurementsOnly: values['measurements-only'] === true,
187 timings: values.timings === true || process.env.BUNDLE_SIZE_TIMINGS === '1',
188 }
190​
191function filterScenarios(filter) {
192 if (!filter) {
193 return SCENARIOS
194 }
195​
196 const requested = filter
197 .split(',')
198 .map((value) => value.trim())
199 .filter(Boolean)
200 const scenarios = SCENARIOS.filter((scenario) => {
201 return requested.some(
202 (value) =>
203 value === scenario.id ||
204 value === scenario.dir ||
205 value === scenario.outDir ||
206 value === `${scenario.framework}-${scenario.case}`,
207 )
208 })
209​
210 const missing = requested.filter(
211 (value) =>
212 !SCENARIOS.some(
213 (scenario) =>
214 value === scenario.id ||
215 value === scenario.dir ||
216 value === scenario.outDir ||
217 value === `${scenario.framework}-${scenario.case}`,
218 ),
219 )
220​
221 if (missing.length > 0) {
222 throw new Error(
223 `Unknown bundle-size scenario: ${missing.join(', ')}\nKnown scenarios: ${SCENARIOS.map((scenario) => scenario.id).join(', ')}`,
224 )
225 }
226​
227 return scenarios
229​
230function toIsoDate(value) {
231 const date = new Date(value)
232​
233 if (Number.isNaN(date.getTime())) {
234 throw new Error(`Invalid date: ${value}`)
235 }
236​
237 return date.toISOString()
239​
240function readJson(filePath) {
241 return JSON.parse(fs.readFileSync(filePath, 'utf8'))
243​
244function parseMaybeDataJs(raw) {
245 const trimmed = raw.trim()
246​
247 if (trimmed.startsWith('window.BENCHMARK_DATA')) {
248 const sandbox = { window: {} }
249 vm.runInNewContext(trimmed, sandbox, { timeout: 1000 })
250 return sandbox.window.BENCHMARK_DATA
251 }
252​
253 return JSON.parse(trimmed)
255​
256function resolveManifestChunkKey(manifest, keyOrFile) {
257 if (manifest[keyOrFile]) {
258 return keyOrFile
259 }
260​
261 for (const [key, value] of Object.entries(manifest)) {
262 if (value?.file === keyOrFile) {
263 return key
264 }
265 }
266​
267 return undefined
269​
270function isJsFile(file) {
271 return file.endsWith('.js') || file.endsWith('.mjs')
273​
274function collectInitialJsFiles(manifest, entryKey) {
275 const visitedKeys = new Set()
276 const files = new Set()
277​
278 function visitByKey(chunkKey) {
279 if (!chunkKey || visitedKeys.has(chunkKey)) {
280 return
281 }
282​
283 visitedKeys.add(chunkKey)
284 const chunk = manifest[chunkKey]
285​
286 if (!chunk) {
287 return
288 }
289​
290 if (typeof chunk.file === 'string' && isJsFile(chunk.file)) {
291 files.add(chunk.file)
292 }
293​
294 for (const imported of chunk.imports || []) {
295 const resolvedKey = resolveManifestChunkKey(manifest, imported)
296 if (resolvedKey) {
297 visitByKey(resolvedKey)
298 }
299 }
300 }
301​
302 visitByKey(entryKey)
303​
304 return [...files].sort()
306​
307function collectAllViteJsFiles(manifest) {
308 const files = new Set()
309​
310 for (const chunk of Object.values(manifest)) {
311 if (typeof chunk?.file === 'string' && isJsFile(chunk.file)) {
312 files.add(chunk.file)
313 }
314 }
315​
316 return [...files].sort()
318​
319async function findManifestFiles(rootDir) {
320 const manifestPaths = []
321​
322 async function visit(currentDir) {
323 const entries = await fsp.readdir(currentDir, { withFileTypes: true })
324​
325 for (const entry of entries) {
326 const fullPath = path.join(currentDir, entry.name)
327​
328 if (entry.isDirectory()) {
329 await visit(fullPath)
330 continue
331 }
332​
333 if (entry.isFile() && entry.name === 'manifest.json') {
334 const parent = path.basename(path.dirname(fullPath))
335 if (parent === '.vite') {
336 manifestPaths.push(fullPath)
337 }
338 }
339 }
340 }
341​
342 await visit(rootDir)
343 return manifestPaths
345​
346function rankManifestPath(manifestPath) {
347 let score = 0
348​
349 if (manifestPath.includes(`${path.sep}client${path.sep}`)) {
350 score += 100
351 }
352​
353 if (manifestPath.includes(`${path.sep}server${path.sep}`)) {
354 score -= 100
355 }
356​
357 return score
359​
360function pickManifestEntryKey(manifest) {
361 const entries = Object.entries(manifest)
362​
363 const htmlEntry = entries.find(
364 ([key, value]) => key.endsWith('.html') && value?.isEntry,
365 )
366 if (htmlEntry) {
367 return htmlEntry[0]
368 }
369​
370 const jsEntries = entries.filter(
371 ([, value]) =>
372 value?.isEntry &&
373 typeof value.file === 'string' &&
374 value.file.endsWith('.js'),
375 )
376​
377 const preferredPatterns = [
378 /virtual:tanstack-start-client-entry/i,
379 /src[\\/]main\./i,
380 /src[\\/]client\./i,
381 /client/i,
382 ]
383​
384 for (const pattern of preferredPatterns) {
385 const preferred = jsEntries.find(
386 ([key, value]) => pattern.test(key) || pattern.test(value.file),
387 )
388​
389 if (preferred) {
390 return preferred[0]
391 }
392 }
393​
394 if (jsEntries.length > 0) {
395 return jsEntries[0][0]
396 }
397​
398 const anyEntry = entries.find(([, value]) => value?.isEntry)
399 if (anyEntry) {
400 return anyEntry[0]
401 }
402​
403 return undefined
405​
406async function resolveManifestAndEntry(outDir, scenarioId) {
407 const manifestPaths = await findManifestFiles(outDir)
408​
409 if (manifestPaths.length === 0) {
410 throw new Error(`No Vite manifest files found for scenario: ${scenarioId}`)
411 }
412​
413 manifestPaths.sort((a, b) => {
414 const scoreDiff = rankManifestPath(b) - rankManifestPath(a)
415 if (scoreDiff !== 0) {
416 return scoreDiff
417 }
418​
419 return a.length - b.length
420 })
421​
422 for (const manifestPath of manifestPaths) {
423 const manifest = readJson(manifestPath)
424 const entryKey = pickManifestEntryKey(manifest)
425​
426 if (!entryKey) {
427 continue
428 }
429​
430 const manifestOutDir = path.dirname(path.dirname(manifestPath))
431​
432 return {
433 manifest,
434 entryKey,
435 manifestPath,
436 manifestOutDir,
437 }
438 }
439​
440 throw new Error(
441 `Could not determine manifest entry for scenario: ${scenarioId}`,
442 )
444​
445async function importFromRoot(root, specifier) {
446 const requireFromRoot = createRequire(
447 path.join(root, 'bundle-size.config.cjs'),
448 )
449 return import(pathToFileURL(requireFromRoot.resolve(specifier)).href)
451​
452function getPackageBuildProjects(scenarios) {
453 const projects = new Set()
454​
455 for (const scenario of scenarios) {
456 projects.add(scenario.packageName)
457​
458 for (const project of scenario.buildProjects || []) {
459 projects.add(project)
460 }
461​
462 if (scenario.packageName.endsWith('-router')) {
463 projects.add('@tanstack/router-plugin')
464 }
465 }
466​
467 return [...projects].sort()
469​
470function buildRequiredPackages({ repoRoot, scenarios, skipPackageBuilds }) {
471 const projects = getPackageBuildProjects(scenarios)
472​
473 if (skipPackageBuilds || projects.length === 0) {
474 return projects
475 }
476​
477 process.stdout.write(
478 `Building package projects for bundle-size scenarios: ${projects.join(', ')}\n`,
479 )
480​
481 const args = [
482 'nx',
483 'run-many',
484 '--target=build',
485 `--projects=${projects.join(',')}`,
486 '--outputStyle=stream',
487 '--skipRemoteCache',
488 ]
489​
490 if (
491 process.env.NX_SKIP_NX_CACHE === 'true' ||
492 process.env.NX_DISABLE_NX_CACHE === 'true'
493 ) {
494 args.push('--skipNxCache')
495 }
496​
497 execFileSync(process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm', args, {
498 cwd: repoRoot,
499 stdio: 'inherit',
500 env: process.env,
501 })
502​
503 return projects
505​
506async function buildViteScenario({ root, outDir, sourcemap }) {
507 const configFile = path.join(root, 'vite.config.ts')
508​
509 await build({
510 root,
511 configFile,
512 logLevel: 'silent',
513 define: {
514 'process.env.NODE_ENV': '"production"',
515 },
516 build: {
517 outDir,
518 emptyOutDir: true,
519 sourcemap: sourcemap ? 'hidden' : false,
520 reportCompressedSize: false,
521 manifest: true,
522 },
523 })
525​
526async function buildRsbuildScenario({ root, outDir, scenario, sourcemap }) {
527 const configFile = path.join(root, 'rsbuild.config.ts')
528 const { createRsbuild, loadConfig } = await importFromRoot(
529 root,
530 '@rsbuild/core',
531 )
532 const previousOutDir = process.env.BUNDLE_SIZE_DIST_DIR
533 const previousClientOutput = process.env.BUNDLE_SIZE_RSB_CLIENT_OUTPUT
534​
535 process.env.BUNDLE_SIZE_DIST_DIR = outDir
536 if (scenario.rsbuildClientOutput) {
537 process.env.BUNDLE_SIZE_RSB_CLIENT_OUTPUT = scenario.rsbuildClientOutput
538 } else {
539 delete process.env.BUNDLE_SIZE_RSB_CLIENT_OUTPUT
540 }
541​
542 try {
543 const { content } = await loadConfig({
544 cwd: root,
545 path: configFile,
546 envMode: 'production',
547 })
548 const rsbuild = await createRsbuild({
549 cwd: root,
550 callerName: 'bundle-size-benchmark',
551 config: sourcemap
552 ? {
553 ...content,
554 output: {
555 ...content.output,
556 sourceMap: {
557 js: 'source-map',
558 },
559 },
560 }
561 : content,
562 })
563 const result = await rsbuild.build()
564 await result.close()
565 } finally {
566 if (previousOutDir === undefined) {
567 delete process.env.BUNDLE_SIZE_DIST_DIR
568 } else {
569 process.env.BUNDLE_SIZE_DIST_DIR = previousOutDir
570 }
571 if (previousClientOutput === undefined) {
572 delete process.env.BUNDLE_SIZE_RSB_CLIENT_OUTPUT
573 } else {
574 process.env.BUNDLE_SIZE_RSB_CLIENT_OUTPUT = previousClientOutput
575 }
576 }
578​
579async function buildScenario({ root, outDir, scenario, sourcemap }) {
580 const previousCwd = process.cwd()
581 process.chdir(root)
582​
583 try {
584 if (scenario.toolchain === 'rsbuild') {
585 await buildRsbuildScenario({ root, outDir, scenario, sourcemap })
586 return
587 }
588​
589 await buildViteScenario({ root, outDir, sourcemap })
590 } finally {
591 process.chdir(previousCwd)
592 }
594​
595async function resolveRsbuildManifest(outDir, scenarioId) {
596 const manifestPath = path.join(outDir, 'client', 'manifest.json')
597​
598 if (!fs.existsSync(manifestPath)) {
599 throw new Error(
600 `No Rsbuild manifest file found for scenario: ${scenarioId}`,
601 )
602 }
603​
604 const manifest = readJson(manifestPath)
605 const entryName = manifest.entries?.index
606 ? 'index'
607 : Object.keys(manifest.entries || {})[0]
608​
609 if (!entryName) {
610 throw new Error(
611 `Could not determine Rsbuild manifest entry for scenario: ${scenarioId}`,
612 )
613 }
614​
615 return {
616 manifest,
617 entryKey: entryName,
618 manifestPath,
619 manifestOutDir: path.dirname(manifestPath),
620 }
622​
623function collectRsbuildInitialJsFiles(manifest, entryKey) {
624 const files = manifest.entries?.[entryKey]?.initial?.js
625​
626 if (!Array.isArray(files)) {
627 return []
628 }
629​
630 return files
631 .filter((file) => typeof file === 'string' && isJsFile(file))
632 .sort()
634​
635function collectRsbuildAllJsFiles(manifest) {
636 const files = new Set()
637​
638 for (const file of manifest.allFiles || []) {
639 if (typeof file === 'string' && isJsFile(file)) {
640 files.add(file)
641 }
642 }
643​
644 if (files.size === 0) {
645 for (const entry of Object.values(manifest.entries || {})) {
646 for (const file of entry?.initial?.js || []) {
647 if (typeof file === 'string' && isJsFile(file)) {
648 files.add(file)
649 }
650 }
651 for (const file of entry?.async?.js || []) {
652 if (typeof file === 'string' && isJsFile(file)) {
653 files.add(file)
654 }
655 }
656 }
657 }
658​
659 return [...files].sort()
661​
662function decodeVlq(segment) {
663 const values = []
664 let value = 0
665 let shift = 0
666​
667 for (const char of segment) {
668 let digit =
669 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.indexOf(
670 char,
671 )
672​
673 if (digit < 0) {
674 continue
675 }
676​
677 const continuation = digit & 32
678 digit &= 31
679 value += digit << shift
680​
681 if (continuation) {
682 shift += 5
683 continue
684 }
685​
686 const negative = value & 1
687 value >>= 1
688 values.push(negative ? -value : value)
689 value = 0
690 shift = 0
691 }
692​
693 return values
695​
696function estimateSourceBytesFromMap(mapPath, generatedPath) {
697 if (!fs.existsSync(mapPath)) {
698 return []
699 }
700​
701 const map = readJson(mapPath)
702 const sources = map.sources || []
703 const sourceBytes = new Map()
704 let sourceIndex = 0
705 const generatedLines = fs.existsSync(generatedPath)
706 ? fs.readFileSync(generatedPath, 'utf8').split('\n')
707 : []
708 let lineIndex = 0
709​
710 for (const line of String(map.mappings || '').split(';')) {
711 let generatedColumn = 0
712 let previousColumn = 0
713 let previousSource = -1
714 const segments = line.split(',').filter(Boolean)
715 const decodedSegments = []
716​
717 for (const segment of segments) {
718 const values = decodeVlq(segment)
719 generatedColumn += values[0] || 0
720​
721 if (values.length >= 4) {
722 sourceIndex += values[1] || 0
723 decodedSegments.push({ column: generatedColumn, sourceIndex })
724 }
725 }
726​
727 for (const segment of decodedSegments) {
728 if (previousSource >= 0) {
729 const bytes = Math.max(0, segment.column - previousColumn)
730 sourceBytes.set(
731 sources[previousSource],
732 (sourceBytes.get(sources[previousSource]) || 0) + bytes,
733 )
734 }
735​
736 previousColumn = segment.column
737 previousSource = segment.sourceIndex
738 }
739​
740 if (previousSource >= 0) {
741 const bytes = Math.max(
742 1,
743 (generatedLines[lineIndex]?.length || previousColumn) - previousColumn,
744 )
745 sourceBytes.set(
746 sources[previousSource],
747 (sourceBytes.get(sources[previousSource]) || 0) + bytes,
748 )
749 }
750​
751 lineIndex++
752 }
753​
754 return [...sourceBytes]
755 .map(([source, estimatedBytes]) => ({ source, estimatedBytes }))
756 .sort((a, b) => b.estimatedBytes - a.estimatedBytes)
758​
759function sourceAttributionForFiles(baseDir, fileList) {
760 return fileList.map((file) => {
761 return {
762 file,
763 sources: estimateSourceBytesFromMap(
764 path.join(baseDir, `${file}.map`),
765 path.join(baseDir, file),
766 ),
767 }
768 })
770​
771async function resolveBundleFiles({ outDir, scenario }) {
772 if (scenario.toolchain === 'rsbuild') {
773 const manifestInfo = await resolveRsbuildManifest(outDir, scenario.id)
774 const initialJsFiles = collectRsbuildInitialJsFiles(
775 manifestInfo.manifest,
776 manifestInfo.entryKey,
777 )
778 const jsFiles = collectRsbuildAllJsFiles(manifestInfo.manifest)
779​
780 return {
781 ...manifestInfo,
782 initialJsFiles,
783 jsFiles,
784 }
785 }
786​
787 const manifestInfo = await resolveManifestAndEntry(outDir, scenario.id)
788 const initialJsFiles = collectInitialJsFiles(
789 manifestInfo.manifest,
790 manifestInfo.entryKey,
791 )
792 const jsFiles = collectAllViteJsFiles(manifestInfo.manifest)
793​
794 return {
795 ...manifestInfo,
796 initialJsFiles,
797 jsFiles,
798 }
800​
801function buildCommitUrl(sha) {
802 const repo = process.env.GITHUB_REPOSITORY
803​
804 if (!repo) {
805 return ''
806 }
807​
808 return `https://github.com/${repo}/commit/${sha}`
810​
811async function appendHistoryFile({ historyPath, measuredAtIso, sha, benches }) {
812 const measuredAtMs = Date.parse(measuredAtIso)
813 let writeAsDataJs = historyPath.endsWith('.js')
814​
815 let history = {
816 lastUpdate: measuredAtMs,
817 entries: {},
818 }
819​
820 if (fs.existsSync(historyPath)) {
821 const raw = await fsp.readFile(historyPath, 'utf8')
822 writeAsDataJs = raw.trim().startsWith('window.BENCHMARK_DATA')
823 history = parseMaybeDataJs(raw)
824 history.entries ||= {}
825 }
826​
827 const entry = {
828 commit: {
829 id: sha,
830 message: `bundle-size snapshot ${sha.slice(0, 12)}`,
831 timestamp: measuredAtIso,
832 url: buildCommitUrl(sha),
833 },
834 date: measuredAtMs,
835 tool: 'customSmallerIsBetter',
836 benches,
837 }
838​
839 const group = history.entries[BENCHMARK_NAME] || []
840 group.push(entry)
841 history.entries[BENCHMARK_NAME] = group
842 history.lastUpdate = measuredAtMs
843​
844 await fsp.mkdir(path.dirname(historyPath), { recursive: true })
845 const serialized = JSON.stringify(history, null, 2)
846 const output = writeAsDataJs
847 ? `window.BENCHMARK_DATA = ${serialized};\n`
848 : `${serialized}\n`
849 await fsp.writeFile(historyPath, output, 'utf8')
851​
852async function main() {
853 const args = parseArgs(process.argv.slice(2))
854 const timings = args.timings ? createTimings() : undefined
855 const finishOverall = timings?.start('overall')
856 try {
857 await measure(args, timings)
858 } finally {
859 finishOverall?.()
860 }
862​
863async function measure(args, timings) {
864 if (
865 args.measurementsOnly &&
866 (args.sha || args.measuredAt || args.appendHistory)
867 ) {
868 throw new Error(
869 '--measurements-only cannot be combined with report metadata or history options',
870 )
871 }
872​
873 const scriptDir = path.dirname(fileURLToPath(import.meta.url))
874 const repoRoot = path.resolve(scriptDir, '../../../')
875 const scenariosRoot = path.join(repoRoot, 'benchmarks/bundle-size/scenarios')
876 const resultsDir = args.resultsDir
877 ? path.resolve(args.resultsDir)
878 : path.join(repoRoot, 'benchmarks/bundle-size/results')
879 const distDir = args.distDir
880 ? path.resolve(args.distDir)
881 : path.join(repoRoot, 'benchmarks/bundle-size/dist')
882​
883 const measuredAtIso = args.measuredAt
884 ? toIsoDate(args.measuredAt)
885 : new Date().toISOString()
886 const startedAt = Date.now()
887 const scenarios = filterScenarios(args.scenario)
888 const finishPackageBuilds = args.skipPackageBuilds
889 ? undefined
890 : timings?.start('package-builds')
891 let packageBuildProjects
892 try {
893 packageBuildProjects = buildRequiredPackages({
894 repoRoot,
895 scenarios,
896 skipPackageBuilds: args.skipPackageBuilds,
897 })
898 } finally {
899 finishPackageBuilds?.()
900 }
901​
902 await fsp.mkdir(resultsDir, { recursive: true })
903 await fsp.mkdir(distDir, { recursive: true })
904​
905 const metrics = []
906​
907 for (const scenario of scenarios) {
908 const root = path.join(scenariosRoot, scenario.dir)
909 const outDir = path.join(distDir, scenario.outDir || scenario.dir)
910​
911 const finishBuild = timings?.start('scenario-build', scenario.id)
912 try {
913 await buildScenario({
914 root,
915 outDir,
916 scenario,
917 sourcemap: args.sourcemap || args.analysis,
918 })
919 } finally {
920 finishBuild?.()
921 }
922​
923 const finishManifest = timings?.start('manifest-resolution', scenario.id)
924 let bundleInfo
925 try {
926 bundleInfo = await resolveBundleFiles({ outDir, scenario })
927 } finally {
928 finishManifest?.()
929 }
930 const { sizes, initialSizes } = measureFileSizes(
931 bundleInfo.manifestOutDir,
932 bundleInfo.jsFiles,
933 bundleInfo.initialJsFiles,
934 timings
935 ? (phase, durationMs) => timings.record(phase, durationMs, scenario.id)
936 : undefined,
937 )
938 const initialFileSet = new Set(bundleInfo.initialJsFiles)
939 const files = sizes.files.map((file) => ({
940 ...file,
941 initial: initialFileSet.has(file.file),
942 }))
943 const metric = {
944 id: scenario.id,
945 scenarioDir: scenario.dir,
946 outDir: scenario.outDir || scenario.dir,
947 toolchain: scenario.toolchain || 'vite',
948 framework: scenario.framework,
949 packageName: scenario.packageName,
950 case: scenario.case,
951 entryKey: bundleInfo.entryKey,
952 manifestPath: path.relative(outDir, bundleInfo.manifestPath),
953 manifestOutDir: path.relative(repoRoot, bundleInfo.manifestOutDir),
954 initialJsFiles: bundleInfo.initialJsFiles,
955 jsFiles: bundleInfo.jsFiles,
956 files,
957 initialRawBytes: initialSizes.rawBytes,
958 initialGzipBytes: initialSizes.gzipBytes,
959 initialBrotliBytes: initialSizes.brotliBytes,
960 rawBytes: sizes.rawBytes,
961 gzipBytes: sizes.gzipBytes,
962 brotliBytes: sizes.brotliBytes,
963 }
964​
965 if (args.analysis) {
966 const finishAnalysis = timings?.start('analysis', scenario.id)
967 try {
968 metric.sources = sourceAttributionForFiles(
969 bundleInfo.manifestOutDir,
970 bundleInfo.jsFiles,
971 )
972 } finally {
973 finishAnalysis?.()
974 }
975 }
976​
977 metrics.push(metric)
978 }
979 const completedAt = Date.now()
980​
981 const measurements = {
982 schemaVersion: 1,
983 benchmarkName: BENCHMARK_NAME,
984 measuredAt: measuredAtIso,
985 status: {
986 state: 'success',
987 command: `node ${path.relative(repoRoot, fileURLToPath(import.meta.url))}${args.scenario ? ` --scenario ${args.scenario}` : ''}${args.analysis ? ' --analysis' : ''}${args.sourcemap ? ' --sourcemap' : ''}${args.timings ? ' --timings' : ''}${args.skipPackageBuilds ? ' --skip-package-builds' : ''}`,
988 scenarioFilter: args.scenario || null,
989 measuredScenarios: scenarios.map((scenario) => scenario.id),
990 packageBuildProjects,
991 skipPackageBuilds: args.skipPackageBuilds,
992 durationMs: completedAt - startedAt,
993 },
994 metrics,
995 }
996​
997 const benchmarkActionRows = metrics.map((metric) => ({
998 name: metric.id,
999 unit: 'bytes',
1000 value: metric.gzipBytes,
1001 extra: `raw=${metric.rawBytes}; brotli=${metric.brotliBytes}; initial_gzip=${metric.initialGzipBytes}`,
1002 }))
1003​
1004 const measurementsPath = path.join(resultsDir, 'measurements.json')
1005 const benchmarkActionPath = path.join(resultsDir, 'benchmark-action.json')
1006​
1007 await fsp.writeFile(
1008 measurementsPath,
1009 JSON.stringify(measurements, null, 2) + '\n',
1010 'utf8',
1012 await fsp.writeFile(
1013 benchmarkActionPath,
1014 JSON.stringify(benchmarkActionRows, null, 2) + '\n',
1015 'utf8',
1017​
1018 if (!args.measurementsOnly) {
1019 const report = writeReport(resultsDir, { sha: args.sha })
1020 if (args.appendHistory) {
1021 await appendHistoryFile({
1022 historyPath: path.resolve(args.appendHistory),
1023 measuredAtIso,
1024 sha: report.sha,
1025 benches: benchmarkActionRows,
1026 })
1029​
1030 process.stdout.write(
1031 `Measured ${metrics.length} scenarios. Wrote ${path.relative(repoRoot, measurementsPath)} and ${path.relative(repoRoot, benchmarkActionPath)}\n`,
1034​
1035main().catch((error) => {
1036 console.error(error)
1037 process.exit(1)