-
Notifications
You must be signed in to change notification settings - Fork 111
/
vite.config.ts
198 lines (191 loc) · 6.45 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
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
/// <reference types="vitest" />
import path from 'path'
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import react from '@vitejs/plugin-react-swc'
import { visualizer } from 'rollup-plugin-visualizer'
import vike from 'vike/plugin'
import { defineConfig, loadEnv } from 'vite'
import glslify from 'vite-plugin-glslify'
import svgr from 'vite-plugin-svgr'
const SOURCEMAP_URL = 'https://s3.us-west-1.amazonaws.com/sourcemaps.audius.co/'
const fixAcceptHeader404 = () => ({
// Fix issue with vite dev server and `wait-on`
// https://github.com/vitejs/vite/issues/9520
// Can be removed when upgrading to vite5.
name: 'fix-accept-header-404',
configureServer(server) {
server.middlewares.use((req, _res, next) => {
if (req.headers.accept === 'application/json, text/plain, */*') {
req.headers.accept = '*/*'
}
next()
})
}
})
export default defineConfig(async ({ mode }) => {
// Despite loading env here, the result is the same as a filtered process.env
// rather than dynamically loading the correct env file by mode.
// Since the build/start scripts use turbo, and other packages don't use vite,
// --mode isn't an allowed parameter. Instead, each script sets the
// environment manually using env-cmd.
// The only exception is test (vitest), which does not set the env manually,
// and uses the "test" mode to load .env.test which sets VITE_ENVIRONMENT to
// "development". Even in that case, process.env gets explicitly set
// anyway because that's what the application code looks at.
// Despite loading .env.production (which notably doesn't exist anyway),
// loadEnv prioritizes process.env anyway so we're not at risk of overrides.
const env = loadEnv(mode, path.join(process.cwd(), 'env'), 'VITE_')
// Explicitly set VITE_ENVIRONMENT to "development" when in test mode.
// Better than defaulting to using env.VITE_ENVIRONMENT, which would lead to
// accidentally using "production"!
process.env.VITE_ENVIRONMENT =
mode === 'test' ? 'development' : process.env.VITE_ENVIRONMENT
// Dynamically import the app environment so that process.env.VITE_ENVIRONMENT
// is already set before evaluating the switch/case. The app env is used for
// transforming the index.html file.
const { env: APP_ENV } = await import('./src/services/env')
const port = parseInt(env.VITE_PORT ?? '3000')
const analyze = env.VITE_BUNDLE_ANALYZE === 'true'
const ssr = env.VITE_SSR === 'true'
env.VITE_BASENAME = env.VITE_BASENAME ?? ''
return {
envDir: 'env',
base: env.VITE_BASENAME || '/',
build: {
outDir: ssr ? 'build-ssr' : 'build',
sourcemap: true,
commonjsOptions: {
include: [/node_modules/],
transformMixedEsModules: true
},
rollupOptions: {
output: {
sourcemapBaseUrl:
env.VITE_ENV === 'production' ? SOURCEMAP_URL : undefined
}
}
},
define: {
// Using `process.env` to support mobile,
// This can be removed once the common migration is complete
'process.env': env
},
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis'
},
plugins: [
NodeGlobalsPolyfillPlugin({
buffer: true
})
]
}
},
plugins: [
glslify(),
svgr({
include: '**/*.svg',
exclude: [
'src/assets/img/wallet-link.svg',
'src/assets/img/phantom-icon-purple.svg'
]
}),
// Import workerscript as raw string
// Could use ?raw suffix but it breaks on mobile
{
name: 'workerscript',
transform(code, id) {
if (/\.workerscript$/.test(id)) {
const str = JSON.stringify(code)
return {
code: `export default ${str}`
}
}
}
},
{
transformIndexHtml(html) {
// Replace HTML env vars with values from the system env
return html.replace(/%(\S+?)%/g, (text: string, key) => {
if (key in APP_ENV) {
const value = APP_ENV[key as keyof typeof APP_ENV]
if (value !== null) {
return value as string
}
}
console.warn(`Missing environment variable: ${key}`)
return text
})
}
},
react({
jsxImportSource: '@emotion/react'
}),
...(ssr ? [vike()] : []),
...((analyze
? [
visualizer({
template: 'treemap',
open: true,
gzipSize: true,
filename: 'analyse.html'
})
]
: []) as any),
fixAcceptHeader404()
],
resolve: {
alias: {
// Can't use vite-tsconfig-paths because of vike
app: '/src/app',
assets: '/src/assets',
common: '/src/common',
components: '/src/components',
hooks: '/src/hooks',
pages: '/src/pages',
'public-site': '/src/public-site',
services: '/src/services',
store: '/src/store',
workers: '/src/workers',
utils: '/src/utils',
ssr: '/src/ssr',
'~': path.resolve(__dirname, '../../packages/common/src'),
'~harmony': path.resolve(__dirname, '../../packages/harmony/src'),
test: '/src/test',
'@audius/common/src': path.resolve(__dirname, '../common/src'),
'@audius/harmony/src': path.resolve(__dirname, '../harmony/src'),
'@audius/harmony/dist': path.resolve(__dirname, '../harmony/dist'),
'@audius/harmony': path.resolve(__dirname, '../harmony/src'),
path: require.resolve('path-browserify'),
url: require.resolve('url'),
zlib: require.resolve('browserify-zlib'),
crypto: require.resolve('crypto-browserify'),
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
stream: require.resolve('stream-browserify'),
// Resolve to lodash-es to support tree-shaking
lodash: 'lodash-es'
}
},
server: {
host: '0.0.0.0',
port
},
test: {
environment: 'jsdom',
setupFiles: ['./src/test/vitest-setup.ts'],
deps: {
optimizer: {
web: {
include: ['./src/test/vitest-canvas-mock']
}
}
},
exclude: ['e2e', 'node_modules', 'dist'],
threads: false,
minWorkers: 1,
maxWorkers: 1 // Segfaults if multithreaded
}
}
})