-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
97 lines (73 loc) · 2.14 KB
/
vite.config.js
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
import { fileURLToPath, URL } from 'url'
import { defineConfig, loadEnv } from 'vite'
import { resolve } from 'path'
import { readFileSync, readdirSync } from 'fs'
import vue from '@vitejs/plugin-vue'
// Exports Vite config.
export default ({ mode }) => {
// Loads env variables.
const env = loadEnv(mode, process.cwd(), '')
// Gets https mode and base URL.
const isHTTPS = process.argv.includes("--https")
const containerIP = "0.0.0.0"
const containerPort = isHTTPS ? "443" : "8080"
const baseURL = isHTTPS ? `https://${containerIP}:443` : `http://${containerIP}:8080`
// Gets proxy entry for each entry in src/pages.
let proxyEntries = readdirSync(resolve("./src/pages"))
.reduce((entries, filePath) => {
// Gets file name without it's extension.
let fileName = filePath
.split("/")
.pop()
.split(".")
.shift()
// Skips if no file name is returned.
if (fileName === '') return entries
// Adds entry.
entries[`/${fileName}/`] = {
target: baseURL,
changeOrigin: true,
rewrite: (_) => `/src/pages/${fileName}.html`
}
// Returns updated entries.
return entries
}, {})
// Adds socket.io entry so site can stay connected to dev server.
proxyEntries['/socket.io'] = {
target: `ws://${containerIP}:${containerPort}`,
ws: true
}
// Configures http dev server.
let server = {
host: containerIP,
port: containerPort,
https: false,
proxy: proxyEntries
}
// Adds HTTPS key and cert.
// @todo This is not working.
if (isHTTPS) {
server.https = {
key: readFileSync(resolve(`${env.LOCAL_KEY}`)),
cert: readFileSync(resolve(`${env.LOCAL_CERT}`))
}
}
// Returns Vite config.
return defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
success: resolve(__dirname, 'src/pages/success.html')
}
}
},
server: server
})
}