codeflash-internal/js/cf-webapp/scripts/postinstall-wasm.mjs
Kevin Turcios d7a8b8f227
perf: fix CI build + lazy-load heavy libs + parallelize DB queries (#2601)
## Summary
- **Fix CI build failure**: Auth0Client crashes during Next.js
prerendering when env vars aren't set. Returns a no-op stub (`getSession
→ null`) when domain is missing — semantically correct for static
generation
- **Lazy-load markdown libs (~260kb)**: ReactMarkdown, remarkGfm, and
react-syntax-highlighter were eagerly imported in monaco-diff-viewer but
only rendered when user expands "Generated Tests". Extracted into a
dynamic component
- **Parallelize repo detail query**: `getRepositoryById` ran the
activity count sequentially after the repo lookup. Since `repoId` is
already available, all three queries now run in parallel

## Test plan
- [ ] CI `build` check passes (was failing since #2598)
- [ ] Trace page still renders generated tests correctly when expanded
- [ ] Repository detail page loads correctly with activity status
2026-04-13 11:03:05 -05:00

76 lines
2.6 KiB
JavaScript

#!/usr/bin/env node
/**
* Postinstall script that caches tree-sitter WASM artifacts in public/.
* Prisma client generation is handled by pnpm workspaces — no symlinks needed.
*
* Uses Node module resolution to find packages regardless of where pnpm
* stores them (isolated node_modules with symlinks to the store).
*/
import { existsSync, readFileSync, writeFileSync, copyFileSync } from "fs"
import { createRequire } from "module"
import { execSync } from "child_process"
import { dirname, resolve } from "path"
const require = createRequire(import.meta.url)
// Resolve package directory. Some packages (e.g. web-tree-sitter) don't
// export ./package.json, so fall back to resolving the main entry.
function pkgDir(name) {
try {
return dirname(require.resolve(`${name}/package.json`))
} catch {
return dirname(require.resolve(name))
}
}
// --- Tree-sitter WASM ---
const PUBLIC = resolve("public")
const WASM_FILE = resolve(PUBLIC, "tree-sitter-python.wasm")
const WEB_WASM = resolve(PUBLIC, "web-tree-sitter.wasm")
const VERSION_STAMP = resolve(PUBLIC, ".tree-sitter-python-version")
// Always copy web-tree-sitter.wasm (fast — just a file copy)
try {
const webTreeSitterSrc = resolve(pkgDir("web-tree-sitter"), "web-tree-sitter.wasm")
copyFileSync(webTreeSitterSrc, WEB_WASM)
console.log("[postinstall] Copied web-tree-sitter.wasm")
} catch {
console.warn("[postinstall] web-tree-sitter.wasm not found — skipping copy")
}
// Read the installed tree-sitter-python version
let installedVersion = "unknown"
let treeSitterPythonDir
try {
treeSitterPythonDir = pkgDir("tree-sitter-python")
const pkg = JSON.parse(readFileSync(resolve(treeSitterPythonDir, "package.json"), "utf8"))
installedVersion = pkg.version
} catch {
// Package not installed — will force build
}
// Check if we can skip the build
let cachedVersion = ""
try {
cachedVersion = readFileSync(VERSION_STAMP, "utf8").trim()
} catch {
// No stamp — first install
}
if (existsSync(WASM_FILE) && cachedVersion === installedVersion) {
console.log(`[postinstall] tree-sitter-python.wasm is up-to-date (v${installedVersion}) — skipping build`)
process.exit(0)
}
// Build tree-sitter-python WASM
console.log(`[postinstall] Building tree-sitter-python.wasm (v${installedVersion})...`)
try {
execSync(`npx tree-sitter build --wasm ${treeSitterPythonDir} -o ${WASM_FILE}`, {
stdio: "inherit",
})
writeFileSync(VERSION_STAMP, installedVersion)
console.log(`[postinstall] Built and cached tree-sitter-python.wasm (v${installedVersion})`)
} catch (err) {
console.error("[postinstall] Failed to build tree-sitter-python.wasm:", err.message)
process.exit(1)
}