forked from RunanywhereAI/web-starter-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
80 lines (73 loc) · 3.05 KB
/
vite.config.ts
File metadata and controls
80 lines (73 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { defineConfig, type Plugin } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
const __dir = path.dirname(fileURLToPath(import.meta.url));
/**
* Copies WASM binaries from the @runanywhere npm packages into dist/assets/
* so they're served alongside the bundled JS at runtime.
*
* In dev mode, Vite serves node_modules directly so this only
* matters for production builds.
*/
function copyWasmPlugin(): Plugin {
const llamacppWasm = path.resolve(__dir, 'node_modules/@runanywhere/web-llamacpp/wasm');
const onnxWasm = path.resolve(__dir, 'node_modules/@runanywhere/web-onnx/wasm');
return {
name: 'copy-wasm',
writeBundle(options) {
const outDir = options.dir ?? path.resolve(__dir, 'dist');
const assetsDir = path.join(outDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
// LlamaCpp WASM binaries (LLM/VLM)
const llamacppFiles = [
{ src: 'racommons-llamacpp.wasm', dest: 'racommons-llamacpp.wasm' },
{ src: 'racommons-llamacpp.js', dest: 'racommons-llamacpp.js' },
{ src: 'racommons-llamacpp-webgpu.wasm', dest: 'racommons-llamacpp-webgpu.wasm' },
{ src: 'racommons-llamacpp-webgpu.js', dest: 'racommons-llamacpp-webgpu.js' },
];
for (const { src, dest } of llamacppFiles) {
const srcPath = path.join(llamacppWasm, src);
if (fs.existsSync(srcPath)) {
fs.copyFileSync(srcPath, path.join(assetsDir, dest));
const sizeMB = (fs.statSync(srcPath).size / 1_000_000).toFixed(1);
console.log(` ✓ Copied ${dest} (${sizeMB} MB)`);
} else {
console.warn(` ⚠ Not found: ${srcPath}`);
}
}
// Sherpa-ONNX: copy all files in sherpa/ subdirectory (STT/TTS/VAD)
const sherpaDir = path.join(onnxWasm, 'sherpa');
const sherpaOut = path.join(assetsDir, 'sherpa');
if (fs.existsSync(sherpaDir)) {
fs.mkdirSync(sherpaOut, { recursive: true });
for (const file of fs.readdirSync(sherpaDir)) {
const src = path.join(sherpaDir, file);
fs.copyFileSync(src, path.join(sherpaOut, file));
const sizeMB = (fs.statSync(src).size / 1_000_000).toFixed(1);
console.log(` ✓ Copied sherpa/${file} (${sizeMB} MB)`);
}
}
},
};
}
export default defineConfig({
plugins: [react(), copyWasmPlugin()],
server: {
headers: {
// Cross-Origin Isolation — required for SharedArrayBuffer / multi-threaded WASM.
// Without these headers the SDK falls back to single-threaded mode.
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'credentialless',
},
},
assetsInclude: ['**/*.wasm'],
worker: { format: 'es' },
optimizeDeps: {
// Exclude WASM-bearing packages from pre-bundling so their
// import.meta.url resolves correctly to node_modules paths
// (needed for automatic WASM file discovery at ../../wasm/).
exclude: ['@runanywhere/web-llamacpp', '@runanywhere/web-onnx'],
},
});