From d3eebfed156cea0f140766f795e7b218be8539d5 Mon Sep 17 00:00:00 2001 From: claude-code-best Date: Sun, 3 May 2026 16:08:14 +0800 Subject: [PATCH] =?UTF-8?q?build:=20Vite=20=E5=8D=95=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=20+=20=E4=BF=AE=E5=A4=8D=20doubaoime-asr=20?= =?UTF-8?q?=E6=89=93=E5=8C=85=E5=90=8E=20WASM=20=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - vite.config.ts: codeSplitting: false 替代多 chunk 输出,产出单文件 dist/cli.js - vite.config.ts: ssr.external 排除 doubaoime-asr/opus-encdec,避免 require.resolve 路径失效 - scripts/post-build.ts: 简化为直接处理单文件 dist/cli.js - src/services/doubaoSTT.ts: 改进错误信息,输出具体异常内容便于排查 Co-Authored-By: Claude Opus 4.7 --- scripts/post-build.ts | 12 +++++------- src/services/doubaoSTT.ts | 13 ++++++++----- vite.config.ts | 9 +++++++-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/scripts/post-build.ts b/scripts/post-build.ts index c5e655af0..f62e9292f 100644 --- a/scripts/post-build.ts +++ b/scripts/post-build.ts @@ -14,20 +14,18 @@ import { execSync } from 'node:child_process' const outdir = 'dist' async function postBuild() { - // Step 1: Patch globalThis.Bun destructuring from third-party deps - const files = await readdir(outdir, { recursive: true }) + // Step 1: Patch globalThis.Bun destructuring in the single bundled file + const cliPath = join(outdir, 'cli.js') const BUN_DESTRUCTURE = /var \{([^}]+)\} = globalThis\.Bun;?/g const BUN_DESTRUCTURE_SAFE = 'var {$1} = typeof globalThis.Bun !== "undefined" ? globalThis.Bun : {};' let bunPatched = 0 - for (const file of files) { - const filePath = join(outdir, file) - if (typeof file !== 'string' || !file.endsWith('.js')) continue - const content = await readFile(filePath, 'utf-8') + { + const content = await readFile(cliPath, 'utf-8') if (BUN_DESTRUCTURE.test(content)) { await writeFile( - filePath, + cliPath, content.replace(BUN_DESTRUCTURE, BUN_DESTRUCTURE_SAFE), ) bunPatched++ diff --git a/src/services/doubaoSTT.ts b/src/services/doubaoSTT.ts index 86c8e5117..0cd1e5024 100644 --- a/src/services/doubaoSTT.ts +++ b/src/services/doubaoSTT.ts @@ -110,12 +110,15 @@ export async function connectDoubaoStream( let doubaoAsr: typeof import('doubaoime-asr') try { doubaoAsr = await import('doubaoime-asr') - } catch { - logError(new Error('[doubao-asr] Failed to import doubaoime-asr package')) - callbacks.onError( - 'doubaoime-asr package is not installed. Install it with: bun add doubaoime-asr', - { fatal: true }, + } catch (err) { + logError( + new Error( + `[doubao-asr] Failed to import doubaoime-asr package: ${String(err)}`, + ), ) + callbacks.onError(`doubaoime-asr package import failed: ${String(err)}`, { + fatal: true, + }) return null } diff --git a/vite.config.ts b/vite.config.ts index ea09e7b0a..115b2e42a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -70,6 +70,11 @@ export default defineConfig({ ssr: { target: 'node', noExternal: true, + // Packages with runtime require.resolve() or WASM binaries can't be + // inlined into the bundle — they must be resolved from node_modules + // at runtime. doubaoime-asr uses opus-encdec which does + // require.resolve('opus-encdec/dist/libopus-encoder.wasm.js'). + external: ['doubaoime-asr', 'opus-encdec'], }, build: { @@ -88,9 +93,9 @@ export default defineConfig({ output: { format: 'es', - dir: 'dist', + // Single-file build: no code splitting, all dynamic imports inlined + codeSplitting: false, entryFileNames: 'cli.js', - chunkFileNames: 'chunks/[name]-[hash].js', }, plugins: [