Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
45 lines (42 loc) · 2.12 KB
/
Copy pathvite.config.ts
File metadata and controls
45 lines (42 loc) · 2.12 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
import { cpSync, createReadStream, statSync } from "node:fs";
import { extname, resolve, sep } from "node:path";
import { defineConfig, type Plugin } from "vite";
import react, { reactCompilerPreset } from "@vitejs/plugin-react";
import babel from "@rolldown/plugin-babel";
import tailwindcss from "@tailwindcss/vite";
// WebR loads its R runtime (WASM + worker + virtual filesystem) at run time
// from a base URL. Serve those files at /webr/ in dev and copy them into the
// build; the rest of the package (REPL, tests, source maps) is left out.
const WEBR_DIST = resolve("node_modules/webr/dist");
const WEBR_RUNTIME = ["R.js", "R.wasm", "libRblas.so", "libRlapack.so", "webr-worker.js", "vfs"];
const MIME: Record<string, string> = { ".js": "text/javascript", ".wasm": "application/wasm" };
function webrRuntime(): Plugin {
return {
name: "webr-runtime",
// Served as-is: Vite must not transform the worker script.
configureServer(server) {
server.middlewares.use("/webr", (req, res, next) => {
const file = resolve(WEBR_DIST, `.${decodeURIComponent(req.url!.split("?")[0])}`);
if (!file.startsWith(WEBR_DIST + sep) || !statSync(file, { throwIfNoEntry: false })?.isFile()) return next();
res.setHeader("Content-Type", MIME[extname(file)] ?? "application/octet-stream");
createReadStream(file).pipe(res);
});
},
writeBundle({ dir }) {
for (const f of WEBR_RUNTIME) cpSync(resolve(WEBR_DIST, f), resolve(dir!, "webr", f), { recursive: true });
},
};
}
export default defineConfig({
plugins: [react(), babel({ presets: [reactCompilerPreset()] }), tailwindcss(), webrRuntime()],
clearScreen: false,
// The native side is rebuilt by Tauri; its build output must not reload the page.
server: { port: 1420, strictPort: true, watch: { ignored: ["**/src-tauri/**"] } },
build: {
// The oldest WebKit Helix meets: macOS 14's (`minimumSystemVersion` in tauri.conf.json).
target: "safari17",
chunkSizeWarningLimit: 5000,
// The app's pages: project windows, and the hidden window running R (engine.ts).
rolldownOptions: { input: ["index.html", "engine.html"] },
},
});