-
Notifications
You must be signed in to change notification settings - Fork 19
/
vite.config.ts
102 lines (100 loc) · 3.53 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
import { defineConfig, loadEnv } from 'vite'
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
import react from '@vitejs/plugin-react-swc'
import rollupNodePolyFill from 'rollup-plugin-polyfill-node'
import mime from 'mime-types'
import fs from 'fs'
export default defineConfig(({ command, mode }) => {
const envVariables = loadEnv(mode, process.cwd())
console.log({ envVariables })
return {
// depending on your application, base can also be "/"
base: '',
plugins: [react()],
define: {
'process.env': {},
global: 'globalThis'
},
server: {
// this ensures that the browser opens upon server start
open: true,
// this sets a default port to 5173
port: 5173,
proxy: {
'/cdn/packages/website': {
selfHandleResponse: true,
bypass(req, res) {
const urlParts = /\/cdn\/packages\/website\/([^\/]+)\/([^?]*).*$/g.exec(req.url || '')
const filePath = urlParts && urlParts.length > 2 && urlParts[2]
if (!filePath) {
return
}
const mimeType = mime.lookup(filePath)
res.setHeader('x-timestamp', Date.now())
res.setHeader('x-sent', 'true')
res.setHeader('etag', JSON.stringify(Date.now().toString()))
res.setHeader('cache-control', 'no-cache,private,max-age=1')
res.setHeader('Content-Type', mimeType ? mimeType.toString() : 'text/plain')
fs.createReadStream(`./public/${filePath}`).pipe(res)
}
},
'/cdn/packages/explorer': {
selfHandleResponse: true,
bypass(req, res) {
const urlParts = /\/cdn\/packages\/explorer\/([^\/]+)\/([^?]*).*$/g.exec(req.url || '')
const filePath = urlParts && urlParts.length > 2 && urlParts[2]
if (!filePath) {
return
}
const mimeType = mime.lookup(filePath)
res.setHeader('x-timestamp', Date.now())
res.setHeader('x-sent', 'true')
res.setHeader('etag', JSON.stringify(Date.now().toString()))
res.setHeader('cache-control', 'no-cache,private,max-age=1')
res.setHeader('Content-Type', mimeType ? mimeType.toString() : 'text/plain')
fs.createReadStream(`./node_modules/@dcl/explorer/${filePath}`).pipe(res)
}
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'/auth': {
target: 'https://decentraland.zone',
followRedirects: true,
changeOrigin: true,
secure: false,
ws: true
}
}
},
...(command === 'build'
? {
base: envVariables.VITE_PUBLIC_URL,
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: 'globalThis'
},
// Enable esbuild polyfill plugins
plugins: [
NodeGlobalsPolyfillPlugin({
buffer: true,
process: true
}),
NodeModulesPolyfillPlugin()
]
}
},
build: {
commonjsOptions: {
transformMixedEsModules: true
},
rollupOptions: {
plugins: [rollupNodePolyFill()]
},
sourcemap: true
}
}
: undefined)
} as any
})