-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathvite-plugin-flynt.js
102 lines (88 loc) · 3.4 KB
/
vite-plugin-flynt.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
98
99
100
101
102
import path from 'path'
import fs from 'fs'
let exitHandlersBound = false
export default function ({ dest, host }) {
const hotFile = path.join(dest, 'hot')
let viteDevServerUrl, resolvedConfig
return {
name: 'flynt',
enforce: 'post',
config: () => {
return {
publicDir: false,
server: {
origin: '__flynt_vite_placeholder__'
}
}
},
configResolved (config) {
resolvedConfig = config
},
transform (code) {
if (resolvedConfig.command === 'serve') {
return code.replace(/__flynt_vite_placeholder__/g, viteDevServerUrl)
}
},
configureServer (server) {
const appUrl = host
server.httpServer?.on('listening', () => {
const address = server.httpServer?.address()
const isAddressInfo = (x) => typeof x === 'object'
if (isAddressInfo(address)) {
viteDevServerUrl = resolveDevServerUrl(address, server.config)
fs.writeFileSync(hotFile, viteDevServerUrl)
setTimeout(() => {
const isHttps = host.indexOf('https://') === 0
const hasCertificates = server.httpServer.key && server.httpServer.cert
if (isHttps && !hasCertificates) {
server.config.logger.info(' ➜ Please define VITE_DEV_SERVER_KEY and VITE_DEV_SERVER_CERT inside a “.env” file in the theme folder to enable ssl support for the vite dev server.')
}
server.config.logger.info(` ➜ APP_URL: ${appUrl.replace(/:(\d+)/, (_, port) => `:${port}`)}`)
}, 100)
}
})
if (!exitHandlersBound) {
const clean = () => {
if (fs.existsSync(hotFile)) {
fs.rmSync(hotFile)
}
}
process.on('exit', clean)
process.on('SIGINT', () => process.exit(130))
process.on('SIGTERM', () => process.exit(143))
process.on('SIGHUP', () => process.exit(129))
exitHandlersBound = true
}
return () => server.middlewares.use((req, res, next) => {
if (req.url === '/index.html') {
res.statusCode = 404
res.end(`please open ${appUrl}`)
}
next()
})
}
}
}
/**
* Resolve the dev server URL from the server address and configuration.
*/
function resolveDevServerUrl (address, config) {
const configHmrProtocol = typeof config.server.hmr === 'object' ? config.server.hmr.protocol : null
const clientProtocol = configHmrProtocol ? (configHmrProtocol === 'wss' ? 'https' : 'http') : null
const serverProtocol = config.server.https ? 'https' : 'http'
const protocol = clientProtocol ?? serverProtocol
const configHmrHost = typeof config.server.hmr === 'object' ? config.server.hmr.host : null
const configHost = typeof config.server.host === 'string' ? config.server.host : null
const serverAddress = isIpv6(address) ? `[${address.address}]` : address.address
const host = configHmrHost ?? configHost ?? serverAddress
const configHmrClientPort = typeof config.server.hmr === 'object' ? config.server.hmr.clientPort : null
const port = configHmrClientPort ?? address.port
return `${protocol}://${host}:${port}`
}
function isIpv6 (address) {
return address.family === 'IPv6' ||
// In node >=18.0 <18.4 this was an integer value. This was changed in a minor version.
// See: https://github.com/laravel/vite-plugin/issues/103
// eslint-disable-next-line
address.family === 6
}