|
6 | 6 | * (.github/scripts/lib/component-size.js); core's package.json; core's and |
7 | 7 | * the CLI's docs; core's and Lab's component source |
8 | 8 | * @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 |
13 | 17 | * @position Node test over source, so it needs no build |
14 | 18 | */ |
15 | 19 |
|
@@ -37,28 +41,36 @@ afterAll(() => { |
37 | 41 | * in it, built as the size report builds a component import. React and StyleX |
38 | 42 | * stay external: they hold no touch code. |
39 | 43 | */ |
| 44 | +/** |
| 45 | + * The size report's build of a component import (minified ESM for the |
| 46 | + * browser). React and StyleX stay external: they hold no touch code. |
| 47 | + */ |
| 48 | +const BUILD_OPTIONS = { |
| 49 | + absWorkingDir: REPO, |
| 50 | + outdir: path.join(scratch, 'out'), |
| 51 | + bundle: true, |
| 52 | + minify: true, |
| 53 | + format: 'esm', |
| 54 | + platform: 'browser', |
| 55 | + define: {'process.env.NODE_ENV': '"production"'}, |
| 56 | + loader: {'.css': 'empty'}, |
| 57 | + external: [ |
| 58 | + 'react', |
| 59 | + 'react/*', |
| 60 | + 'react-dom', |
| 61 | + 'react-dom/*', |
| 62 | + '@stylexjs/stylex', |
| 63 | + ], |
| 64 | + metafile: true, |
| 65 | + write: false, |
| 66 | + logLevel: 'silent', |
| 67 | +}; |
| 68 | + |
40 | 69 | function outputsOf(entries) { |
41 | 70 | const result = esbuild.buildSync({ |
42 | | - absWorkingDir: REPO, |
| 71 | + ...BUILD_OPTIONS, |
43 | 72 | entryPoints: entries, |
44 | | - outdir: path.join(scratch, 'out'), |
45 | | - bundle: true, |
46 | 73 | 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', |
62 | 74 | }); |
63 | 75 | return Object.values(result.metafile.outputs).map(output => |
64 | 76 | Object.fromEntries( |
@@ -100,13 +112,247 @@ function filesUnder(dir, pattern) { |
100 | 112 | return found; |
101 | 113 | } |
102 | 114 |
|
| 115 | +/** |
| 116 | + * The public imports that may carry the gesture code: those that use a |
| 117 | + * gesture, or reach one through a component that does. Each is named here, so |
| 118 | + * an import that starts carrying it shows up in review. An entry only allows: |
| 119 | + * one that carries nothing yet passes, so a component is listed before its |
| 120 | + * touch work lands. |
| 121 | + */ |
| 122 | +const GESTURE_USERS = new Set([ |
| 123 | + // AppShell's scroll-edge bar. |
| 124 | + 'AppShell', |
| 125 | + // The action-group tracker. |
| 126 | + 'ButtonGroup', |
| 127 | + 'ToggleButton', |
| 128 | + // Carousel's paging. |
| 129 | + 'Carousel', |
| 130 | + // Chat's keyboard dismissal on a transcript drag. |
| 131 | + 'Chat', |
| 132 | + // Item's swipe actions, and the imports that reach Item. |
| 133 | + 'Item', |
| 134 | + 'List', |
| 135 | + 'CheckboxList', |
| 136 | + 'RadioList', |
| 137 | + 'Breadcrumbs', |
| 138 | + // NumberInput's steppers. |
| 139 | + 'NumberInput', |
| 140 | + // SegmentedControl's press and drag to select. |
| 141 | + 'SegmentedControl', |
| 142 | + // Switch's thumb drag. |
| 143 | + 'Switch', |
| 144 | + // Toast's swipe to dismiss. |
| 145 | + 'Toast', |
| 146 | + // The public pan, which the root's createPanGesture shares. |
| 147 | + 'hooks: usePanGesture', |
| 148 | + // The public snap-point drag. |
| 149 | + 'dragToSnapPoints', |
| 150 | + 'hooks: useDragToSnapPoints', |
| 151 | + // Slider's thumb. |
| 152 | + 'Slider', |
| 153 | + // The List reorder, through createReorderable. |
| 154 | + 'reorderable', |
| 155 | + // DateInput's and TimeInput's wheel, and the imports that reach it. |
| 156 | + 'DateInput', |
| 157 | + 'DateTimeInput', |
| 158 | + 'PowerSearch', |
| 159 | + 'Table', |
| 160 | + 'TimeInput', |
| 161 | + // useMenuPress's trigger, which waits on a scroll view under a finger. |
| 162 | + 'hooks: useMenuPress', |
| 163 | + // useScrollLock's page hold, and the layered components that reach it. |
| 164 | + 'hooks: useScrollLock', |
| 165 | + 'Layer', |
| 166 | + 'AlertDialog', |
| 167 | + 'BottomSheet', |
| 168 | + 'CommandPalette', |
| 169 | + 'ContextMenu', |
| 170 | + 'Dialog', |
| 171 | + 'Drawer', |
| 172 | + 'DropdownMenu', |
| 173 | + 'Lightbox', |
| 174 | + 'Markdown', |
| 175 | + 'MoreMenu', |
| 176 | + 'MultiSelector', |
| 177 | + 'Pagination', |
| 178 | + 'Popover', |
| 179 | + 'Selector', |
| 180 | + 'MobileNav', |
| 181 | + 'SideNav', |
| 182 | + 'TopNav', |
| 183 | +]); |
| 184 | + |
| 185 | +/** Public imports that carry no gesture code, whatever the list above says. */ |
| 186 | +const NEVER_GESTURE_USERS = ['Button', 'TextInput']; |
| 187 | + |
| 188 | +/** |
| 189 | + * Each public import of Core, built alone as the size report builds one: |
| 190 | + * every subpath export but the root and the internal entries, and each export |
| 191 | + * of the hooks entry on its own, since that barrel gathers independent hooks. |
| 192 | + * The root re-exports all of them, so it is not one import: under INV13 a |
| 193 | + * component that uses a gesture imports it statically, and the root then |
| 194 | + * carries it by design. |
| 195 | + */ |
| 196 | +function publicImports() { |
| 197 | + const pkg = JSON.parse( |
| 198 | + fs.readFileSync(path.join(REPO, 'packages/core/package.json'), 'utf8'), |
| 199 | + ); |
| 200 | + const imports = []; |
| 201 | + for (const [subpath, target] of Object.entries(pkg.exports)) { |
| 202 | + const source = target?.source; |
| 203 | + if ( |
| 204 | + typeof source !== 'string' || |
| 205 | + !/\.tsx?$/.test(source) || |
| 206 | + subpath === '.' || |
| 207 | + subpath.includes('*') || |
| 208 | + subpath.startsWith('./internal/') |
| 209 | + ) { |
| 210 | + continue; |
| 211 | + } |
| 212 | + const file = path.join(REPO, 'packages/core', source); |
| 213 | + if (subpath === './hooks') { |
| 214 | + for (const name of exportNamesOf(file)) { |
| 215 | + imports.push({ |
| 216 | + name: `hooks: ${name}`, |
| 217 | + source: `export {${name}} from ${JSON.stringify(file)};\n`, |
| 218 | + }); |
| 219 | + } |
| 220 | + } else { |
| 221 | + imports.push({ |
| 222 | + name: subpath.slice(2), |
| 223 | + source: `export * from ${JSON.stringify(file)};\n`, |
| 224 | + }); |
| 225 | + } |
| 226 | + } |
| 227 | + return imports; |
| 228 | +} |
| 229 | + |
| 230 | +/** The names a module exports at runtime, read from its build. */ |
| 231 | +function exportNamesOf(file) { |
| 232 | + const result = esbuild.buildSync({...BUILD_OPTIONS, entryPoints: [file]}); |
| 233 | + const entry = path.relative(REPO, file); |
| 234 | + return Object.values(result.metafile.outputs).find( |
| 235 | + output => output.entryPoint === entry, |
| 236 | + ).exports; |
| 237 | +} |
| 238 | + |
| 239 | +/** |
| 240 | + * The gesture modules each of `imports` ships, with their bytes and the import |
| 241 | + * chain that brings the first of them, building each import on its own. |
| 242 | + */ |
| 243 | +async function gestureCarriers(imports) { |
| 244 | + const probe = path.join(scratch, 'public-import.ts'); |
| 245 | + fs.writeFileSync(probe, ''); |
| 246 | + const context = await esbuild.context({ |
| 247 | + ...BUILD_OPTIONS, |
| 248 | + entryPoints: [probe], |
| 249 | + }); |
| 250 | + const carriers = []; |
| 251 | + try { |
| 252 | + for (const {name, source} of imports) { |
| 253 | + fs.writeFileSync(probe, source); |
| 254 | + const {metafile} = await context.rebuild(); |
| 255 | + const [output] = Object.values(metafile.outputs); |
| 256 | + const gestureModules = bytesOf( |
| 257 | + Object.fromEntries( |
| 258 | + Object.entries(output.inputs).map(([input, {bytesInOutput}]) => [ |
| 259 | + input, |
| 260 | + bytesInOutput, |
| 261 | + ]), |
| 262 | + ), |
| 263 | + ); |
| 264 | + if (gestureModules.length > 0) { |
| 265 | + carriers.push({ |
| 266 | + name, |
| 267 | + gestureModules, |
| 268 | + chain: importChain( |
| 269 | + metafile, |
| 270 | + path.relative(REPO, probe), |
| 271 | + gestureModules[0][0], |
| 272 | + ), |
| 273 | + }); |
| 274 | + } |
| 275 | + } |
| 276 | + } finally { |
| 277 | + await context.dispose(); |
| 278 | + } |
| 279 | + return carriers; |
| 280 | +} |
| 281 | + |
| 282 | +/** The first import chain from `from` to `to` in a build's metafile. */ |
| 283 | +function importChain(metafile, from, to) { |
| 284 | + const previous = new Map([[from, null]]); |
| 285 | + const queue = [from]; |
| 286 | + while (queue.length > 0) { |
| 287 | + const current = queue.shift(); |
| 288 | + if (current === to) { |
| 289 | + break; |
| 290 | + } |
| 291 | + for (const {path: next} of metafile.inputs[current]?.imports ?? []) { |
| 292 | + if (!previous.has(next) && metafile.inputs[next] != null) { |
| 293 | + previous.set(next, current); |
| 294 | + queue.push(next); |
| 295 | + } |
| 296 | + } |
| 297 | + } |
| 298 | + const chain = []; |
| 299 | + for (let at = to; at != null; at = previous.get(at)) { |
| 300 | + chain.unshift(at.replace('packages/core/src/', '')); |
| 301 | + } |
| 302 | + return chain.slice(1).join(' -> '); |
| 303 | +} |
| 304 | + |
103 | 305 | describe('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)])) { |
| 306 | + it('is in no public import that uses no gesture', async () => { |
| 307 | + const carriers = await gestureCarriers(publicImports()); |
| 308 | + const unexpected = carriers.filter(({name}) => !GESTURE_USERS.has(name)); |
| 309 | + expect( |
| 310 | + unexpected.map( |
| 311 | + ({name, gestureModules, chain}) => |
| 312 | + `${name}: ${gestureModules |
| 313 | + .map( |
| 314 | + ([input, size]) => |
| 315 | + `${input.replace('packages/core/src/', '')} ${size} B`, |
| 316 | + ) |
| 317 | + .join(', ')} (via ${chain})`, |
| 318 | + ), |
| 319 | + ).toEqual([]); |
| 320 | + }, 120_000); |
| 321 | + |
| 322 | + it('is in none of the imports that never use a gesture', async () => { |
| 323 | + const strict = publicImports().filter(({name}) => |
| 324 | + NEVER_GESTURE_USERS.includes(name), |
| 325 | + ); |
| 326 | + expect(strict.map(({name}) => name).sort()).toEqual( |
| 327 | + [...NEVER_GESTURE_USERS].sort(), |
| 328 | + ); |
| 329 | + expect( |
| 330 | + (await gestureCarriers(strict)).map( |
| 331 | + ({name, chain}) => `${name} (via ${chain})`, |
| 332 | + ), |
| 333 | + ).toEqual([]); |
| 334 | + }, 120_000); |
| 335 | + |
| 336 | + it('is in the root entry only as much as its touch components import', async () => { |
| 337 | + const used = new Set( |
| 338 | + ( |
| 339 | + await gestureCarriers( |
| 340 | + publicImports().filter(({name}) => GESTURE_USERS.has(name)), |
| 341 | + ) |
| 342 | + ).flatMap(({gestureModules}) => gestureModules.map(([input]) => input)), |
| 343 | + ); |
| 344 | + const inRoot = outputsOf([path.join(CORE_SRC, 'index.ts')]).flatMap(bytes => |
| 345 | + bytesOf(bytes).map(([input]) => input), |
| 346 | + ); |
| 347 | + expect( |
| 348 | + [...new Set(inRoot)] |
| 349 | + .filter(input => !used.has(input)) |
| 350 | + .map(input => input.replace('packages/core/src/', '')), |
| 351 | + ).toEqual([]); |
| 352 | + }, 120_000); |
| 353 | + |
| 354 | + it('is absent from the utils entry', () => { |
| 355 | + for (const bytes of outputsOf([path.join(CORE_SRC, 'utils/index.ts')])) { |
110 | 356 | expect(bytesOf(bytes)).toEqual([]); |
111 | 357 | } |
112 | 358 | }); |
|
0 commit comments