@@ -59,6 +59,48 @@ function sumGroup(files) {
5959 return { raw, gzip, count : files . length } ;
6060}
6161
62+ function addRouteStats ( stats ) {
63+ const routeStatsFile = path . join (
64+ nextDir ,
65+ 'diagnostics' ,
66+ 'route-bundle-stats.json'
67+ ) ;
68+ if ( ! fs . existsSync ( routeStatsFile ) ) return ;
69+
70+ const routeStats = JSON . parse ( fs . readFileSync ( routeStatsFile , 'utf8' ) ) ;
71+ const representativeRoutes = {
72+ 'First-load JS: Home' : '/' ,
73+ 'First-load JS: Learn' : '/learn/[[...slug]]' ,
74+ 'First-load JS: Reference' : '/reference/[[...slug]]' ,
75+ 'First-load JS: Community' : '/community/[[...slug]]' ,
76+ 'First-load JS: Blog' : '/blog/[[...slug]]' ,
77+ } ;
78+
79+ for ( const [ label , route ] of Object . entries ( representativeRoutes ) ) {
80+ const entry = routeStats . find ( ( item ) => item . route === route ) ;
81+ if ( ! entry ) continue ;
82+ const files = entry . firstLoadChunkPaths
83+ . map ( ( file ) => path . join ( root , file ) )
84+ . filter ( ( file ) => fs . existsSync ( file ) ) ;
85+ stats [ label ] = sumGroup ( files ) ;
86+ }
87+ }
88+
89+ function addHtmlStats ( stats ) {
90+ const representativePages = {
91+ 'HTML: Home' : 'index.html' ,
92+ 'HTML: Effects guide' : 'learn/synchronizing-with-effects.html' ,
93+ 'HTML: useState' : 'reference/react/useState.html' ,
94+ } ;
95+
96+ for ( const [ label , file ] of Object . entries ( representativePages ) ) {
97+ const fullPath = path . join ( nextDir , 'server' , 'app' , file ) ;
98+ if ( fs . existsSync ( fullPath ) ) {
99+ stats [ label ] = sumGroup ( [ fullPath ] ) ;
100+ }
101+ }
102+ }
103+
62104function report ( ) {
63105 const manifest = JSON . parse (
64106 fs . readFileSync ( path . join ( nextDir , 'build-manifest.json' ) , 'utf8' )
@@ -73,7 +115,7 @@ function report() {
73115 const jsFiles = walk ( path . join ( nextDir , 'static' , 'chunks' ) ) . filter ( ( f ) =>
74116 f . endsWith ( '.js' )
75117 ) ;
76- const cssFiles = walk ( path . join ( nextDir , 'static' , 'css' ) ) . filter ( ( f ) =>
118+ const cssFiles = walk ( path . join ( nextDir , 'static' ) ) . filter ( ( f ) =>
77119 f . endsWith ( '.css' )
78120 ) ;
79121
@@ -82,6 +124,8 @@ function report() {
82124 'Total JS' : sumGroup ( jsFiles ) ,
83125 'Total CSS' : sumGroup ( cssFiles ) ,
84126 } ;
127+ addRouteStats ( stats ) ;
128+ addHtmlStats ( stats ) ;
85129
86130 fs . mkdirSync ( analyzeDir , { recursive : true } ) ;
87131 fs . writeFileSync ( statsFile , JSON . stringify ( stats , null , 2 ) ) ;
@@ -110,7 +154,9 @@ function isNewFormat(obj) {
110154 const vals = obj && typeof obj === 'object' ? Object . values ( obj ) : [ ] ;
111155 return (
112156 vals . length > 0 &&
113- vals . every ( ( v ) => v && typeof v . gzip === 'number' && typeof v . count === 'number' )
157+ vals . every (
158+ ( v ) => v && typeof v . gzip === 'number' && typeof v . count === 'number'
159+ )
114160 ) ;
115161}
116162
0 commit comments