-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
53 lines (44 loc) · 1.23 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
import { defineConfig } from "vite";
import { parse } from "dotenv";
import fs from "fs";
import path from "path";
import reactRefresh from "@vitejs/plugin-react-refresh";
import svgr from "vite-plugin-svgr";
const getEnvFile = (envFile: string) => {
const filepath = path.resolve(process.cwd(), envFile);
if (fs.existsSync(filepath)) {
return fs.readFileSync(filepath);
}
};
const getEnv = (mode: string) => {
const modeSpecificEnvFile = getEnvFile(`.env.${mode}`);
const rootEnvFile = getEnvFile(".env");
if (!modeSpecificEnvFile && !rootEnvFile) return {};
const fileToParse = modeSpecificEnvFile || rootEnvFile;
const parsedEnv = parse(fileToParse);
const REACT_APP = /^REACT_APP_/i;
const VITE = /^VITE_/i;
const filteredEnvs = Object.entries(parsedEnv).reduce((acc, [key, value]) => {
if (REACT_APP.test(key) || VITE.test(key)) {
acc[key] = value;
}
return acc;
}, {} as Record<string, string>);
return {
"process.env": {
...filteredEnvs,
NODE_ENV: mode,
},
};
};
// https://vitejs.dev/config/
export default ({ mode }) =>
defineConfig({
build: {
outDir: "build",
},
plugins: [reactRefresh(), svgr()],
define: {
...getEnv(mode),
},
});