66 * (.github/scripts/lib/component-size.js); core's package.json; core's and
77 * the CLI's docs; core's and Lab's component source
88 * @output Pins architecture:react-component-runtime INV13's bundle and
9- * boundary claims: Core's public entries carry none of the touch and gesture
10- * module until a component imports it; two components that import it share
11- * one copy per page; only the internal `internal/touch` entry reaches it,
12- * and no consumer doc or component `import()` does
9+ * boundary claims: a public import of Core (each subpath, and each hook on
10+ * its own) carries none of the gesture code unless it is listed as using a
11+ * gesture, Button and TextInput never do, and a failure names the import
12+ * chain that brings it; the root entry
13+ * carries only the gesture code its touch components import; two components
14+ * that import the touch module share one copy per page; only the internal
15+ * `internal/touch` entry reaches it, and no consumer doc or component
16+ * `import()` does
1317 * @position Node test over source, so it needs no build
1418 */
1519
@@ -32,33 +36,40 @@ afterAll(() => {
3236 fs . rmSync ( scratch , { recursive : true , force : true } ) ;
3337} ) ;
3438
39+ /**
40+ * The size report's build of a component import (minified ESM for the
41+ * browser). React and StyleX stay external: they hold no touch code.
42+ */
43+ const BUILD_OPTIONS = {
44+ absWorkingDir : REPO ,
45+ outdir : path . join ( scratch , 'out' ) ,
46+ bundle : true ,
47+ minify : true ,
48+ format : 'esm' ,
49+ platform : 'browser' ,
50+ define : { 'process.env.NODE_ENV' : '"production"' } ,
51+ loader : { '.css' : 'empty' } ,
52+ external : [
53+ 'react' ,
54+ 'react/*' ,
55+ 'react-dom' ,
56+ 'react-dom/*' ,
57+ '@stylexjs/stylex' ,
58+ ] ,
59+ metafile : true ,
60+ write : false ,
61+ logLevel : 'silent' ,
62+ } ;
63+
3564/**
3665 * Each output of a build of `entries`, with the bytes each source module puts
37- * in it, built as the size report builds a component import. React and StyleX
38- * stay external: they hold no touch code.
66+ * in it, built as the size report builds a component import.
3967 */
4068function outputsOf ( entries ) {
4169 const result = esbuild . buildSync ( {
42- absWorkingDir : REPO ,
70+ ... BUILD_OPTIONS ,
4371 entryPoints : entries ,
44- outdir : path . join ( scratch , 'out' ) ,
45- bundle : true ,
4672 splitting : true ,
47- minify : true ,
48- format : 'esm' ,
49- platform : 'browser' ,
50- define : { 'process.env.NODE_ENV' : '"production"' } ,
51- loader : { '.css' : 'empty' } ,
52- external : [
53- 'react' ,
54- 'react/*' ,
55- 'react-dom' ,
56- 'react-dom/*' ,
57- '@stylexjs/stylex' ,
58- ] ,
59- metafile : true ,
60- write : false ,
61- logLevel : 'silent' ,
6273 } ) ;
6374 return Object . values ( result . metafile . outputs ) . map ( output =>
6475 Object . fromEntries (
@@ -100,13 +111,186 @@ function filesUnder(dir, pattern) {
100111 return found ;
101112}
102113
114+ /**
115+ * The public imports that may carry the gesture code: those that use a
116+ * gesture, or reach one through a component that does. Each is named here, so
117+ * an import that starts carrying it shows up in review: the pull request that
118+ * gives a component its gesture adds that component's import, and each import
119+ * that reaches it, with a comment naming the gesture.
120+ */
121+ const GESTURE_USERS = new Set ( [ ] ) ;
122+
123+ /** Public imports that carry no gesture code, whatever the list above says. */
124+ const NEVER_GESTURE_USERS = [ 'Button' , 'TextInput' ] ;
125+
126+ /**
127+ * Each public import of Core, built alone as the size report builds one:
128+ * every subpath export but the root and the internal entries, and each export
129+ * of the hooks entry on its own, since that barrel gathers independent hooks.
130+ * The root re-exports all of them, so it is not one import: under INV13 a
131+ * component that uses a gesture imports it statically, and the root then
132+ * carries it by design.
133+ */
134+ function publicImports ( ) {
135+ const pkg = JSON . parse (
136+ fs . readFileSync ( path . join ( REPO , 'packages/core/package.json' ) , 'utf8' ) ,
137+ ) ;
138+ const imports = [ ] ;
139+ for ( const [ subpath , target ] of Object . entries ( pkg . exports ) ) {
140+ const source = target ?. source ;
141+ if (
142+ typeof source !== 'string' ||
143+ ! / \. t s x ? $ / . test ( source ) ||
144+ subpath === '.' ||
145+ subpath . includes ( '*' ) ||
146+ subpath . startsWith ( './internal/' )
147+ ) {
148+ continue ;
149+ }
150+ const file = path . join ( REPO , 'packages/core' , source ) ;
151+ if ( subpath === './hooks' ) {
152+ for ( const name of exportNamesOf ( file ) ) {
153+ imports . push ( {
154+ name : `hooks: ${ name } ` ,
155+ source : `export {${ name } } from ${ JSON . stringify ( file ) } ;\n` ,
156+ } ) ;
157+ }
158+ } else {
159+ imports . push ( {
160+ name : subpath . slice ( 2 ) ,
161+ source : `export * from ${ JSON . stringify ( file ) } ;\n` ,
162+ } ) ;
163+ }
164+ }
165+ return imports ;
166+ }
167+
168+ /** The names a module exports at runtime, read from its build. */
169+ function exportNamesOf ( file ) {
170+ const result = esbuild . buildSync ( { ...BUILD_OPTIONS , entryPoints : [ file ] } ) ;
171+ const entry = path . relative ( REPO , file ) ;
172+ return Object . values ( result . metafile . outputs ) . find (
173+ output => output . entryPoint === entry ,
174+ ) . exports ;
175+ }
176+
177+ /**
178+ * The gesture modules each of `imports` ships, with their bytes and the import
179+ * chain that brings the first of them, building each import on its own.
180+ */
181+ async function gestureCarriers ( imports ) {
182+ const probe = path . join ( scratch , 'public-import.ts' ) ;
183+ fs . writeFileSync ( probe , '' ) ;
184+ const context = await esbuild . context ( {
185+ ...BUILD_OPTIONS ,
186+ entryPoints : [ probe ] ,
187+ } ) ;
188+ const carriers = [ ] ;
189+ try {
190+ for ( const { name, source} of imports ) {
191+ fs . writeFileSync ( probe , source ) ;
192+ const { metafile} = await context . rebuild ( ) ;
193+ const [ output ] = Object . values ( metafile . outputs ) ;
194+ const gestureModules = bytesOf (
195+ Object . fromEntries (
196+ Object . entries ( output . inputs ) . map ( ( [ input , { bytesInOutput} ] ) => [
197+ input ,
198+ bytesInOutput ,
199+ ] ) ,
200+ ) ,
201+ ) ;
202+ if ( gestureModules . length > 0 ) {
203+ carriers . push ( {
204+ name,
205+ gestureModules,
206+ chain : importChain (
207+ metafile ,
208+ path . relative ( REPO , probe ) ,
209+ gestureModules [ 0 ] [ 0 ] ,
210+ ) ,
211+ } ) ;
212+ }
213+ }
214+ } finally {
215+ await context . dispose ( ) ;
216+ }
217+ return carriers ;
218+ }
219+
220+ /** The first import chain from `from` to `to` in a build's metafile. */
221+ function importChain ( metafile , from , to ) {
222+ const previous = new Map ( [ [ from , null ] ] ) ;
223+ const queue = [ from ] ;
224+ while ( queue . length > 0 ) {
225+ const current = queue . shift ( ) ;
226+ if ( current === to ) {
227+ break ;
228+ }
229+ for ( const { path : next } of metafile . inputs [ current ] ?. imports ?? [ ] ) {
230+ if ( ! previous . has ( next ) && metafile . inputs [ next ] != null ) {
231+ previous . set ( next , current ) ;
232+ queue . push ( next ) ;
233+ }
234+ }
235+ }
236+ const chain = [ ] ;
237+ for ( let at = to ; at != null ; at = previous . get ( at ) ) {
238+ chain . unshift ( at . replace ( 'packages/core/src/' , '' ) ) ;
239+ }
240+ return chain . slice ( 1 ) . join ( ' -> ' ) ;
241+ }
242+
103243describe ( 'the touch module in bundles' , ( ) => {
104- it . each ( [
105- [ 'the root entry' , 'index.ts' ] ,
106- [ 'the hooks entry' , 'hooks/index.ts' ] ,
107- [ 'the utils entry' , 'utils/index.ts' ] ,
108- ] ) ( 'is absent from %s' , ( _name , entry ) => {
109- for ( const bytes of outputsOf ( [ path . join ( CORE_SRC , entry ) ] ) ) {
244+ it ( 'is in no public import that uses no gesture' , async ( ) => {
245+ const carriers = await gestureCarriers ( publicImports ( ) ) ;
246+ const unexpected = carriers . filter ( ( { name} ) => ! GESTURE_USERS . has ( name ) ) ;
247+ expect (
248+ unexpected . map (
249+ ( { name, gestureModules, chain} ) =>
250+ `${ name } : ${ gestureModules
251+ . map (
252+ ( [ input , size ] ) =>
253+ `${ input . replace ( 'packages/core/src/' , '' ) } ${ size } B` ,
254+ )
255+ . join ( ', ' ) } (via ${ chain } )`,
256+ ) ,
257+ ) . toEqual ( [ ] ) ;
258+ } , 120_000 ) ;
259+
260+ it ( 'is in none of the imports that never use a gesture' , async ( ) => {
261+ const strict = publicImports ( ) . filter ( ( { name} ) =>
262+ NEVER_GESTURE_USERS . includes ( name ) ,
263+ ) ;
264+ expect ( strict . map ( ( { name} ) => name ) . sort ( ) ) . toEqual (
265+ [ ...NEVER_GESTURE_USERS ] . sort ( ) ,
266+ ) ;
267+ expect (
268+ ( await gestureCarriers ( strict ) ) . map (
269+ ( { name, chain} ) => `${ name } (via ${ chain } )` ,
270+ ) ,
271+ ) . toEqual ( [ ] ) ;
272+ } , 120_000 ) ;
273+
274+ it ( 'is in the root entry only as much as its touch components import' , async ( ) => {
275+ const used = new Set (
276+ (
277+ await gestureCarriers (
278+ publicImports ( ) . filter ( ( { name} ) => GESTURE_USERS . has ( name ) ) ,
279+ )
280+ ) . flatMap ( ( { gestureModules} ) => gestureModules . map ( ( [ input ] ) => input ) ) ,
281+ ) ;
282+ const inRoot = outputsOf ( [ path . join ( CORE_SRC , 'index.ts' ) ] ) . flatMap ( bytes =>
283+ bytesOf ( bytes ) . map ( ( [ input ] ) => input ) ,
284+ ) ;
285+ expect (
286+ [ ...new Set ( inRoot ) ]
287+ . filter ( input => ! used . has ( input ) )
288+ . map ( input => input . replace ( 'packages/core/src/' , '' ) ) ,
289+ ) . toEqual ( [ ] ) ;
290+ } , 120_000 ) ;
291+
292+ it ( 'is absent from the utils entry' , ( ) => {
293+ for ( const bytes of outputsOf ( [ path . join ( CORE_SRC , 'utils/index.ts' ) ] ) ) {
110294 expect ( bytesOf ( bytes ) ) . toEqual ( [ ] ) ;
111295 }
112296 } ) ;
0 commit comments