-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
73 lines (70 loc) · 1.96 KB
/
Copy pathvite.config.ts
File metadata and controls
73 lines (70 loc) · 1.96 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
import react from '@vitejs/plugin-react';
import { defineConfig, loadEnv } from 'vite';
import type { IncomingMessage } from 'node:http';
import { fileURLToPath } from 'node:url';
import { rewriteCookieForLocalHttp } from './proxy';
const root = fileURLToPath(new URL('.', import.meta.url));
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, root, 'VITE_');
const frappeTarget = env.VITE_FRAPPE_TARGET || 'https://frappe.localhost';
const siteUrlHeader = 'x-frappe-site-url';
return {
root,
plugins: [react()],
resolve: {
// When linked locally or installed via github, force React-facing
// imports from the datastore package to use the app's copies
// so hooks share one React dispatcher.
dedupe: [
'react',
'react-dom',
'@wordpress/data',
'@wordpress/element',
],
},
build: {
outDir: 'dist',
emptyOutDir: true,
},
server: {
host: '127.0.0.1',
port: 5180,
strictPort: true,
proxy: {
'/frappe-api': {
target: frappeTarget,
router: (request: IncomingMessage) => {
const header = request.headers[siteUrlHeader];
const candidate = Array.isArray(header) ? header[0] : header;
if (!candidate) return frappeTarget;
try {
const url = new URL(candidate);
return ['http:', 'https:'].includes(url.protocol)
? url.origin
: frappeTarget;
} catch {
return frappeTarget;
}
},
changeOrigin: true,
secure: false,
cookieDomainRewrite: '',
rewrite: (path) => path.replace(/^\/frappe-api/, ''),
configure: (proxy) => {
proxy.on('proxyReq', (proxyRequest) => {
proxyRequest.removeHeader(siteUrlHeader);
});
proxy.on('proxyRes', (proxyResponse) => {
const cookies = proxyResponse.headers['set-cookie'];
if (cookies) {
proxyResponse.headers['set-cookie'] = cookies.map(
rewriteCookieForLocalHttp
);
}
});
},
},
},
},
};
});