-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvite.config.js
49 lines (45 loc) · 1.86 KB
/
vite.config.js
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
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import envCompatible from "vite-plugin-env-compatible";
import svgrPlugin from "vite-plugin-svgr";
import * as child from "child_process";
// https://vitejs.dev/config/
// https://stackoverflow.com/questions/71162040/how-to-insert-git-info-in-environment-variables-using-vite
// https://stackoverflow.com/questions/70436753/how-to-add-commit-hash-into-reactjs-vite-config-js
export default defineConfig(({ command, mode }) => {
const commitHash = child.execSync("git rev-parse --short HEAD").toString().trimEnd();
const commitDate = child.execSync("git log -1 --format='%ad' --date=short --date=format:'%m/%d/%Y'").toString().trimEnd();
const errString = "fatal: not a git repository (or any of the parent directories): .git"
const errStringShort = "Err: not a git repository";
if (commitHash !== errString && commitDate !== errString) {
process.env.REACT_APP_GIT_SHORT_HASH = commitHash;
process.env.REACT_APP_GIT_COMMIT_DATE = commitDate;
} else {
process.env.REACT_APP_GIT_SHORT_HASH = errStringShort;
process.env.REACT_APP_GIT_COMMIT_DATE = errStringShort;
}
const env = loadEnv(mode, process.cwd(), '')
return {
envPrefix: 'REACT_APP_',
// This changes the out put dir from dist to build
// comment this out if that isn't relevant for your project
build: {
outDir: "build",
},
base: env.REACT_APP_ENDPOINT,
plugins: [
react(),
envCompatible(),
svgrPlugin({
svgrOptions: {
icon: true,
// ...svgr options (https://react-svgr.com/docs/options/)
},
}),
],
server: {
host: "localhost",
port: 3000,
},
};
})