-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.mjs
112 lines (98 loc) · 2.59 KB
/
vite.config.mjs
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
import { resolve } from 'path'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import replace from 'rollup-plugin-replace'
import tsconfigPaths from 'vite-tsconfig-paths'
import { createHtmlPlugin } from 'vite-plugin-html'
import { replaceCodePlugin } from 'vite-plugin-replace'
import basicSsl from '@vitejs/plugin-basic-ssl'
import shelljs from 'shelljs'
import dotenv from 'dotenv'
import { dependencies, name, version, homepage } from './package.json'
const NODE_ENV = process.env.NODE_ENV || 'production'
const isProd = NODE_ENV === 'production'
const host = dotenv.config()?.parsed?.HOST
const basePath = dotenv.config()?.parsed?.PUBLIC_URL || homepage
const { stdout: lastCommit } = shelljs.exec('git rev-parse --short HEAD', { silent: true })
console.log('lastCommit', lastCommit)
const forceGeneratingSourceMaps = process.argv.includes('source-maps')
const versionToken = `${version}, ${lastCommit?.trim()}`
const nonChunks = ['react', 'react-dom',
'reselect', 'react-router-dom', 'dayjs',
]
function renderChunks(deps) {
const chunks = {}
Object.keys(deps).forEach((key) => {
if (nonChunks.includes(key)) { return }
chunks[`chunk-${key}`] = [key]
})
return chunks
}
export default defineConfig({
plugins: [
tsconfigPaths(),
react(),
createHtmlPlugin({
minify: false,
inject: {
data: {
stratAppName: name,
stratAppVersion: versionToken,
stratAppEntry: '/src/main.tsx',
basePath: `${basePath}`,
},
},
}),
replaceCodePlugin({
replacements: [
{
from: /CMF_APP_VERSION/g,
to: versionToken,
},
{
from: /CMF_APP/g,
to: name,
},
{
from: /CMF_CONFIG_BASE_PATH/g,
to: process.env.VITE_BASE || `${basePath}`,
},
],
}),
isProd ? null : basicSsl(),
],
server: {
port: 3000,
hot: true,
host,
},
base: process.env.VITE_BASE || `${basePath}`,
esbuild: {
drop: isProd ? ['console', 'debugger'] : [],
},
build: {
minify: 'terser',
assetsInlineLimit: 0,
outDir: 'dist',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
sourcemap: forceGeneratingSourceMaps || !isProd,
rollupOptions: {
output: {
manualChunks: {
'chunk-react': ['react', 'react-dom'],
...renderChunks(dependencies),
},
},
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
}),
],
},
},
})