generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathvite.config.ts
182 lines (172 loc) · 6.53 KB
/
vite.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/// <reference types="vitest" />
import { resolve } from "path";
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";
import mdx from "@mdx-js/rollup";
import rehypeSlug from "rehype-slug";
import { remarkMdxToc } from "remark-mdx-toc";
import remarkFrontmatter from "remark-frontmatter";
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
import { checker } from "vite-plugin-checker";
const LOCAL_BACKEND_MODES = ["preview", "development", "test", "csp", "ci"];
const LOCAL_PROXY_MODES = ["preview", "development", "test", "csp"];
const DEMO_MODES = /^demo\d+$/;
const TRIALFRONTEND_MODES = /^trialfrontend\d+$/;
/**
* Determine the backend url based on mode.
*/
function createProxyUrl(mode: string) {
if (LOCAL_PROXY_MODES.includes(mode)) return "http://127.0.0.1:7071";
const subdomain = mode === "production" ? "" : mode === "ci" ? "staging" : mode;
return `https://${subdomain ? subdomain + "." : ""}prime.cdc.gov`;
}
function createBackendUrl(mode: string) {
const port = getPort(mode);
if (LOCAL_BACKEND_MODES.includes(mode)) return `http://localhost${port ? ":" + port.toString() : ""}`;
return `https://${mode !== "production" ? (mode.startsWith("trialfrontend") ? "staging" : mode) + "." : ""}prime.cdc.gov`;
}
function getPort(mode: string) {
switch (mode) {
case "ci":
case "test":
case "csp":
case "preview":
return 4173;
default:
return 3000;
}
}
/**
* Simplify known number-ranged modes to base mode (ex: trialfrontend01 -> trialfrontend)
*/
function loadRedirectedEnv(mode: string) {
let redirectedMode = mode;
if (mode === "ci") redirectedMode = "test";
if (DEMO_MODES.exec(mode)) redirectedMode = "demo";
else if (TRIALFRONTEND_MODES.exec(mode)) redirectedMode = "trialfrontend";
return loadEnv(redirectedMode, process.cwd());
}
// https://vitejs.dev/config/
export default defineConfig(async ({ mode }) => {
const env = loadRedirectedEnv(mode);
const isCsp = mode === "csp";
const port = getPort(mode);
const backendUrl = env.VITE_BACKEND_URL ?? createBackendUrl(mode);
const proxyUrl = env.PROXY_URL ?? createProxyUrl(mode);
return {
define: {
"import.meta.env.VITE_BACKEND_URL": JSON.stringify(backendUrl),
},
optimizeDeps: {
include: ["react/jsx-runtime"],
},
plugins: [
react(),
mdx({
mdExtensions: [],
providerImportSource: "@mdx-js/react",
remarkPlugins: [remarkMdxToc, remarkFrontmatter, remarkMdxFrontmatter],
rehypePlugins: [rehypeSlug],
}),
svgr(),
checker({
typescript: true,
eslint: {
lintCommand: 'eslint "./src/**/*[!.test][!.stories].{ts,tsx}"',
useFlatConfig: true,
},
}),
],
server: {
port,
open: true,
// Proxy localhost/api to local prime-router
proxy: {
"/api": {
target: proxyUrl,
changeOrigin: true,
},
},
headers: {
"content-security-policy": isCsp
? "default-src 'self';" +
" script-src 'self'" +
" https://reportstream.oktapreview.com" +
" https://global.oktacdn.com" +
" https://www.google-analytics.com" +
" https://*.in.applicationinsights.azure.com" +
" https://dap.digitalgov.gov" +
" https://www.googletagmanager.com;" +
" style-src 'self' 'unsafe-inline'" +
" https://global.oktacdn.com" +
" https://cdnjs.cloudflare.com;" +
" frame-src 'self'" +
" https://reportstream.oktapreview.com;" +
" img-src 'self'" +
" https://reportstream.oktapreview.com" +
` https://localhost:${port}` +
" data:;" +
" connect-src 'self'" +
" https://www.google-analytics.com" +
" https://*.in.applicationinsights.azure.com" +
" https://reportstream.oktapreview.com" +
` http://localhost:${port}/api/` +
" https://dap.digitalgov.gov" +
" https://www.googletagmanager.com" +
" https://js.monitor.azure.com/"
: "",
},
},
build: {
outDir: "build",
target: "esnext",
assetsDir: "assets/app",
sourcemap: true,
rollupOptions: {
input: {
// Key alphabetical order is important, otherwise
// rollup will name the bundle something other
// than index
index: resolve(__dirname, "index.html"),
notfound: resolve(__dirname, "404.html"),
unsupportedBrowser: resolve(__dirname, "unsupported-browser.html"),
},
},
},
css: {
preprocessorOptions: {
scss: {
loadPaths: ["node_modules/@uswds/uswds/packages"],
},
},
devSourcemap: true,
},
test: {
globals: true,
environment: "jsdom",
setupFiles: "./src/setupTests.ts",
globalSetup: "./src/globalSetup.ts",
include: ["./src/**/__tests__/**/*.[jt]s?(x)", "./src/**/?(*.)+(spec|test).[jt]s?(x)"],
css: false,
coverage: {
include: ["src/**/*.{js,jsx,ts,tsx}", "!src/**/*.d.ts"],
provider: "istanbul",
all: false,
reporter: ["clover", "json", "lcov", "text"],
},
clearMocks: true, // TODO: re-evalulate this setting,
server: {
deps: {
// Allows for mocking peer dependencies these libraries import
inline: ["@trussworks/react-uswds"],
},
},
},
resolve: {
alias: {
"msw/native": resolve(__dirname, "./node_modules/msw/lib/native/index.mjs"),
},
},
};
});