-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
110 lines (101 loc) · 3.85 KB
/
Copy pathvite.config.ts
File metadata and controls
110 lines (101 loc) · 3.85 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
/// <reference types="vitest" />
import { UserConfig, defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import nodePolyfills from "vite-plugin-node-stdlib-browser";
import * as path from "path";
const redirectPaths = ["/dhis-web-pivot", "/dhis-web-data-visualizer", "/dhis-web-commons-ajax-json"];
export default defineConfig(({ mode }): UserConfig => {
const isLibraryMode = mode === "library";
if (isLibraryMode) {
return defineConfig({
plugins: [nodePolyfills(), react()],
build: {
lib: {
entry: path.resolve(__dirname, "src/lib/index.ts"),
name: "TrainingApp",
formats: ["es", "cjs"],
fileName: format => `index.${format === "es" ? "es" : "cjs"}.js`,
},
rollupOptions: {
external: ["react", "react-dom", "styled-components"],
},
},
resolve: {
alias: {
src: path.resolve(__dirname, "./src"),
},
},
});
}
const env = loadEnv(mode, process.cwd(), "");
const proxy = getProxy(env);
const port = Number(env.VITE_PORT || env.PORT || 8081);
return {
base: "",
plugins: [nodePolyfills(), react()],
resolve: {
alias: {
src: path.resolve(__dirname, "./src"),
// Algunas dependencias hacen `require("process/")` y esbuild intentaba
// resolverlo a un directorio (sin index). Lo forzamos a un archivo.
"process/": path.resolve(__dirname, "node_modules/node-stdlib-browser/cjs/proxy/process.js"),
process: path.resolve(__dirname, "node_modules/node-stdlib-browser/cjs/proxy/process.js"),
},
},
server: {
port,
proxy,
},
test: {
environment: "jsdom",
globals: true,
include: ["**/*.{test,spec}.{ts,tsx}"],
setupFiles: "./src/tests/setup.ts",
exclude: ["node_modules"],
},
};
});
function getProxy(env: Record<string, string>) {
const targetUrl = env.VITE_DHIS2_BASE_URL || env.REACT_APP_DHIS2_BASE_URL;
const auth = env.VITE_DHIS2_AUTH || env.REACT_APP_DHIS2_AUTH;
const logLevel = env.VITE_PROXY_LOG_LEVEL || env.REACT_APP_PROXY_LOG_LEVEL || "warn";
const isBuild = env.NODE_ENV === "production";
if (isBuild) return {};
if (!targetUrl) {
// Keep the same behavior as previous setupProxy.js in development.
throw new Error("Set VITE_DHIS2_BASE_URL (or REACT_APP_DHIS2_BASE_URL)");
}
const shared = {
target: targetUrl,
auth,
changeOrigin: true,
configure: proxy => {
proxy.on("proxyReq", (proxyReq, req, res) => {
const reqPath = proxyReq.path;
const shouldRedirect = redirectPaths.some(redirectPath => reqPath.startsWith(redirectPath));
if (!shouldRedirect || !res || !("writeHead" in res)) return;
const redirectUrl = targetUrl.replace(/\/$/, "") + reqPath;
// `res` is a node response object at runtime.
(res as any).writeHead(302, { Location: redirectUrl });
(res as any).end();
});
proxy.on("error", error => {
console.error(`[proxy:${logLevel}]`, error);
});
},
};
return {
"/dhis2": {
...shared,
rewrite: (p: string) => p.replace(/^\/dhis2/, "/"),
},
"/documents": {
...shared,
rewrite: (p: string) => p.replace(/^\/documents/, "/api/documents"),
},
"/api": {
...shared,
rewrite: (p: string) => p.replace(/^\/api/, "/api"),
},
};
}