ShtukbaseStandard
package file→ .shtukbase/packages/shtukbase-ui-react/tools/vendor.ts

tools/vendor.ts

57 lines, 3.8 KB.

  1. // Rebuilds test/vendor: the few npm packages the render tests need, bundled into ES modules and committed, so that
  2. // `bun test` runs in a clean checkout with no install (the package itself has no npm dependencies; a project that
  3. // uses the library installs them, see /design/shtukbase-ui-react.md). A maintainer's tool, never run by install.
  4. // Usage, from the package folder (needs the network): bun tools/vendor.ts
  5. import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  6. import { tmpdir } from 'node:os';
  7. import { join } from 'node:path';
  8. 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'];
  9. const OUT = join(import.meta.dir, '..', 'test', 'vendor');
  10. // Entry name -> what the bundle exports. The names are the specifiers the library imports (see test/setup.ts).
  11. const ENTRIES: Record<string, string> = {
  12. // jsxDEV: Bun compiles TSX to the dev runtime; the production runtime has none, `jsx` takes its first three arguments
  13. 'react-jsx-runtime': `export { jsx, jsxs, Fragment, jsx as jsxDEV } from 'react/jsx-runtime';`,
  14. 'react-dom-server': `export { renderToString, renderToStaticMarkup } from 'react-dom/server';`,
  15. 'radix-ui': `export { Slot, Switch, Select, Slider, Label } from 'radix-ui';`,
  16. 'class-variance-authority': `export { cva } from 'class-variance-authority';`,
  17. cn: `export { cn } from 'cn';`,
  18. // the package index pulls in every icon; the three the primitives use are taken from their own files
  19. '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';`,
  20. };
  21. const work = mkdtempSync(join(tmpdir(), 'sb-ui-react-vendor-'));
  22. const run = (cmd: string[]) => {
  23. const p = Bun.spawnSync(cmd, { cwd: work, stdout: 'inherit', stderr: 'inherit' });
  24. if (p.exitCode !== 0) throw new Error(`${cmd.join(' ')} failed`);
  25. };
  26. try {
  27. writeFileSync(join(work, 'package.json'), '{"name":"vendor","private":true}\n');
  28. run(['bun', 'add', ...PACKAGES]);
  29. // react: explicit names, a CommonJS module has no named exports to re-export
  30. const names = Object.keys(require(join(work, 'node_modules', 'react'))).filter((n) => /^[A-Za-z_$][\w$]*$/.test(n) && n !== 'default');
  31. writeFileSync(join(work, 'react.entry.js'), `import * as R from 'react';\nexport default R;\nexport const { ${names.join(', ')} } = R;\n`);
  32. const entries = ['react.entry.js'];
  33. for (const [name, text] of Object.entries(ENTRIES)) {
  34. writeFileSync(join(work, `${name}.entry.js`), `${text}\n`);
  35. entries.push(`${name}.entry.js`);
  36. }
  37. rmSync(OUT, { recursive: true, force: true });
  38. mkdirSync(OUT, { recursive: true });
  39. 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']);
  40. const licenses = PACKAGES.map((p) => {
  41. const dir = p.replace(/@[^@]+$/, '');
  42. let text = '';
  43. try { text = readFileSync(join(work, 'node_modules', dir, 'LICENSE'), 'utf8').split('\n')[0]; } catch { /* some packages keep it elsewhere */ }
  44. return `- ${p}${text ? ` — ${text.trim()}` : ''}`;
  45. });
  46. writeFileSync(
  47. join(OUT, 'SOURCE.md'),
  48. `# 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`,
  49. );
  50. console.log(`written to ${OUT}`);
  51. } finally {
  52. rmSync(work, { recursive: true, force: true });
  53. }