-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
126 lines (115 loc) · 3.76 KB
/
vite.config.ts
File metadata and controls
126 lines (115 loc) · 3.76 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/// <reference types="vitest" />
import { ProxyOptions, UserConfig, defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import checker from "vite-plugin-checker";
import { execSync } from "child_process";
import { readFileSync } from "fs";
import * as path from "path";
export default ({ mode }): UserConfig => {
const env = { ...process.env, ...loadEnv(mode, process.cwd(), "") };
const proxy = getProxy(env, mode);
const appTitle = resolveAppTitle();
const buildCommit = resolveBuildCommit();
const buildTime = resolveBuildTime(env);
// https://vitejs.dev/config/
return defineConfig({
base: "", // Relative paths
define: {
__APP_BUILD_COMMIT__: JSON.stringify(buildCommit),
__APP_BUILD_TIME__: JSON.stringify(buildTime),
},
plugins: [
injectAppTitlePlugin(appTitle),
react(),
checker({
overlay: false,
typescript: true,
eslint: {
lintCommand: 'eslint "./src/**/*.{ts,tsx}"',
dev: { logLevel: ["warning"] },
},
}),
],
test: {
environment: "jsdom",
include: ["**/*.spec.{ts,tsx}"],
setupFiles: "./src/tests/setup.js",
exclude: ["node_modules", "src/tests/playwright"],
globals: true,
},
server: {
port: parseInt(env.VITE_PORT),
host: "127.0.0.1",
proxy: proxy,
},
resolve: {
alias: {
$: path.resolve(__dirname, "./src"),
},
},
});
};
function injectAppTitlePlugin(appTitle: string) {
return {
name: "inject-app-title",
transformIndexHtml(html: string) {
return html.replace(/%APP_TITLE%/g, appTitle);
},
};
}
function resolveAppTitle() {
try {
const packageJsonPath = path.resolve(__dirname, "package.json");
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")) as {
name?: string;
"manifest.webapp"?: {
name?: string;
};
};
return packageJson["manifest.webapp"]?.name ?? packageJson.name ?? "Metadata Visualizer";
} catch {
return "Metadata Visualizer";
}
}
function getProxy(env: Record<string, string>, mode: string) {
const dhis2UrlVar = "VITE_DHIS2_BASE_URL";
const dhis2AuthVar = "DHIS2_AUTH";
const targetUrl = env[dhis2UrlVar];
const auth = env[dhis2AuthVar];
const isBuild = env.NODE_ENV === "production";
// The proxy is only needed by the dev server (`vite dev`, mode === "development").
// Other modes — vitest loads this config too but never hits the proxy — should
// skip the VITE_DHIS2_BASE_URL check so CI can load the config.
const isDevServer = mode === "development";
if (isBuild || !isDevServer) {
return {};
} else if (!targetUrl) {
console.error(`Set ${dhis2UrlVar}`);
process.exit(1);
} else {
const proxy: Record<string, ProxyOptions> = {
"/dhis2": {
target: targetUrl,
changeOrigin: true,
rewrite: path => path.replace(/^\/dhis2/, ""),
...(auth ? { auth } : {}),
},
};
return proxy;
}
}
function resolveBuildCommit() {
try {
return execSync("git rev-parse --short=12 HEAD", { stdio: ["ignore", "pipe", "ignore"] })
.toString()
.trim();
} catch {
return "unknown";
}
}
function resolveBuildTime(env: Record<string, string>) {
if (env.VITE_APP_BUILD_TIME) {
return env.VITE_APP_BUILD_TIME;
}
return new Date().toISOString();
}