-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
vite.config.js
224 lines (214 loc) · 6.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// @ts-check
/* eslint-disable no-continue */
/* eslint-disable import/no-extraneous-dependencies */
const { defineConfig, loadEnv, createLogger } = require('vite')
const { default: react } = require('@vitejs/plugin-react-swc')
const { default: checker } = require('vite-plugin-checker')
const removeFiles = require('rollup-plugin-delete')
const { resolve } = require('path')
const fs = require('fs')
const { sentryVitePlugin } = require('@sentry/vite-plugin')
const config = require('@rm/config')
const { log, TAGS } = require('@rm/logger')
const { locales, status } = require('@rm/locales')
const {
faviconPlugin,
customFilePlugin,
localePlugin,
muteWarningsPlugin,
} = require('@rm/vite-plugins')
const defaultLogger = createLogger()
const logLevel = config.getSafe('devOptions.logLevel')
const viteLogLevel =
logLevel === 'debug' || logLevel === 'trace' ? 'info' : logLevel
const viteConfig = defineConfig(({ mode }) => {
const env = loadEnv(mode, resolve(process.cwd(), './'), '')
const isRelease = process.argv.includes('-r')
const isDevelopment = mode === 'development'
const serverPort = +(env.PORT || config.getSafe('port') || '8080')
const pkg = JSON.parse(
fs.readFileSync(resolve(__dirname, 'package.json'), 'utf8'),
)
const version = env.npm_package_version || pkg.version
const hasCustom = (function checkFolders(folder, isCustom = false) {
const files = fs.readdirSync(folder)
for (let i = 0; i < files.length; i += 1) {
if (isCustom) return true
if (files[i].startsWith('.')) continue
if (!files[i].includes('.'))
isCustom = checkFolders(`${folder}/${files[i]}`, isCustom)
if (/\.custom.(jsx?|css)$/.test(files[i])) return true
}
return isCustom
})(resolve(__dirname, 'src'))
if (mode === 'production') {
log.info(TAGS.build, `Building production version: ${version}`)
}
if (env.GOOGLE_ANALYTICS_ID) {
log.warn(
TAGS.build,
'The .env file has been deprecated, please move your Google Analytics ID to your config file as this functionality will be removed in the future.',
)
}
const sentry = config.getSafe('sentry.client')
sentry.enabled = sentry.enabled || !!env.SENTRY_DSN
if (env.SENTRY_AUTH_TOKEN) sentry.authToken = env.SENTRY_AUTH_TOKEN
if (env.SENTRY_ORG) sentry.org = env.SENTRY_ORG
if (env.SENTRY_PROJECT) sentry.project = env.SENTRY_PROJECT
if (env.SENTRY_DSN) sentry.dsn = env.SENTRY_DSN
if (env.SENTRY_TRACES_SAMPLE_RATE)
sentry.tracesSampleRate = +env.SENTRY_TRACES_SAMPLE_RATE || 0.1
if (env.SENTRY_DEBUG) sentry.debug = !!env.SENTRY_DEBUG
return {
plugins: [
react(),
...(isDevelopment
? [
checker({
overlay: {
initialIsOpen: false,
},
// typescript: {
// tsconfigPath: './jsconfig.json',
// },
eslint: {
lintCommand: 'eslint "./src/**/*.{js,jsx}"',
},
}),
]
: []),
...(hasCustom ? [customFilePlugin(isDevelopment)] : []),
...(sentry.authToken && sentry.org && sentry.project
? [
sentryVitePlugin({
org: sentry.org,
project: sentry.project,
authToken: sentry.authToken,
}),
]
: []),
localePlugin(isDevelopment),
faviconPlugin(isDevelopment),
muteWarningsPlugin([
['SOURCEMAP_ERROR', "Can't resolve original location of error"],
]),
],
optimizeDeps: isDevelopment ? { exclude: ['@mui/*'] } : undefined,
publicDir: 'public',
resolve: {
alias: {
'@assets': resolve(__dirname, './src/assets'),
'@components': resolve(__dirname, './src/components'),
'@features': resolve(__dirname, './src/features'),
'@hooks': resolve(__dirname, './src/hooks'),
'@services': resolve(__dirname, './src/services'),
'@utils': resolve(__dirname, './src/utils'),
'@store': resolve(__dirname, './src/store'),
},
},
define: {
CONFIG: {
client: {
version,
locales,
localeStatus: status,
hasCustom,
title: config.getSafe('map.general.headerTitle'),
},
sentry: {
client: {
enabled: sentry.enabled,
dsn: sentry.dsn,
tracesSampleRate: sentry.tracesSampleRate,
debug: sentry.debug,
},
},
googleAnalyticsId:
config.getSafe('googleAnalyticsId') || env.GOOGLE_ANALYTICS_ID || '',
map: {
general: {
startLat: config.getSafe('map.general.startLat'),
startLon: config.getSafe('map.general.startLon'),
startZoom: config.getSafe('map.general.startZoom'),
},
theme: config.getSafe('map.theme'),
},
api: {
polling: config.getSafe('api.polling'),
},
},
},
esbuild: {
legalComments: 'none',
},
build: {
target: ['safari11.1', 'chrome64', 'firefox66', 'edge88'],
outDir: resolve(
__dirname,
`./dist${
process.env.NODE_CONFIG_ENV ? `-${process.env.NODE_CONFIG_ENV}` : ''
}`,
),
sourcemap: isRelease || isDevelopment ? true : 'hidden',
minify:
isDevelopment || config.getSafe('devOptions.skipMinified')
? false
: 'esbuild',
input: { main: resolve(__dirname, 'index.html') },
assetsDir: '',
emptyOutDir: true,
chunkSizeWarningLimit: 2000,
rollupOptions: {
plugins: [
// @ts-ignore
removeFiles({
targets: ['dist/favicon'],
hook: 'generateBundle',
}),
],
output: {
manualChunks: (id) => {
if (id.endsWith('.css')) return 'index'
if (id.includes('node_modules')) return 'vendor'
// return id.replace(/.*node_modules\//, '').split('/')[0]
if (id.includes('src')) return version.replaceAll('.', '-')
},
},
},
},
logLevel: viteLogLevel,
customLogger: {
...defaultLogger,
error: (message) => log.error(TAGS.build, message),
warn: (message) => log.warn(TAGS.build, message),
info: (message) => log.info(TAGS.build, message),
// debug: (message) => log.debug(TAGS.build, message),
},
server: {
host: '0.0.0.0',
open: true,
port: serverPort + 1,
fs: {
strict: false,
},
proxy: {
'/api': {
target: `http://0.0.0.0:${serverPort}`,
changeOrigin: true,
secure: false,
},
'/auth': {
target: `http://0.0.0.0:${serverPort}`,
changeOrigin: true,
secure: false,
},
'/graphql': {
target: `http://0.0.0.0:${serverPort}`,
changeOrigin: true,
secure: false,
},
},
},
}
})
module.exports = viteConfig