Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
136 lines (129 loc) · 4.25 KB
/
Copy pathvite.config.ts
File metadata and controls
136 lines (129 loc) · 4.25 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { defineConfig, type Plugin } from "vite";
import react, { reactCompilerPreset } from "@vitejs/plugin-react";
import babel from "@rolldown/plugin-babel";
import tailwindcss from "@tailwindcss/vite";
import path from "path";
import {
getDevServerConfig,
getDevServerOrigins,
getDevServerWebSocketOrigins,
} from "./shared/config/devServer";
const devServerConfig = getDevServerConfig();
const devServerOrigins = getDevServerOrigins();
const devServerWebSocketOrigins = getDevServerWebSocketOrigins();
// CSP definitions for development and production
const DEV_CSP = [
`default-src 'self' ${devServerOrigins.join(" ")} ${devServerWebSocketOrigins.join(" ")}`,
`script-src 'self' ${devServerOrigins.join(" ")} 'unsafe-eval'`,
`style-src 'self' ${devServerOrigins.join(" ")} 'unsafe-inline'`,
"font-src 'self' data:",
`connect-src 'self' ${devServerOrigins.join(" ")} ${devServerWebSocketOrigins.join(" ")} canopy-file:`,
`img-src 'self' ${devServerOrigins.join(" ")} https://avatars.githubusercontent.com canopy-file: data:`,
"frame-src 'self' http://localhost:* http://127.0.0.1:* https://localhost:* https://127.0.0.1:*",
].join("; ");
const PROD_CSP = [
"default-src 'self'",
"script-src 'self' 'wasm-unsafe-eval'",
"style-src 'self' 'unsafe-inline'",
"font-src 'self' data:",
"connect-src 'self' canopy-file:",
"img-src 'self' https://avatars.githubusercontent.com canopy-file: data: blob:",
"frame-src 'self' http://localhost:* http://127.0.0.1:* https://localhost:* https://127.0.0.1:*",
"worker-src 'self' blob:",
"object-src 'none'",
"base-uri 'self'",
"form-action 'none'",
].join("; ");
// Plugin to transform CSP meta tag based on build mode
function cspTransformPlugin(): Plugin {
return {
name: "csp-transform",
transformIndexHtml(html, ctx) {
const csp = ctx.server ? DEV_CSP : PROD_CSP;
const cspRegex = /<meta\s+[^>]*http-equiv=["']Content-Security-Policy["'][^>]*>/i;
if (!cspRegex.test(html)) {
throw new Error(
'CSP meta tag not found in index.html. Expected: <meta http-equiv="Content-Security-Policy" ...>'
);
}
return html.replace(
cspRegex,
`<meta http-equiv="Content-Security-Policy" content="${csp}" />`
);
},
};
}
export default defineConfig(({ mode }) => ({
envPrefix: ["VITE_", "CANOPY_"],
plugins: [
react(),
babel({
presets: [reactCompilerPreset({ compilationMode: "infer" })],
}),
tailwindcss(),
cspTransformPlugin(),
],
base: "./",
build: {
target: "chrome144",
modulePreload: { polyfill: false },
outDir: "dist",
emptyOutDir: true,
sourcemap: false,
rolldownOptions: {
...(mode === "production" && {
treeshake: {
manualPureFunctions: ["console.log", "console.info", "console.warn", "console.debug"],
},
}),
output: {
codeSplitting: {
groups: [
{ name: "vendor-xterm", test: /node_modules[\\/]@xterm[\\/]/, priority: 70 },
{
name: "vendor-editor",
test: /node_modules[\\/](@codemirror[\\/]|@uiw[\\/]|refractor[\\/])/,
priority: 60,
},
{
name: "vendor-motion",
test: /node_modules[\\/]framer-motion[\\/]/,
priority: 50,
},
{
name: "vendor-icons",
test: /node_modules[\\/]lucide-react[\\/]/,
priority: 40,
},
{
name: "vendor-ai-github",
test: /node_modules[\\/](@octokit[\\/]|@ai-sdk[\\/]|ai[\\/])/,
priority: 30,
},
{
name: "vendor-zod",
test: /node_modules[\\/](zod[\\/]|zod-to-json-schema[\\/])/,
priority: 20,
},
{ name: "vendor", test: /node_modules[\\/]/, priority: 10 },
],
},
},
},
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
"@shared": path.resolve(__dirname, "./shared"),
},
},
server: {
host: devServerConfig.host,
port: devServerConfig.port,
strictPort: true,
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "credentialless",
},
},
}));