-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.js
106 lines (90 loc) · 3.33 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
98
99
100
101
102
103
104
105
106
import fs from 'fs';
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { sveltePreprocess } from 'svelte-preprocess';
/**
* Simple plugin to create a "hot" file while dev mode is running. tThis file contains
* host:port of the dev server (the port might change if it's already in use)!
*
* Adapted von laravel/blade integration of vite, from the vite-laravel plugin:
* https://github.com/laravel/vite-plugin/blob/7271dcd048a8450afc92d9cb861e2795b2b43ec6/src/index.ts#L209
*
*/
const hotFilePlugin = () => ({
name: 'hotfile-server',
configureServer(server) {
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 @typescript-eslint/ban-ts-comment
// @ts-ignore-next-line
|| address.family === 6;
}
/**
* Resolve the dev server URL from the server address and configuration.
*/
function resolveDevServerUrl(address, config, userConfig) {
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 sailHost = process.env.LARAVEL_SAIL && ! userConfig.server?.host ? 'localhost' : null
const serverAddress = isIpv6(address) ? `[${address.address}]` : address.address
const host = configHmrHost ?? sailHost ?? configHost ?? serverAddress
const configHmrClientPort = typeof config.server.hmr === 'object' ? config.server.hmr.clientPort : null
const port = configHmrClientPort ?? address.port
return `${protocol}://${host}:${port}`
}
let exitHandlersBound = false;
let hotFile = 'app/static/hot';
server.httpServer?.once('listening', () => {
const address = server.httpServer?.address()
const isAddressInfo = (x) => typeof x === 'object'
if (isAddressInfo(address)) {
let viteDevServerUrl = resolveDevServerUrl(address, server.config, {})
fs.writeFileSync(hotFile, `${viteDevServerUrl}${server.config.base.replace(/\/$/, '')}`)
}
})
if (! exitHandlersBound) {
const clean = () => {
if (fs.existsSync(hotFile)) {
fs.rmSync(hotFile)
}
}
process.on('exit', clean)
process.on('SIGINT', () => process.exit())
process.on('SIGTERM', () => process.exit())
process.on('SIGHUP', () => process.exit())
exitHandlersBound = true
}
},
});
export default defineConfig({
//root: 'app/resources',
build: {
// explicitly set path of manifest, the value true would lead to it being
// created at ./.vite/ and Djangos collectstatic ignores dot folders
manifest: "manifest.json",
outDir: 'app/static/build',
rollupOptions: {
input: [
'app/resources/js/app.js',
'app/resources/css/app.css',
]
},
minify: true,
},
plugins: [
hotFilePlugin(),
svelte({
preprocess: sveltePreprocess(),
/* plugin options */
compilerOptions: {
customElement: true,
},
})
],
})