-
Notifications
You must be signed in to change notification settings - Fork 48
/
vite.config.ts
113 lines (101 loc) · 2.8 KB
/
vite.config.ts
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
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import react from '@vitejs/plugin-react'
import { isArray, mergeWith } from 'lodash'
import rollupNodePolyFill from 'rollup-plugin-node-polyfills'
import istanbul from 'vite-plugin-istanbul'
import builtins from 'rollup-plugin-node-builtins'
import { defineConfig, loadEnv, UserConfigExport } from 'vite'
import eslint from 'vite-plugin-eslint'
import svgrPlugin from 'vite-plugin-svgr'
import viteTsconfigPaths from 'vite-tsconfig-paths'
const commonConfigOptions = ({ mode }: { mode: Mode }): UserConfigExport => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
const config: UserConfigExport = {
resolve: {
alias: {
stream: 'stream-browserify',
crypto: 'crypto-browserify',
},
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
plugins: [react(), viteTsconfigPaths(), svgrPlugin(), eslint(), builtins({ crypto: false })],
define: {
APP_VERSION: JSON.stringify(process.env.npm_package_version),
},
optimizeDeps: {
include: ['base64-arraybuffer', 'diffie-hellman'],
esbuildOptions: {
define: {
global: 'globalThis',
},
plugins: [
NodeGlobalsPolyfillPlugin({
buffer: true,
process: true,
}),
],
},
},
}
return config
}
const devConfigOptions: UserConfigExport = {
...mergeWith(
commonConfigOptions({ mode: 'development' }),
{
server: {
open: true,
},
plugins: [
...(process.env.VITE_APP_IS_E2E
? [
istanbul({
include: 'src/*',
exclude: ['node_modules', 'test/'],
extension: ['.js', '.ts', '.jsx', '.tsx'],
requireEnv: false,
cypress: true,
}),
]
: []),
],
},
customizer,
),
}
const prodConfigOptions: UserConfigExport = {
...mergeWith(
commonConfigOptions({ mode: 'production' }),
{
resolve: {},
build: {
outDir: 'build',
},
rollupOptions: {
plugins: [
// Enable rollup polyfills plugin
// used during production bundling
rollupNodePolyFill(),
],
},
},
customizer,
),
}
type Mode = 'development' | 'production'
const configDispatch: Record<Mode, UserConfigExport> = {
development: devConfigOptions,
production: prodConfigOptions,
}
// https://vitejs.dev/config/
export default ({ mode }: { mode: Mode }) => {
const config = configDispatch[mode]
return defineConfig(config)
}
// eslint-disable-next-line consistent-return
function customizer(objValue, srcValue) {
if (isArray(objValue)) {
return objValue.concat(srcValue)
}
}