perf: Vite 构建启用 code splitting,Bun RSS 从 966MB 降至 35MB

Bun/JSC 全量解析单文件大 JS 的 bytecode 和 JIT,17MB 产物导致
RSS 暴涨至 ~1GB(Node/V8 懒解析仅需 ~220MB)。启用代码分割后
Bun 按需加载 chunk,--version RSS 35MB,完整加载 ~500MB。

改动:
- vite.config.ts: 移除 codeSplitting:false,添加 chunkFileNames
- post-build.ts: 遍历 dist/ + dist/chunks/ 所有文件做 Bun patch
- 新建 distRoot.ts 共享工具函数,统一路径定位逻辑
- ripgrep.ts/computerUse/setup.ts/claudeInChrome/setup.ts/updateCCB.ts:
  用 distRoot 替换内联 import.meta.url 路径推算
This commit is contained in:
claude-code-best
2026-05-21 16:36:27 +08:00
parent 5b5fbb2f47
commit dab04af7c9
9 changed files with 133 additions and 46 deletions

View File

@@ -9,28 +9,52 @@
import { readdir, readFile, writeFile, cp } from 'node:fs/promises'
import { chmodSync } from 'node:fs'
import { join } from 'node:path'
import { execSync } from 'node:child_process'
const outdir = 'dist'
async function postBuild() {
// Step 1: Patch globalThis.Bun destructuring in the single bundled file
const cliPath = join(outdir, 'cli.js')
// Step 1: Patch globalThis.Bun destructuring in ALL output files
const BUN_DESTRUCTURE = /var \{([^}]+)\} = globalThis\.Bun;?/g
const BUN_DESTRUCTURE_SAFE =
'var {$1} = typeof globalThis.Bun !== "undefined" ? globalThis.Bun : {};'
let bunPatched = 0
{
const content = await readFile(cliPath, 'utf-8')
const files = await readdir(outdir)
const jsFiles = files.filter(f => f.endsWith('.js'))
for (const file of jsFiles) {
const filePath = join(outdir, file)
const content = await readFile(filePath, 'utf-8')
BUN_DESTRUCTURE.lastIndex = 0
if (BUN_DESTRUCTURE.test(content)) {
await writeFile(
cliPath,
filePath,
content.replace(BUN_DESTRUCTURE, BUN_DESTRUCTURE_SAFE),
)
bunPatched++
}
}
// Also patch chunk files in dist/chunks/
const chunksDir = join(outdir, 'chunks')
let chunkFiles: string[] = []
try {
chunkFiles = (await readdir(chunksDir)).filter(f => f.endsWith('.js'))
} catch {
// No chunks directory — single-file build fallback
}
for (const file of chunkFiles) {
const filePath = join(chunksDir, file)
const content = await readFile(filePath, 'utf-8')
BUN_DESTRUCTURE.lastIndex = 0
if (BUN_DESTRUCTURE.test(content)) {
await writeFile(
filePath,
content.replace(BUN_DESTRUCTURE, BUN_DESTRUCTURE_SAFE),
)
bunPatched++
}
}
// Step 2: Copy native addon files
@@ -55,7 +79,7 @@ async function postBuild() {
chmodSync(cliNode, 0o755)
console.log(
`Post-build complete: patched ${bunPatched} Bun destructure, generated entry points`,
`Post-build complete: patched ${bunPatched} Bun destructure across ${jsFiles.length + chunkFiles.length} files, generated entry points`,
)
}