-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.demo.ts
More file actions
89 lines (88 loc) · 3.3 KB
/
Copy pathvite.config.demo.ts
File metadata and controls
89 lines (88 loc) · 3.3 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
81
82
83
84
85
86
87
88
89
import { defineConfig, transformWithEsbuild } from 'vite';
import wasm from 'vite-plugin-wasm';
import path from 'path';
import fs from 'fs';
import { nodeboxPreview } from './js/src/preview/middleware.js';
export default defineConfig({
root: 'demo',
server: {
port: 3287,
host: true,
headers: {
// COOP/COEP required for SharedArrayBuffer (Atomics.wait in syscall channel).
// Note: COEP 'credentialless' is used instead of 'require-corp' so that
// cross-origin CDN assets load without needing explicit CORP opt-in.
// COOP 'same-origin' does not block postMessage — it only prevents
// opener/popup window access, so preview iframe postMessage still works.
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'credentialless',
'Cross-Origin-Resource-Policy': 'cross-origin',
},
fs: {
allow: [path.resolve(__dirname)],
},
},
resolve: {
// Use array form so more-specific sub-paths are matched before the bare name.
alias: [
// nodebox-core/wasm: serve the wasm-pack source so Vite can handle the
// .wasm binary via import.meta.url (no pre-built hashed filename needed).
{
find: 'nodebox-core/wasm',
replacement: path.resolve(__dirname, 'packages/nodebox-core/src/wasm/nodebox_runtime.js'),
},
// nodebox-core/host and the main nodebox-core entry: use the pre-built dist
// so that WORKER_ENTRY_URL and KERNEL_WORKER_URL resolve to the compiled
// worker bundles rather than raw source (which can't be served as-is).
{
find: 'nodebox-core/host',
replacement: path.resolve(__dirname, 'packages/nodebox-core/dist/runtime/host.mjs'),
},
{
find: /^nodebox-core$/,
replacement: path.resolve(__dirname, 'packages/nodebox-core/dist/index.mjs'),
},
{
find: /^nodebox$/,
replacement: path.resolve(__dirname, 'js/src/index.ts'),
},
],
},
plugins: [
wasm(),
{
// Serve the service worker TypeScript source at /__nodebox_sw__.js.
// Vite's root is demo/ so the SW (in js/src/) isn't reachable via normal
// static serving — this plugin intercepts the request and compiles it.
name: 'nodebox-sw',
configureServer(server) {
server.middlewares.use(async (req: any, res: any, next: () => void) => {
if (req.url !== '/__nodebox_sw__.js') return next();
try {
const swPath = path.resolve(__dirname, 'js/src/service-worker.ts');
const src = fs.readFileSync(swPath, 'utf8');
// Bundle as IIFE — classic service workers don't support ESM imports.
const result = await transformWithEsbuild(src, swPath, {
format: 'iife',
target: 'es2020',
});
res.setHeader('Content-Type', 'application/javascript');
res.end(result.code);
} catch (e) {
next(e);
}
});
},
},
{
name: 'nodebox-preview',
configureServer(server) {
// Install preview middleware — handles /__preview/{port}/* requests
const middleware = nodeboxPreview();
server.middlewares.use((req: any, res: any, next: () => void) => {
middleware(req, res, next);
});
},
},
],
});