archivo del paquete→ .shtukbase/packages/shtukbase-ui-react/tools/vendor.ts
tools/vendor.ts
57 líneas, 3.8 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // Rebuilds test/vendor: the few npm packages the render tests need, bundled into ES modules and committed, so that
- // `bun test` runs in a clean checkout with no install (the package itself has no npm dependencies; a project that
- // uses the library installs them, see /design/shtukbase-ui-react.md). A maintainer's tool, never run by install.
- // Usage, from the package folder (needs the network): bun tools/vendor.ts
- import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { join } from 'node:path';
- const PACKAGES = ['react@19.3.1', 'react-dom@19.3.1', 'radix-ui@1.8.1', 'class-variance-authority@0.7.1', 'cn@0.5.2', 'lucide-react@1.83.0'];
- const OUT = join(import.meta.dir, '..', 'test', 'vendor');
- // Entry name -> what the bundle exports. The names are the specifiers the library imports (see test/setup.ts).
- const ENTRIES: Record<string, string> = {
- // jsxDEV: Bun compiles TSX to the dev runtime; the production runtime has none, `jsx` takes its first three arguments
- 'react-jsx-runtime': `export { jsx, jsxs, Fragment, jsx as jsxDEV } from 'react/jsx-runtime';`,
- 'react-dom-server': `export { renderToString, renderToStaticMarkup } from 'react-dom/server';`,
- 'radix-ui': `export { Slot, Switch, Select, Slider, Label } from 'radix-ui';`,
- 'class-variance-authority': `export { cva } from 'class-variance-authority';`,
- cn: `export { cn } from 'cn';`,
- // the package index pulls in every icon; the three the primitives use are taken from their own files
- 'lucide-react': `export { default as CheckIcon } from 'lucide-react/dist/esm/icons/check.mjs';\nexport { default as ChevronDownIcon } from 'lucide-react/dist/esm/icons/chevron-down.mjs';\nexport { default as ChevronUpIcon } from 'lucide-react/dist/esm/icons/chevron-up.mjs';`,
- };
- const work = mkdtempSync(join(tmpdir(), 'sb-ui-react-vendor-'));
- const run = (cmd: string[]) => {
- const p = Bun.spawnSync(cmd, { cwd: work, stdout: 'inherit', stderr: 'inherit' });
- if (p.exitCode !== 0) throw new Error(`${cmd.join(' ')} failed`);
- };
- try {
- writeFileSync(join(work, 'package.json'), '{"name":"vendor","private":true}\n');
- run(['bun', 'add', ...PACKAGES]);
- // react: explicit names, a CommonJS module has no named exports to re-export
- const names = Object.keys(require(join(work, 'node_modules', 'react'))).filter((n) => /^[A-Za-z_$][\w$]*$/.test(n) && n !== 'default');
- writeFileSync(join(work, 'react.entry.js'), `import * as R from 'react';\nexport default R;\nexport const { ${names.join(', ')} } = R;\n`);
- const entries = ['react.entry.js'];
- for (const [name, text] of Object.entries(ENTRIES)) {
- writeFileSync(join(work, `${name}.entry.js`), `${text}\n`);
- entries.push(`${name}.entry.js`);
- }
- rmSync(OUT, { recursive: true, force: true });
- mkdirSync(OUT, { recursive: true });
- run(['bun', 'build', ...entries, '--outdir', OUT, '--splitting', '--minify-whitespace', '--target=bun', '--define', 'process.env.NODE_ENV="production"', '--entry-naming', '[name].js', '--chunk-naming', 'chunk-[hash].js']);
- const licenses = PACKAGES.map((p) => {
- const dir = p.replace(/@[^@]+$/, '');
- let text = '';
- try { text = readFileSync(join(work, 'node_modules', dir, 'LICENSE'), 'utf8').split('\n')[0]; } catch { /* some packages keep it elsewhere */ }
- return `- ${p}${text ? ` — ${text.trim()}` : ''}`;
- });
- writeFileSync(
- join(OUT, 'SOURCE.md'),
- `# Vendored test dependencies\n\nBundled by \`tools/vendor.ts\` from npm, all MIT licensed (radix-ui: MIT, WorkOS):\n\n${licenses.join('\n')}\n\nThe files are ES-module bundles (whitespace-minified; identifier mangling breaks react-dom-server in Bun 1.3) the render tests load when the packages are not installed.\n`,
- );
- console.log(`written to ${OUT}`);
- } finally {
- rmSync(work, { recursive: true, force: true });
- }