-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvite.config.js
186 lines (171 loc) · 6.88 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as path from 'path'
import copy from 'rollup-plugin-copy'
import outputManifest from 'rollup-plugin-output-manifest'
import { defineConfig, loadEnv } from 'vite'
import FullReload from 'vite-plugin-full-reload'
const publicDir = 'public'
const manifestFile = 'manifest.json'
const assets = {
node_modules: 'node_modules/',
base: 'resources',
scripts: 'resources/scripts',
styles: 'resources/styles',
images: 'resources/images',
fonts: 'resources/fonts'
}
const formatName = (name) => name.replace(`${assets.scripts}/`, '').replace(/.css$/gm, '')
export default defineConfig(({ mode }) => {
const devServerConfig = loadEnv(mode, process.cwd(), 'HMR')
const dev = mode === 'development'
const config = {
appType: 'custom',
publicDir: false,
base: './',
resolve: {
alias: {
'@': path.resolve(__dirname, assets.base),
'@scripts': path.resolve(__dirname, assets.scripts),
'@styles': path.resolve(__dirname, assets.styles),
'@fonts': path.resolve(__dirname, assets.fonts),
'@images': path.resolve(__dirname, assets.images),
'~': path.resolve(__dirname, assets.node_modules)
}
},
css: {
devSourcemap: true
},
plugins: [
FullReload(['app/*/**', 'resources/views/**/*']),
copy({
copyOnce: true,
hook: 'writeBundle',
targets: [
{
src: path.resolve(__dirname, `${assets.base}/images/**/*`),
dest: `${publicDir}/images`
},
{
src: path.resolve(__dirname, `${assets.base}/svg/**/*`),
dest: `${publicDir}/svg`
},
{
src: path.resolve(__dirname, `${assets.base}/fonts/**/*`),
dest: `${publicDir}/fonts`
}
]
})
],
build: {
//target: 'es2015',
sourcemap: 'inline',
manifest: false,
outDir: publicDir,
assetsDir: '',
rollupOptions: {
input: {
app: path.resolve(__dirname, `${assets.scripts}/app.js`),
editor: path.resolve(__dirname, `${assets.scripts}/editor.js`),
},
output: {
sourcemap: true
},
plugins: [
outputManifest({
fileName: manifestFile,
generate:
(keyValueDecorator, seed, opt) => chunks =>
chunks.reduce((manifest, { name, fileName }) => {
return name
? {
...manifest,
...keyValueDecorator(formatName(name), fileName, opt)
}
: manifest
}, seed)
}),
outputManifest({
fileName: 'entrypoints.json',
nameWithExt: true,
generate: (_, seed) => chunks =>
chunks.reduce((manifest, { name, fileName }) => {
const formatedName = name && formatName(name)
const output = {}
const js =
formatedName && manifest[formatedName]?.js?.length ? manifest[formatedName].js : []
const css =
formatedName && manifest[formatedName]?.css?.length
? manifest[formatedName].css
: []
const dependencies =
formatedName && manifest[formatedName] ? manifest[formatedName].dependencies : []
const inject = { js, css, dependencies }
fileName.match(/.js$/gm) && js.push(fileName)
fileName.match(/.css$/gm) && css.push(fileName)
name && (output[formatedName] = inject)
return {
...manifest,
...output
}
}, seed)
}),
copy({
copyOnce: true,
hook: 'writeBundle',
targets: [
{
src: path.resolve(__dirname, `${assets.base}/images/**/*`),
dest: `${publicDir}/images`
},
{
src: path.resolve(__dirname, `${assets.base}/svg/**/*`),
dest: `${publicDir}/svg`
},
{
src: path.resolve(__dirname, `${assets.base}/fonts/**/*`),
dest: `${publicDir}/fonts`
}
]
})
]
}
}
}
if (dev) {
let host = 'localhost'
let port = 5173
const protocol = 'http'
const https = !!(devServerConfig.HMR_HTTPS_KEY && devServerConfig.HMR_HTTPS_CERT)
// unlink(`${publicDir}/${manifestFile}`, error =>
// console.log(`🧹 Wipe ${manifestFile} :`, error ? `No ${manifestFile} in the public directory` : '✅')
// )
devServerConfig.HMR_HOST && (host = devServerConfig.HMR_HOST)
devServerConfig.HMR_PORT && (port = parseInt(devServerConfig.HMR_PORT))
https &&
(config.server.https = {
key: devServerConfig.HMR_HTTPS_KEY,
cert: devServerConfig.HMR_HTTPS_CERT
})
config.server = {
host,
port,
https,
strictPort: true,
origin: `${protocol}://${host}:${port}`,
fs: {
strict: true,
allow: ['node_modules', assets.base]
}
/***
* For Windows user with files system watching not working
* https://vitejs.dev/config/server-options.html#server-watch
*/
/*
watch: {
usePolling: true,
interval: 1000
}
*/
}
}
return config
})