-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvite-prod.config.ts
73 lines (69 loc) · 2.32 KB
/
vite-prod.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
import { execSync } from "node:child_process";
import path from "path";
import { defineConfig, UserConfig } from "vite";
import pkgjson from "./package.json";
import tsConfig from "./tsconfig.json";
const mapObj = <V0, V>(obj: Record<string, V0>, kf: (t: string) => string, vf: (t: V0) => V): Record<string, V> =>
Object.fromEntries(Object.entries(obj).map(([k, v]) => [kf(k), vf(v)]));
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
const mswEnabled = process.env.API_MODE === "mock";
let gitDetails = {
__GIT_DIRTY__: undefined as string | undefined,
__GIT_BRANCH__: undefined as string | undefined,
__GIT_COMMIT__: undefined as string | undefined,
};
if (command == "build") {
let gitDirty: boolean | undefined;
let gitBranch: string | undefined;
let gitCommit: string | undefined;
try {
gitDirty = execSync("git status --porcelain").toString().trimEnd().length > 0;
gitBranch = process.env.GITHUB_HEAD_REF ?? execSync("git rev-parse --abbrev-ref HEAD").toString().trimEnd();
gitCommit = execSync("git rev-parse --short HEAD").toString().trimEnd();
} catch {
// ignore errors, most likely building from outside a Git repository
}
gitDetails = {
__GIT_DIRTY__: JSON.stringify(gitDirty),
__GIT_BRANCH__: JSON.stringify(gitBranch),
__GIT_COMMIT__: JSON.stringify(gitCommit),
};
}
return {
build: {
outDir: path.resolve(__dirname, "dist"),
emptyOutDir: true,
sourcemap: true,
minify: false,
rollupOptions: {
input: {
app: "index.html",
},
},
},
css: {
modules: {
// Only dashes in class names will be converted to camelCase,
// the original class name will not to be removed from the locals
localsConvention: "dashes",
},
},
define: {
__API_MSW__: JSON.stringify(mswEnabled),
__APP_VERSION__: JSON.stringify(pkgjson.version),
...gitDetails,
},
optimizeDeps: { exclude: ["msw"] },
resolve: {
alias: mapObj(
tsConfig.compilerOptions.paths,
(k) => k.replace("/*", ""),
(paths) => {
if (!paths[0]) throw new Error("No path found");
return path.resolve(__dirname, paths[0].replace("/*", ""));
},
),
},
} satisfies UserConfig;
});