Skip to content

Commit 827dc55

Browse files
committed
feat: vite plugin to configure proxy automatically
1 parent 2d30e15 commit 827dc55

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/utils/vite-plugin.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const path = require('path')
2+
const fs = require('fs')
3+
4+
module.exports = function proxyOptions({ port = 8080 } = {}) {
5+
const config = getCommonSiteConfig()
6+
const webserver_port = config ? config.webserver_port : 8000
7+
if (!config) {
8+
console.log('No common_site_config.json found, using default port 8000')
9+
}
10+
return {
11+
name: 'frappeui-vite-plugin',
12+
config: () => ({
13+
server: {
14+
port: port,
15+
proxy: {
16+
'^/(app|login|api|assets|files)': {
17+
target: `http://127.0.0.1:${webserver_port}`,
18+
ws: true,
19+
router: function (req) {
20+
const site_name = req.headers.host.split(':')[0]
21+
return `http://${site_name}:${webserver_port}`
22+
},
23+
},
24+
},
25+
},
26+
}),
27+
}
28+
}
29+
30+
function getCommonSiteConfig() {
31+
let currentDir = path.resolve('.')
32+
// traverse up till we find frappe-bench with sites directory
33+
while (currentDir !== '/') {
34+
if (
35+
fs.existsSync(path.join(currentDir, 'sites')) &&
36+
fs.existsSync(path.join(currentDir, 'apps'))
37+
) {
38+
let configPath = path.join(currentDir, 'sites', 'common_site_config.json')
39+
if (fs.existsSync(configPath)) {
40+
return JSON.parse(fs.readFileSync(configPath))
41+
}
42+
return null
43+
}
44+
currentDir = path.resolve(currentDir, '..')
45+
}
46+
return null
47+
}

0 commit comments

Comments
 (0)