-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
77 lines (73 loc) · 1.97 KB
/
vite.config.ts
File metadata and controls
77 lines (73 loc) · 1.97 KB
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
import { defineConfig, loadEnv } from 'vite';
import type { Plugin } from 'vite';
import react from '@vitejs/plugin-react';
// Keep dev server on 9754 to match Tauri config
// Map a few Node core modules to browser-friendly shims if dependencies attempt to import them.
// Also expose CRA-style env keys used in code (REACT_APP_*).
export default defineConfig(({ mode }) => {
// Load .env files so REACT_APP_* variables are available even in Vite
const env = loadEnv(mode, process.cwd(), '');
const defineProcessEnv: Record<string, string> = {
NODE_ENV: JSON.stringify(process.env.NODE_ENV || mode),
};
const reactAppKeys = [
'REACT_APP_ganacheRpc',
'REACT_APP_ganacheAddress',
'REACT_APP_BASE_URL',
];
for (const key of reactAppKeys) {
if (env[key] !== undefined) {
defineProcessEnv[key] = JSON.stringify(env[key] as string);
}
}
const jsAsJsx: Plugin = {
name: 'js-as-jsx-transform',
enforce: 'pre',
async transform(code, id) {
if (!id.endsWith('.js')) return null;
// only transform source files
if (!(id.includes('/src/') || id.includes('\\src\\'))) return null;
const esbuild = await import('esbuild');
const result = await esbuild.transform(code, {
loader: 'jsx',
jsx: 'automatic',
sourcemap: true,
});
return { code: result.code, map: result.map };
},
};
return {
plugins: [jsAsJsx, react()],
server: {
port: 9754,
strictPort: true,
host: true,
},
preview: {
port: 9754,
strictPort: true,
host: true,
},
resolve: {
alias: {
crypto: 'crypto-browserify',
path: 'path-browserify',
os: 'os-browserify/browser',
},
},
define: {
global: 'globalThis',
'process.env': defineProcessEnv,
},
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
},
},
},
build: {
outDir: 'dist',
},
};
});