Skip to content

Commit d3eebfe

Browse files
build: Vite 单文件构建 + 修复 doubaoime-asr 打包后 WASM 加载失败
- 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 <noreply@anthropic.com>
1 parent 6becb8b commit d3eebfe

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

scripts/post-build.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@ import { execSync } from 'node:child_process'
1414
const outdir = 'dist'
1515

1616
async function postBuild() {
17-
// Step 1: Patch globalThis.Bun destructuring from third-party deps
18-
const files = await readdir(outdir, { recursive: true })
17+
// Step 1: Patch globalThis.Bun destructuring in the single bundled file
18+
const cliPath = join(outdir, 'cli.js')
1919
const BUN_DESTRUCTURE = /var \{([^}]+)\} = globalThis\.Bun;?/g
2020
const BUN_DESTRUCTURE_SAFE =
2121
'var {$1} = typeof globalThis.Bun !== "undefined" ? globalThis.Bun : {};'
2222

2323
let bunPatched = 0
24-
for (const file of files) {
25-
const filePath = join(outdir, file)
26-
if (typeof file !== 'string' || !file.endsWith('.js')) continue
27-
const content = await readFile(filePath, 'utf-8')
24+
{
25+
const content = await readFile(cliPath, 'utf-8')
2826
if (BUN_DESTRUCTURE.test(content)) {
2927
await writeFile(
30-
filePath,
28+
cliPath,
3129
content.replace(BUN_DESTRUCTURE, BUN_DESTRUCTURE_SAFE),
3230
)
3331
bunPatched++

src/services/doubaoSTT.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,15 @@ export async function connectDoubaoStream(
110110
let doubaoAsr: typeof import('doubaoime-asr')
111111
try {
112112
doubaoAsr = await import('doubaoime-asr')
113-
} catch {
114-
logError(new Error('[doubao-asr] Failed to import doubaoime-asr package'))
115-
callbacks.onError(
116-
'doubaoime-asr package is not installed. Install it with: bun add doubaoime-asr',
117-
{ fatal: true },
113+
} catch (err) {
114+
logError(
115+
new Error(
116+
`[doubao-asr] Failed to import doubaoime-asr package: ${String(err)}`,
117+
),
118118
)
119+
callbacks.onError(`doubaoime-asr package import failed: ${String(err)}`, {
120+
fatal: true,
121+
})
119122
return null
120123
}
121124

vite.config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ export default defineConfig({
7070
ssr: {
7171
target: 'node',
7272
noExternal: true,
73+
// Packages with runtime require.resolve() or WASM binaries can't be
74+
// inlined into the bundle — they must be resolved from node_modules
75+
// at runtime. doubaoime-asr uses opus-encdec which does
76+
// require.resolve('opus-encdec/dist/libopus-encoder.wasm.js').
77+
external: ['doubaoime-asr', 'opus-encdec'],
7378
},
7479

7580
build: {
@@ -88,9 +93,9 @@ export default defineConfig({
8893

8994
output: {
9095
format: 'es',
91-
dir: 'dist',
96+
// Single-file build: no code splitting, all dynamic imports inlined
97+
codeSplitting: false,
9298
entryFileNames: 'cli.js',
93-
chunkFileNames: 'chunks/[name]-[hash].js',
9499
},
95100

96101
plugins: [

0 commit comments

Comments
 (0)