Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
120 lines (116 loc) · 5.19 KB
/
Copy pathvite.config.js
File metadata and controls
120 lines (116 loc) · 5.19 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { readFileSync } from 'fs'
const { version } = JSON.parse(readFileSync('./package.json', 'utf-8'))
// The IPTV window frames its own player page (public/iptv-player.html) in an
// <iframe> to play HLS streams. Like the G-Dash surface, this page must be
// embeddable by the same-origin app shell — otherwise the default
// `frame-ancestors 'none'` / `X-Frame-Options: DENY` blocks the frame with
// ERR_BLOCKED_BY_RESPONSE and the TV renders blank white. COEP is kept on the
// player (see setSecurityHeaders): the shell is cross-origin isolated (COOP +
// COEP), so even this same-origin iframe must itself send COEP: require-corp or
// it fails to load.
function allowsSameOriginFrame(req) {
const url = req.url || '';
return url.startsWith('/gdash/') || url.startsWith('/iptv-player.html');
}
function setSecurityHeaders(req, res) {
const frameAncestors = allowsSameOriginFrame(req) ? "'self'" : "'none'";
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('X-Frame-Options', allowsSameOriginFrame(req) ? 'SAMEORIGIN' : 'DENY');
res.setHeader('Content-Security-Policy', `frame-ancestors ${frameAncestors}; object-src 'none'; base-uri 'self'`);
}
// https://vitejs.dev/config/
export default defineConfig({
base: './',
define: {
__APP_VERSION__: JSON.stringify(version),
},
test: {
environment: 'node',
setupFiles: ['./test/setup.js'],
include: ['test/**/*.test.js'],
},
server: {
port: 5173,
strictPort: true,
},
build: {
// Electron loads production assets over the file:// protocol.
// Disable modulepreload to avoid file-origin preload fetch quirks.
modulePreload: false,
rollupOptions: {
output: {
manualChunks(id) {
if (!id.includes('node_modules')) return undefined;
if (id.includes('@webcontainer')) return 'webcontainer';
if (id.includes('@monaco-editor') || id.includes('monaco-editor')) return 'monaco';
if (id.includes('@xterm')) return 'terminal';
if (id.includes('pdfjs-dist')) return 'pdf';
if (id.includes('html2canvas')) return 'html-export';
if (id.includes('jspdf')) return 'pdf-export';
if (id.includes('xlsx')) return 'spreadsheet';
if (id.includes('framer-motion')) return 'motion';
if (id.includes('lucide-react')) return 'icons';
if (id.includes('streamdown')) return 'streamdown';
if (id.includes('katex')) return 'math';
if (id.includes('dompurify')) return 'sanitize';
if (id.includes('d3')) return 'charts';
if (id.includes('react-syntax-highlighter')) return 'syntax-ui';
// Keep the markdown/syntax-highlighting graph together with vendor.
// Splitting react-markdown + remark/rehype/micromark/unified/hast/mdast
// away from refractor/prismjs introduces a cross-chunk circular import,
// which Rollup emits as `Cannot access 'X' before initialization` (TDZ)
// under file:// in the packaged build. Letting them share `vendor`
// eliminates the cycle.
//
// React core must ALSO stay in `vendor`. A dedicated `react` chunk swept in
// @react-spring/@react-three (they match 'react'), which depend on three/zustand
// in `vendor`, while `vendor` holds React-dependent helpers like
// use-isomorphic-layout-effect. That circular react<->vendor dependency made
// `vendor` init before React in the packaged build and throw
// `Cannot read properties of undefined (reading 'useLayoutEffect')`, white-screening
// the app. Keeping React in `vendor` keeps the whole React graph in one chunk.
return 'vendor';
},
},
},
},
plugins: [
react(),
// WebContainer API requires these headers for SharedArrayBuffer support
{
name: 'configure-webcontainer-headers',
configureServer(server) {
server.middlewares.use((req, res, next) => {
// COOP/COEP enables SharedArrayBuffer for WebContainer; frame
// protection prevents the dev app from being embedded/clickjacked.
setSecurityHeaders(req, res);
next();
});
},
// Also configure for preview builds
configurePreviewServer(server) {
server.middlewares.use((req, res, next) => {
setSecurityHeaders(req, res);
next();
});
}
},
// Strip `crossorigin` attributes from the built HTML.
// When the Electron app loads `dist/index.html` via the `file://` protocol,
// Chromium treats each file as a unique origin and blocks the `crossorigin`
// module script, leaving the renderer blank after the splash.
{
name: 'strip-crossorigin-for-electron',
apply: 'build',
enforce: 'post',
transformIndexHtml(html) {
// Remove both boolean and valued forms (e.g. crossorigin or crossorigin="anonymous")
// so module scripts are not blocked under file:// in Electron.
return html.replace(/\s+crossorigin(?:=(?:"[^"]*"|'[^']*'|[^\s>]+))?(?=[\s>])/g, '');
},
}
],
})