1414const fs = require ( 'node:fs' ) ;
1515const path = require ( 'node:path' ) ;
1616const { execSync } = require ( 'node:child_process' ) ;
17+ const {
18+ gzipBytes : gzipTextBytes ,
19+ measureComponentImport,
20+ bundleInputsByEntry,
21+ packageStylesheetPath,
22+ } = require ( './lib/component-size' ) ;
1723const {
1824 COMPONENT_PACKAGES ,
1925 componentExportsFromBarrel,
@@ -194,7 +200,17 @@ function componentExistsInBase(pkg, componentName) {
194200 } ) ;
195201 return true ;
196202 } catch {
197- return false ;
203+ // A nested component folder without an index barrel still exists in base.
204+ if ( pkg . layout === 'flat' ) return false ;
205+ try {
206+ const listing = execSync (
207+ `git ls-tree --name-only ${ baseBranch } -- ${ pkgSrc ( pkg ) } /${ componentName } /` ,
208+ { encoding : 'utf8' , stdio : 'pipe' } ,
209+ ) ;
210+ return listing . trim ( ) . length > 0 ;
211+ } catch {
212+ return false ;
213+ }
198214 }
199215}
200216
@@ -500,6 +516,113 @@ function getComplexity(pkg, componentName) {
500516 }
501517}
502518
519+ // A package's prebuilt stylesheet text, read once per tree.
520+ const stylesheetCache = new Map ( ) ;
521+ function packageStylesheet ( pkg ) {
522+ const file = packageStylesheetPath ( path . join ( process . cwd ( ) , pkg . dir ) ) ;
523+ if ( ! file ) return null ;
524+ if ( ! stylesheetCache . has ( file ) ) stylesheetCache . set ( file , fs . readFileSync ( file , 'utf8' ) ) ;
525+ return stylesheetCache . get ( file ) ;
526+ }
527+
528+ // Every canonical public component of a package, with the dist module that
529+ // defines its export.
530+ function packageTargets ( pkg ) {
531+ if ( pkg . layout === 'flat' ) {
532+ return getComponentNames ( pkg ) . map ( ( name ) => ( {
533+ key : `${ pkg . packageName } /${ name } ` ,
534+ pkg,
535+ name,
536+ distModule : `${ name } .js` ,
537+ } ) ) ;
538+ }
539+ const sourceDir = path . join ( process . cwd ( ) , pkgSrc ( pkg ) ) ;
540+ const exports = componentExportsFromBarrel ( path . join ( sourceDir , 'index.ts' ) , sourceDir ) ;
541+ const targets = [ ] ;
542+ for ( const name of nestedPackageComponentNames ( process . cwd ( ) , pkg ) ) {
543+ const file = exports . get ( name ) ;
544+ if ( ! file ) continue ;
545+ targets . push ( {
546+ key : `${ pkg . packageName } /${ name } ` ,
547+ pkg,
548+ name,
549+ distModule : path . relative ( sourceDir , file ) . replace ( / \. t s x ? $ / , '.js' ) ,
550+ } ) ;
551+ }
552+ return targets ;
553+ }
554+
555+ // Every canonical public component a change can affect: its own folder or
556+ // file changed, or its bundle, import() targets included, reaches a changed
557+ // module of its package, such as a shared hook or style module. Family folders
558+ // that hold several components yield each of them.
559+ function sizeTargets ( changedFiles ) {
560+ const withoutExtension = ( file ) => file . replace ( / \. ( t s x ? | j s x ? | m j s ) $ / , '' ) ;
561+ const targets = [ ] ;
562+ for ( const pkg of PACKAGES ) {
563+ const prefix = pkgSrc ( pkg ) + '/' ;
564+ const changedModules = new Set (
565+ changedFiles
566+ . filter ( ( file ) => file . startsWith ( prefix ) )
567+ . map ( ( file ) => withoutExtension ( file . slice ( prefix . length ) ) ) ,
568+ ) ;
569+ if ( changedModules . size === 0 ) continue ;
570+ const changedTops = new Set ( [ ...changedModules ] . map ( ( file ) => file . split ( '/' ) [ 0 ] ) ) ;
571+ const dist = path . join ( process . cwd ( ) , pkgDist ( pkg ) ) ;
572+ // esbuild reports inputs by real path.
573+ const distReal = fs . existsSync ( dist ) ? fs . realpathSync ( dist ) : dist ;
574+ const all = packageTargets ( pkg ) ;
575+ const reach = bundleInputsByEntry ( all . map ( ( target ) => path . join ( dist , target . distModule ) ) ) ;
576+ for ( const target of all ) {
577+ const top = withoutExtension ( target . distModule . split ( '/' ) [ 0 ] ) ;
578+ const inputs = reach ?. get ( path . join ( dist , target . distModule ) ) ?? [ ] ;
579+ const reachesChange = inputs . some ( ( input ) => {
580+ const relative = path . relative ( distReal , input ) ;
581+ return ! relative . startsWith ( '..' ) && changedModules . has ( withoutExtension ( relative ) ) ;
582+ } ) ;
583+ if ( changedTops . has ( top ) || reachesChange ) targets . push ( target ) ;
584+ }
585+ }
586+ return targets ;
587+ }
588+
589+ // Targets from either tree, once each by key.
590+ function unionTargets ( first , second ) {
591+ const byKey = new Map ( first . map ( ( target ) => [ target . key , target ] ) ) ;
592+ for ( const target of second ) if ( ! byKey . has ( target . key ) ) byKey . set ( target . key , target ) ;
593+ return [ ...byKey . values ( ) ] ;
594+ }
595+
596+ // What each target's import costs in the current tree: the gzip JavaScript it
597+ // loads eagerly, and the gzip CSS of the package stylesheet rules referenced by
598+ // its own package's modules it can load, import() targets included (spec:AST-080 FR8,
599+ // architecture:component-style-authoring INV6).
600+ function measureSizes ( targets ) {
601+ const sizes = { } ;
602+ for ( const target of targets ) {
603+ const dist = path . join ( process . cwd ( ) , pkgDist ( target . pkg ) ) ;
604+ let size = measureComponentImport (
605+ path . join ( dist , target . distModule ) ,
606+ packageStylesheet ( target . pkg ) ,
607+ dist ,
608+ target . name ,
609+ ) ;
610+ // An alias (`export {A as B}`) is not exported by its defining module;
611+ // measure it through the package entry instead.
612+ if ( size . importGzipBytes === null ) {
613+ const { esmPath} = resolveEntries ( dist , target . pkg ) ;
614+ size = measureComponentImport (
615+ esmPath ,
616+ packageStylesheet ( target . pkg ) ,
617+ dist ,
618+ target . name ,
619+ ) ;
620+ }
621+ sizes [ target . key ] = { package : target . pkg . name , ...size } ;
622+ }
623+ return sizes ;
624+ }
625+
503626// Get component stats
504627function getComponentStats ( pkg , componentName ) {
505628 // Per-component dist sizing only applies to nested packages that emit a
@@ -512,7 +635,6 @@ function getComponentStats(pkg, componentName) {
512635 const entries = distDir ? resolveEntries ( distDir , pkg ) : { esmPath : null , cjsPath : null } ;
513636 const loc = getComponentLOC ( pkg , componentName ) ;
514637 const complexity = getComplexity ( pkg , componentName ) ;
515-
516638 return {
517639 package : pkg . name ,
518640 esmSize : getFileSize ( entries . esmPath ) ,
@@ -545,6 +667,10 @@ function getPackageBundleStats(pkg) {
545667 cjsBytes : getFileSizeBytes ( cjsPath ) ,
546668 gzipSize : formatBytes ( gzipBytes ) ,
547669 gzipBytes,
670+ stylesheetGzipBytes : ( ( ) => {
671+ const css = packageStylesheet ( pkg ) ;
672+ return css === null ? null : gzipTextBytes ( css ) ;
673+ } ) ( ) ,
548674 } ;
549675}
550676
@@ -655,6 +781,12 @@ function analyze() {
655781 // and package lists stay the head's; only the stats are read in the base tree.
656782 let baseComponentStats = null ;
657783 let baseBundlePackages = null ;
784+ // Components a change can affect, by reach in the head build and, when the
785+ // merge base is built, in the base build too: CI's size-base job builds only
786+ // the base, and a module change can add or remove reach.
787+ const headTargets = sizeTargets ( changedFiles ) ;
788+ let targets = headTargets ;
789+ let baseComponentSizes = null ;
658790 if ( baseRoot ) {
659791 const headDir = process . cwd ( ) ;
660792 process . chdir ( path . resolve ( headDir , baseRoot ) ) ;
@@ -667,17 +799,24 @@ function analyze() {
667799 baseBundlePackages = PACKAGES . filter ( ( p ) => changedPackages . has ( p . name ) ) . map ( ( p ) =>
668800 getPackageBundleStats ( p ) ,
669801 ) ;
802+ // Each tree is measured through its own defining modules, so a renamed
803+ // module is not mistaken for a missing one.
804+ const baseTargets = sizeTargets ( changedFiles ) ;
805+ baseComponentSizes = measureSizes ( unionTargets ( baseTargets , headTargets ) ) ;
806+ targets = unionTargets ( headTargets , baseTargets ) ;
670807 } finally {
671808 process . chdir ( headDir ) ;
672809 }
673810 }
811+ const componentSizes = baseOnly ? null : measureSizes ( targets ) ;
674812
675813 if ( baseOnly ) {
676814 const baseResult = {
677815 headSha : execSync ( 'git rev-parse HEAD' , { encoding : 'utf8' } ) . trim ( ) ,
678816 baseSha : execSync ( 'git rev-parse HEAD' , { cwd : baseRoot , encoding : 'utf8' } ) . trim ( ) ,
679817 baseComponentStats,
680818 baseBundlePackages,
819+ baseComponentSizes,
681820 analyzedAt : new Date ( ) . toISOString ( ) ,
682821 } ;
683822 fs . writeFileSync ( outputFile , JSON . stringify ( baseResult , null , 2 ) ) ;
@@ -701,7 +840,8 @@ function analyze() {
701840 // or render it in the report should caveat when this is 'two-dot'.
702841 diffMode,
703842 bundlePackages,
704- ...( baseRoot ? { baseComponentStats, baseBundlePackages } : { } ) ,
843+ componentSizes,
844+ ...( baseRoot ? { baseComponentStats, baseBundlePackages, baseComponentSizes } : { } ) ,
705845 // Back-compat: keep totalBundle pointed at core when core changed, so any
706846 // older consumer of this field still resolves.
707847 totalBundle : bundlePackages . find ( ( b ) => b . package === '@astryxdesign/core' ) || null ,
0 commit comments