|
1 | 1 | import { defineConfig, loadEnv } from "vite"; |
2 | 2 | import react from "@vitejs/plugin-react"; |
3 | 3 |
|
| 4 | +const normalizePath = (value, fallback = "/cwms-data") => { |
| 5 | + /* Returns a normalized path segment from a URL or path string. */ |
| 6 | + |
| 7 | + const candidate = value || fallback; |
| 8 | + let pathname = candidate; |
| 9 | + |
| 10 | + try { |
| 11 | + pathname = new URL(candidate).pathname; |
| 12 | + } catch { |
| 13 | + // Use as-is when candidate is already a path segment. |
| 14 | + } |
| 15 | + // remove trailing slashes and ensure leading slash |
| 16 | + if (!pathname.startsWith("/")) pathname = `/${pathname}`; |
| 17 | + return pathname.replace(/\/+$/, "") || "/"; |
| 18 | +}; |
| 19 | + |
4 | 20 | // https://vitejs.dev/config/ |
5 | 21 | export default defineConfig(({ mode }) => { |
6 | 22 | const env = loadEnv(mode, ".", ""); |
7 | 23 | const BASE_PATH = env.VITE_BASE_PATH || "/cwms-data"; |
8 | | - const CDA_PATH = env.VITE_CDA_URL || BASE_PATH; |
| 24 | + const CDA_PATH = normalizePath(env.VITE_CDA_URL, BASE_PATH); |
9 | 25 | const proxyTarget = env.VITE_CDA_PROXY_TARGET || "https://cwms-data.usace.army.mil"; |
10 | 26 |
|
11 | 27 | return { |
12 | 28 | base: BASE_PATH, |
13 | 29 | plugins: [react()], |
14 | 30 | server: { |
15 | 31 | proxy: { |
16 | | - [`^${CDA_PATH}/timeseries/.*`]: { |
17 | | - target: proxyTarget, |
18 | | - changeOrigin: true, |
19 | | - secure: false, |
20 | | - }, |
21 | | - [`^${CDA_PATH}/catalog/.*`]: { |
| 32 | + [`^${CDA_PATH}/.*`]: { |
22 | 33 | target: proxyTarget, |
23 | 34 | changeOrigin: true, |
24 | 35 | secure: false, |
25 | 36 | }, |
26 | 37 | }, |
27 | | - }, |
28 | | - experimental: { |
29 | | - renderBuiltUrl(filename, { hostType }) { |
30 | | - if (hostType === "js" || hostType === "css") { |
31 | | - return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` }; |
32 | | - } else { |
33 | | - return { relative: true }; |
34 | | - } |
35 | | - }, |
36 | | - }, |
37 | | - }; |
38 | | -}); |
| 38 | + }, |
| 39 | + experimental: { |
| 40 | + renderBuiltUrl(filename, { hostType }) { |
| 41 | + if (hostType === "js" || hostType === "css") { |
| 42 | + return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` }; |
| 43 | + } else { |
| 44 | + return { relative: true }; |
| 45 | + } |
| 46 | + }, |
| 47 | + }, |
| 48 | + }; |
| 49 | +}); |
0 commit comments