|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file touch-support-bundle.test.mjs |
| 5 | + * @input Core's source, bundled with the PR size report's esbuild options |
| 6 | + * (.github/scripts/lib/component-size.js); core's package.json; core's and |
| 7 | + * the CLI's docs; core's and Lab's component source |
| 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 |
| 13 | + * @position Node test over source, so it needs no build |
| 14 | + */ |
| 15 | + |
| 16 | +import fs from 'node:fs'; |
| 17 | +import {createRequire} from 'node:module'; |
| 18 | +import os from 'node:os'; |
| 19 | +import path from 'node:path'; |
| 20 | +import {afterAll, describe, expect, it} from 'vitest'; |
| 21 | + |
| 22 | +const REPO = path.resolve(import.meta.dirname, '..'); |
| 23 | +const CORE_SRC = path.join(REPO, 'packages/core/src'); |
| 24 | +const require = createRequire(path.join(REPO, 'package.json')); |
| 25 | +const esbuild = require('esbuild'); |
| 26 | + |
| 27 | +/** The gesture core the internal entry re-exports. */ |
| 28 | +const PRIMITIVE_INPUT = /^packages\/core\/src\/gestures\//; |
| 29 | + |
| 30 | +const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-touch-module-')); |
| 31 | +afterAll(() => { |
| 32 | + fs.rmSync(scratch, {recursive: true, force: true}); |
| 33 | +}); |
| 34 | + |
| 35 | +/** |
| 36 | + * 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. |
| 39 | + */ |
| 40 | +function outputsOf(entries) { |
| 41 | + const result = esbuild.buildSync({ |
| 42 | + absWorkingDir: REPO, |
| 43 | + entryPoints: entries, |
| 44 | + outdir: path.join(scratch, 'out'), |
| 45 | + bundle: true, |
| 46 | + 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 | + }); |
| 63 | + return Object.values(result.metafile.outputs).map(output => |
| 64 | + Object.fromEntries( |
| 65 | + Object.entries(output.inputs).map(([input, {bytesInOutput}]) => [ |
| 66 | + input, |
| 67 | + bytesInOutput, |
| 68 | + ]), |
| 69 | + ), |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +/** The inputs matching `pattern` that put bytes in `bytes`. */ |
| 74 | +function bytesOf(bytes, pattern = PRIMITIVE_INPUT) { |
| 75 | + return Object.entries(bytes).filter( |
| 76 | + ([input, size]) => pattern.test(input) && size > 0, |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/** A scratch entry module with `source`. */ |
| 81 | +function entryWith(name, source) { |
| 82 | + const file = path.join(scratch, name); |
| 83 | + fs.writeFileSync(file, source); |
| 84 | + return file; |
| 85 | +} |
| 86 | + |
| 87 | +/** Every file under `dir` whose name matches `pattern`. */ |
| 88 | +function filesUnder(dir, pattern) { |
| 89 | + const found = []; |
| 90 | + for (const entry of fs.readdirSync(dir, {withFileTypes: true})) { |
| 91 | + const full = path.join(dir, entry.name); |
| 92 | + if (entry.isDirectory()) { |
| 93 | + if (entry.name !== 'node_modules' && entry.name !== 'dist') { |
| 94 | + found.push(...filesUnder(full, pattern)); |
| 95 | + } |
| 96 | + } else if (pattern.test(entry.name)) { |
| 97 | + found.push(full); |
| 98 | + } |
| 99 | + } |
| 100 | + return found; |
| 101 | +} |
| 102 | + |
| 103 | +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)])) { |
| 110 | + expect(bytesOf(bytes)).toEqual([]); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + it('is one copy that two components importing it share', () => { |
| 115 | + const component = name => |
| 116 | + entryWith( |
| 117 | + `${name}.ts`, |
| 118 | + `import {GestureArena} from ${JSON.stringify( |
| 119 | + path.join(CORE_SRC, 'internal/touch.ts'), |
| 120 | + )}; |
| 121 | +export const ${name} = () => new GestureArena(); |
| 122 | +`, |
| 123 | + ); |
| 124 | + const outputs = outputsOf([component('First'), component('Second')]); |
| 125 | + const holding = outputs.filter( |
| 126 | + bytes => bytes['packages/core/src/gestures/GestureArena.ts'] > 0, |
| 127 | + ); |
| 128 | + expect(holding).toHaveLength(1); |
| 129 | + for (const bytes of outputs.filter(output => output !== holding[0])) { |
| 130 | + expect(bytesOf(bytes)).toEqual([]); |
| 131 | + } |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('the touch module internals', () => { |
| 136 | + it('have no entry in the exports map beside the internal touch entry', () => { |
| 137 | + const {exports} = JSON.parse( |
| 138 | + fs.readFileSync(path.join(REPO, 'packages/core/package.json'), 'utf8'), |
| 139 | + ); |
| 140 | + expect(exports['./internal/touch']?.source).toBe('./src/internal/touch.ts'); |
| 141 | + const other = Object.keys(exports).filter(key => |
| 142 | + /^\.\/(gestures|motion|touch-support)(\/|$)/.test(key), |
| 143 | + ); |
| 144 | + expect(other).toEqual([]); |
| 145 | + }); |
| 146 | + |
| 147 | + it('are named by no consumer doc', () => { |
| 148 | + const docs = [ |
| 149 | + ...filesUnder(CORE_SRC, /\.doc(\.[a-z]+)?\.mjs$/), |
| 150 | + ...filesUnder(path.join(REPO, 'packages/cli/assets/docs'), /\.mjs$/), |
| 151 | + ]; |
| 152 | + const naming = docs.filter(file => |
| 153 | + /\b(GestureArena|GestureRecognizer)\b|@astryxdesign\/core\/internal\//.test( |
| 154 | + fs.readFileSync(file, 'utf8'), |
| 155 | + ), |
| 156 | + ); |
| 157 | + expect(naming.map(file => path.relative(REPO, file))).toEqual([]); |
| 158 | + }); |
| 159 | + |
| 160 | + it('are never loaded through an import() in Core or Lab source', () => { |
| 161 | + const sources = [ |
| 162 | + ...filesUnder(CORE_SRC, /\.(ts|tsx)$/), |
| 163 | + ...filesUnder(path.join(REPO, 'packages/lab/src'), /\.(ts|tsx)$/), |
| 164 | + ].filter(file => !/\.(test|spec)\.(ts|tsx)$|__tests__/.test(file)); |
| 165 | + const dynamic = sources.filter(file => |
| 166 | + /\bimport\(\s*['"][^'"]*(\/gestures\/|\/motion\/|touch-support|internal\/touch)/.test( |
| 167 | + fs.readFileSync(file, 'utf8'), |
| 168 | + ), |
| 169 | + ); |
| 170 | + expect(dynamic.map(file => path.relative(REPO, file))).toEqual([]); |
| 171 | + }); |
| 172 | +}); |
0 commit comments