forked from kiesraad/abacus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-prod.config.ts
More file actions
105 lines (94 loc) · 3.39 KB
/
vite-prod.config.ts
File metadata and controls
105 lines (94 loc) · 3.39 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
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
103
104
105
import { execSync } from "node:child_process";
import path from "node:path";
import browserslist from "browserslist";
import { browserslistToTargets } from "lightningcss";
import { defineConfig, type UserConfig } from "vite";
import pkgjson from "./package.json";
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
const mswEnabled = process.env.API_MODE === "mock";
const includeStorybookLink = process.env.INCLUDE_STORYBOOK_LINK === "true";
// true by default, can be set to false
const showDevPage = process.env.SHOW_DEV_PAGE === undefined || process.env.SHOW_DEV_PAGE === "true";
let gitDetails = {
__GIT_DIRTY__: undefined as string | undefined,
__GIT_BRANCH__: undefined as string | undefined,
__GIT_COMMIT__: undefined as string | undefined,
__GIT_VERSION__: undefined as string | undefined,
};
if (command === "build") {
let gitDirty: boolean | undefined;
let gitBranch: string | undefined;
let gitCommit: string | undefined;
let gitVersion: 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
}
try {
gitVersion = execSync("git describe --tags --exact-match --abbrev=0 --match 'v*' HEAD").toString().trimEnd();
if (gitVersion.startsWith("v")) {
gitVersion = gitVersion.slice(1);
}
} catch {
// ignore errors, most likely no tag for this commit
}
// fallback to dev-<commit> if no tag found
if (gitVersion === undefined && gitCommit !== undefined) {
gitVersion = `dev-${gitCommit.slice(0, 7)}`;
}
// append -dirty suffix if needed to version
if (gitVersion !== undefined && gitDirty === true) {
gitVersion += "-dirty";
}
gitDetails = {
__GIT_DIRTY__: JSON.stringify(gitDirty),
__GIT_BRANCH__: JSON.stringify(gitBranch),
__GIT_COMMIT__: JSON.stringify(gitCommit),
__GIT_VERSION__: JSON.stringify(gitVersion),
};
}
const define = {
__API_MSW__: JSON.stringify(mswEnabled),
__APP_VERSION__: JSON.stringify(pkgjson.version),
__INCLUDE_STORYBOOK_LINK__: JSON.stringify(includeStorybookLink),
__SHOW_DEV_PAGE__: JSON.stringify(showDevPage),
...gitDetails,
};
// print environment variables on CI
if (process.env.CI) {
// biome-ignore-start lint/suspicious/noConsole: only for debugging CI
console.log("Vite build environment:");
Object.entries(define).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
// biome-ignore-end lint/suspicious/noConsole: only for debugging CI
}
return {
build: {
outDir: path.resolve(__dirname, "dist"),
emptyOutDir: true,
sourcemap: true,
minify: false,
rollupOptions: {
input: {
app: "index.html",
},
},
},
css: {
transformer: "lightningcss",
lightningcss: {
targets: browserslistToTargets(browserslist(">= 0.25% and not dead")),
},
},
define,
optimizeDeps: { exclude: ["msw"] },
resolve: {
alias: [{ find: "@", replacement: path.resolve(__dirname, "./src") }],
},
} satisfies UserConfig;
});