/ concept-collection / stan-web-ide
Sign in
concept-collection / stan-web-ide
stan-web-ide / vite.config.ts
44 lines · 1.8 KBCodeBlameHistory
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 1import { fileURLToPath } from 'node:url';
2import { defineConfig, mergeConfig } from 'vite';
3import { minwebide } from 'minwebide/vite';
5// Cross-origin isolation makes SharedArrayBuffer available — the compiled
6// Stan modules are built with pthreads (chains run in parallel threads).
7// Dev/preview get it from plain response headers — no service worker
8// involved. Production builds are for GitHub Pages, which can't set headers,
9// so only there the coi-serviceworker is injected.
10const coiHeaders = {
11 'Cross-Origin-Embedder-Policy': 'require-corp',
12 'Cross-Origin-Opener-Policy': 'same-origin',
13};
15const injectCoiServiceWorker = {
16 name: 'inject-coi-serviceworker',
17 apply: 'build' as const,
18 transformIndexHtml() {
19 // relative src so it resolves under the DEPLOY_BASE sub-path
20 return [{ tag: 'script', attrs: { src: 'coi-serviceworker.js' }, injectTo: 'head' as const }];
21 },
22};
24// DEPLOY_BASE is set by CI when building for GitHub Pages
25// (the site is served from /stan-web-ide/, not the domain root).
26//
27// Ports: the stock stan-wasm-server docker image only allows the origins
28// http://127.0.0.1:3000 and http://127.0.0.1:4173 in its CORS config, so
29// dev runs on 3000 (open the 127.0.0.1 URL, not localhost) and preview on
30// Vite's default 4173.
31export default defineConfig(mergeConfig(minwebide(), {
32 base: process.env.DEPLOY_BASE ?? '/',
33 plugins: [injectCoiServiceWorker],
34 resolve: {
35 alias: {
36 // stan-language-server imports node's 'path' (join only)
37 path: fileURLToPath(new URL('./src/stan/pathShim.ts', import.meta.url)),
38 },
39 },
40 // host 127.0.0.1 (not 'localhost', which may bind IPv6-only): the page
41 // origin must be exactly http://127.0.0.1:<port> for the server's CORS
42 server: { host: '127.0.0.1', port: 3000, headers: coiHeaders },
43 preview: { host: '127.0.0.1', port: 4173, headers: coiHeaders },
44}));
moveopenescclose