|
| 1 | +// Bootstraps the Vite host the specs drive: packs this package, installs the |
| 2 | +// tarball into a throwaway app in a temp directory (teardown.ts removes it), |
| 3 | +// typechecks fixtures.tsx against the packed types and serves it on :4173. |
| 4 | +import { execFileSync } from "node:child_process"; |
| 5 | +import { copyFileSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | +import { join } from "node:path"; |
| 8 | + |
| 9 | +const root = join(import.meta.dirname, ".."); |
| 10 | +// Outside node_modules, where Vite would skip prebundling the host's own deps. |
| 11 | +export const host = join(tmpdir(), "react-ui-e2e-host"); |
| 12 | + |
| 13 | +const packageJson = { |
| 14 | + name: "react-ui-e2e-host", |
| 15 | + private: true, |
| 16 | + type: "module", |
| 17 | + // Exact pins instead of a lockfile: bun keeps a stale copy of a rewritten local tarball. |
| 18 | + dependencies: { |
| 19 | + "@corbits/react-ui": "file:./react-ui.tgz", |
| 20 | + "@radix-ui/react-dialog": "1.1.23", |
| 21 | + "@radix-ui/react-slot": "1.3.3", |
| 22 | + "lucide-react": "1.48.0", |
| 23 | + react: "19.3.0", |
| 24 | + "react-dom": "19.3.0", |
| 25 | + }, |
| 26 | + devDependencies: { |
| 27 | + "@types/react": "19.3.0", |
| 28 | + "@types/react-dom": "19.3.0", |
| 29 | + typescript: "5.9.3", |
| 30 | + vite: "6.4.3", |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +const tsconfig = { |
| 35 | + compilerOptions: { |
| 36 | + target: "ES2022", |
| 37 | + lib: ["dom", "dom.iterable", "ES2022"], |
| 38 | + jsx: "react-jsx", |
| 39 | + module: "esnext", |
| 40 | + moduleResolution: "bundler", |
| 41 | + strict: true, |
| 42 | + noEmit: true, |
| 43 | + skipLibCheck: true, |
| 44 | + isolatedModules: true, |
| 45 | + }, |
| 46 | + include: ["src"], |
| 47 | +}; |
| 48 | + |
| 49 | +const viteConfig = `export default { |
| 50 | + esbuild: { jsx: "automatic" }, |
| 51 | + server: { port: 4173, strictPort: true }, |
| 52 | +}; |
| 53 | +`; |
| 54 | + |
| 55 | +const indexHtml = `<!doctype html> |
| 56 | +<html lang="en"> |
| 57 | + <head> |
| 58 | + <meta charset="UTF-8" /> |
| 59 | + <title>react-ui e2e host</title> |
| 60 | + </head> |
| 61 | + <body> |
| 62 | + <div id="root"></div> |
| 63 | + <script type="module" src="/src/main.tsx"></script> |
| 64 | + </body> |
| 65 | +</html> |
| 66 | +`; |
| 67 | + |
| 68 | +const mainTsx = `import "@corbits/react-ui/styles.css"; |
| 69 | +
|
| 70 | +import { StrictMode } from "react"; |
| 71 | +import { createRoot } from "react-dom/client"; |
| 72 | +
|
| 73 | +import { scenarios } from "./fixtures.js"; |
| 74 | +
|
| 75 | +const name = window.location.pathname.slice(1); |
| 76 | +const Scenario = scenarios[name]; |
| 77 | +
|
| 78 | +createRoot(document.getElementById("root")!).render( |
| 79 | + <StrictMode>{Scenario ? <Scenario /> : <p>Unknown scenario: {name}</p>}</StrictMode>, |
| 80 | +); |
| 81 | +`; |
| 82 | + |
| 83 | +function run(cwd: string, command: string, ...args: string[]): void { |
| 84 | + execFileSync(command, args, { cwd, stdio: "inherit" }); |
| 85 | +} |
| 86 | + |
| 87 | +if (import.meta.main) { |
| 88 | + rmSync(host, { recursive: true, force: true }); |
| 89 | + mkdirSync(join(host, "src"), { recursive: true }); |
| 90 | + run( |
| 91 | + root, |
| 92 | + "bun", |
| 93 | + "pm", |
| 94 | + "pack", |
| 95 | + "--quiet", |
| 96 | + "--filename", |
| 97 | + join(host, "react-ui.tgz"), |
| 98 | + ); |
| 99 | + writeFileSync( |
| 100 | + join(host, "package.json"), |
| 101 | + JSON.stringify(packageJson, null, 2), |
| 102 | + ); |
| 103 | + writeFileSync(join(host, "tsconfig.json"), JSON.stringify(tsconfig, null, 2)); |
| 104 | + writeFileSync(join(host, "vite.config.mjs"), viteConfig); |
| 105 | + writeFileSync(join(host, "index.html"), indexHtml); |
| 106 | + writeFileSync(join(host, "src", "main.tsx"), mainTsx); |
| 107 | + copyFileSync( |
| 108 | + join(import.meta.dirname, "fixtures.tsx"), |
| 109 | + join(host, "src", "fixtures.tsx"), |
| 110 | + ); |
| 111 | + run(host, "bun", "install"); |
| 112 | + run(host, "bun", "x", "tsc", "--noEmit"); |
| 113 | + run(host, "bun", "x", "vite"); |
| 114 | +} |
0 commit comments