22// Design mode (design-mode.tsx, design-overlay.tsx) or the device preview (device-preview.tsx,
33// device-preview-panel.tsx). They load only on the development server, through loaders a production
44// build compiles away: `import.meta.env.DEV ? lazy(() => import("./settings-panel")) : undefined`.
5+ // Also fails when server-only code reaches it: the Worker's cache, GitHub calls and the build-time
6+ // GitHub snapshot. Pages may import their modules for the queries, whose "use server" bodies the
7+ // browser build drops along with everything only those bodies read.
8+ // Last, it fails when a page's initial JavaScript (the entry and every chunk the page's route imports
9+ // statically, which the server modulepreloads) passes its budget below, gzipped. Code a page needs
10+ // only later belongs behind a dynamic import.
511// Run after `vite build`, optionally with the output directory: `bun script/check-client-bundle.ts .output`.
612import path from "node:path"
713import { Glob } from "bun"
@@ -16,13 +22,15 @@ const scriptMarkers = [
1622 'opencode-site-device-preview"' , // device-preview-panel.tsx: the saved device
1723 "Toggle Design mode" , // search-index.ts: the ⌘K action
1824 "Open Settings" , // search-index.ts: the ⌘K action
25+ "edge-cache.opencode.internal" , // lib/edge-cache.ts
26+ "api.github.com" , // lib/changelog-releases.ts
1927]
2028// The tools' styles (settings-panel.css, design-mode.css, device-preview.css).
2129const styleMarker =
2230 / \[ d a t a - c o m p o n e n t = " ? ( s e t t i n g s - p a n e l | s e t t i n g s - b a c k d r o p | d e s i g n - o v e r l a y | d e s i g n - c o n t r o l s | d e s i g n - e x p o r t | h i s t o r y - t o a s t | d e v i c e - p r e v i e w ) " ? \] /
23- // Chunks named after the tools' modules.
31+ // Chunks named after the tools' modules, or after the GitHub snapshot (lib/github-snapshot.json) .
2432const chunkName =
25- / ( ^ | \/ ) ( d e s i g n - m o d e | d e s i g n - o v e r l a y | s e t t i n g s - p a n e l | d e v i c e - p r e v i e w | d e v i c e - p r e v i e w - p a n e l | p a g e - s e t t i n g s | s e t t i n g s - t r a n s f e r ) - [ \w - ] + \. ( j s | c s s ) $ /
33+ / ( ^ | \/ ) ( d e s i g n - m o d e | d e s i g n - o v e r l a y | s e t t i n g s - p a n e l | d e v i c e - p r e v i e w | d e v i c e - p r e v i e w - p a n e l | p a g e - s e t t i n g s | s e t t i n g s - t r a n s f e r | g i t h u b - s n a p s h o t ) - [ \w - ] + \. ( j s | c s s ) $ /
2634
2735const files = await Array . fromAsync ( new Glob ( "**/*.{js,mjs,css}" ) . scan ( { cwd : root } ) )
2836if ( ! files . length ) {
@@ -32,7 +40,7 @@ if (!files.length) {
3240const found = (
3341 await Promise . all (
3442 files . map ( async ( file ) => {
35- if ( chunkName . test ( file ) ) return [ `${ file } : a development tool's chunk` ]
43+ if ( chunkName . test ( file ) ) return [ `${ file } : a development tool's or server-only chunk` ]
3644 const text = await Bun . file ( path . join ( root , file ) ) . text ( )
3745 if ( file . endsWith ( ".css" ) ) {
3846 const match = text . match ( styleMarker )
@@ -44,9 +52,77 @@ const found = (
4452) . flat ( )
4553if ( found . length ) {
4654 console . error (
47- "check-client-bundle: Settings, Design mode or device preview code is in the client bundle:\n " +
55+ "check-client-bundle: Settings, Design mode, device preview or server-only code is in the client bundle:\n " +
4856 found . join ( "\n " ) ,
4957 )
5058 process . exit ( 1 )
5159}
52- console . log ( `check-client-bundle: ${ files . length } client files, no Settings, Design mode or device preview code` )
60+ console . log (
61+ `check-client-bundle: ${ files . length } client files, no Settings, Design mode, device preview or server-only code` ,
62+ )
63+
64+ // Each page's budget in kB, gzipped: what it was when last measured, plus a kB of room. Lower a
65+ // budget when a page gets lighter; a page without one fails until it has one.
66+ const budgets : Record < string , number > = {
67+ "[...404].tsx" : 144 ,
68+ "brand/index.tsx" : 153 ,
69+ "changelog/[tag].tsx" : 167 ,
70+ "changelog/index.tsx" : 166 ,
71+ "desktop.tsx" : 178 ,
72+ "devices.tsx" : 83 ,
73+ "extensions/browse.tsx" : 155 ,
74+ "font/index.tsx" : 174 ,
75+ "ide.tsx" : 178 ,
76+ "index.tsx" : 183 ,
77+ "legal/index.tsx" : 193 ,
78+ "legal/privacy-policy/index.tsx" : 193 ,
79+ "legal/terms-of-service/index.tsx" : 193 ,
80+ "mini.tsx" : 178 ,
81+ "mobile.tsx" : 178 ,
82+ "press/index.tsx" : 155 ,
83+ "product/index.tsx" : 168 ,
84+ "sdk.tsx" : 178 ,
85+ "terminal.tsx" : 178 ,
86+ "video-editor/index.tsx" : 81 ,
87+ }
88+ type Chunk = { file : string ; imports ?: string [ ] }
89+ const manifest : Record < string , Chunk > = await Bun . file ( path . join ( root , ".vite/manifest.json" ) ) . json ( )
90+ const sizes = new Map < string , number > ( )
91+ const gzipped = async ( file : string ) => {
92+ const known = sizes . get ( file )
93+ if ( known !== undefined ) return known
94+ const size = Bun . gzipSync ( await Bun . file ( path . join ( root , file ) ) . bytes ( ) , { level : 9 } ) . length
95+ sizes . set ( file , size )
96+ return size
97+ }
98+ const closure = ( key : string , seen = new Set < string > ( ) ) : Set < string > => {
99+ if ( seen . has ( key ) ) return seen
100+ seen . add ( key )
101+ manifest [ key ] . imports ?. forEach ( ( next ) => closure ( next , seen ) )
102+ return seen
103+ }
104+ const entry = closure ( "src/entry-client.tsx" )
105+ const pages = await Promise . all (
106+ Object . keys ( manifest )
107+ . filter ( ( key ) => key . startsWith ( "src/routes/" ) && key . includes ( "?pick=default" ) )
108+ . map ( async ( key ) => {
109+ const chunks = [ ...new Set ( [ ...entry , ...closure ( key ) ] ) ] . map ( ( chunk ) => manifest [ chunk ] . file )
110+ const bytes = ( await Promise . all ( chunks . map ( gzipped ) ) ) . reduce ( ( sum , size ) => sum + size , 0 )
111+ return { route : key . slice ( "src/routes/" . length ) . split ( "?" ) [ 0 ] , kB : Math . round ( bytes / 100 ) / 10 }
112+ } ) ,
113+ )
114+ const over = pages . filter ( ( page ) => ! ( page . kB <= budgets [ page . route ] ) )
115+ if ( over . length ) {
116+ console . error (
117+ "check-client-bundle: initial JavaScript (gzipped) over its page's budget:\n " +
118+ over . map ( ( page ) => `${ page . route } : ${ page . kB } kB, budget ${ budgets [ page . route ] ?? "none" } ` ) . join ( "\n " ) ,
119+ )
120+ process . exit ( 1 )
121+ }
122+ console . log (
123+ "check-client-bundle: initial JavaScript within budget (kB gzipped): " +
124+ pages
125+ . toSorted ( ( a , b ) => b . kB - a . kB )
126+ . map ( ( page ) => `${ page . route } ${ page . kB } ` )
127+ . join ( ", " ) ,
128+ )
0 commit comments