|
| 1 | +// The published JavaScript tree-shakes: an app that imports one component |
| 2 | +// gets that component, not the whole package (scripts/modules.mjs). |
| 3 | +import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join, relative } from 'node:path'; |
| 6 | +import { build } from 'esbuild'; |
| 7 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 8 | +import { buildModules } from '../scripts/modules.mjs'; |
| 9 | + |
| 10 | +const pkg = join(__dirname, '..'); |
| 11 | +let app = ''; |
| 12 | + |
| 13 | +/** Package files an app's bundle keeps when the app imports `names`. */ |
| 14 | +async function bundledFiles(names: string[]) { |
| 15 | + const entry = join(app, `entry-${names.join('-')}.js`); |
| 16 | + writeFileSync(entry, `import { ${names.join(', ')} } from '@usememos/ui';\nconsole.log(${names.join(', ')});\n`); |
| 17 | + const result = await build({ |
| 18 | + entryPoints: [entry], |
| 19 | + bundle: true, |
| 20 | + write: false, |
| 21 | + metafile: true, |
| 22 | + format: 'esm', |
| 23 | + external: ['react', 'react/*', 'react-dom', 'lucide-react', '@base-ui/react', '@base-ui/react/*'], |
| 24 | + logLevel: 'silent', |
| 25 | + }); |
| 26 | + const output = Object.values(result.metafile.outputs)[0]; |
| 27 | + const ui = join(app, 'node_modules/@usememos/ui/dist'); |
| 28 | + const kept = Object.entries(output?.inputs ?? {}).filter(([, input]) => input.bytesInOutput > 0); |
| 29 | + return kept.map(([path]) => relative(ui, join(process.cwd(), path))).filter((path) => !path.startsWith('..')); |
| 30 | +} |
| 31 | + |
| 32 | +describe('published modules', () => { |
| 33 | + beforeAll(async () => { |
| 34 | + // A stand-in app with the package installed: the built modules and the real package.json, whose sideEffects field matters. |
| 35 | + app = mkdtempSync(join(tmpdir(), 'mu-modules-')); |
| 36 | + const installed = join(app, 'node_modules/@usememos/ui'); |
| 37 | + mkdirSync(installed, { recursive: true }); |
| 38 | + await buildModules(join(pkg, 'src'), join(installed, 'dist')); |
| 39 | + cpSync(join(pkg, 'package.json'), join(installed, 'package.json')); |
| 40 | + }); |
| 41 | + |
| 42 | + afterAll(() => rmSync(app, { recursive: true, force: true })); |
| 43 | + |
| 44 | + it('mark every module as client code for React Server Components', () => { |
| 45 | + const index = readFileSync(join(app, 'node_modules/@usememos/ui/dist/index.js'), 'utf8'); |
| 46 | + expect(index.startsWith("'use client';")).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it('keep only Button when an app imports Button', async () => { |
| 50 | + const files = await bundledFiles(['Button']); |
| 51 | + expect(files).toContain('components/button/index.js'); |
| 52 | + expect(files.filter((path) => path.startsWith('components/') && !path.startsWith('components/button/'))).toEqual([]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('keep only the components an app uses through compound parts', async () => { |
| 56 | + const files = await bundledFiles(['Menu', 'Tooltip']); |
| 57 | + const components = [...new Set(files.filter((path) => path.startsWith('components/')).map((path) => path.split('/')[1]))].sort(); |
| 58 | + expect(components).toEqual(['menu', 'tooltip']); |
| 59 | + }); |
| 60 | +}); |
0 commit comments