-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvite.config.ts
175 lines (167 loc) · 5.18 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
/// <reference types="vitest" />
import { esbuildCommonjs } from "@originjs/vite-plugin-commonjs";
import { sentryVitePlugin } from "@sentry/vite-plugin";
import react from "@vitejs/plugin-react";
import { visualizer } from "rollup-plugin-visualizer";
import { defineConfig, mergeConfig } from "vite";
import { checker } from "vite-plugin-checker";
import envCompatible from "vite-plugin-env-compatible";
import vitePluginImp from "vite-plugin-imp";
import tsconfigPaths from "vite-tsconfig-paths";
import { defineConfig as defineTestConfig } from "vitest/config";
import dns from "dns";
import * as fs from "fs";
import { createRequire } from "node:module";
import path from "path";
import { generateBaseHTTPSViteServerConfig } from "@evg-ui/vite-utils";
import injectVariablesInHTML from "./config/injectVariablesInHTML";
const getProjectConfig = () => {
const require = createRequire(import.meta.url);
// Remove when https://github.com/cypress-io/cypress/issues/25397 is resolved.
dns.setDefaultResultOrder("ipv4first");
// Do not apply Antd's global styles
fs.writeFileSync(require.resolve("antd/es/style/core/global.less"), "");
fs.writeFileSync(require.resolve("antd/lib/style/core/global.less"), "");
const serverConfig = generateBaseHTTPSViteServerConfig({
port: 3000,
appURL: process.env.REACT_APP_SPRUCE_URL,
httpsPort: 8443,
useHTTPS:
process.env.REACT_APP_RELEASE_STAGE !== "local" &&
process.env.NO_HTTPS !== "true",
});
// https://vitejs.dev/config/
const viteConfig = defineConfig({
server: serverConfig,
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: "globalThis",
},
// Enable esbuild polyfill plugins
plugins: [esbuildCommonjs(["antd"])],
},
},
build: {
sourcemap: true,
rollupOptions: {
output: {
manualChunks: {
vendor: [
"react",
"react-router-dom",
"react-dom",
"react-router",
"lodash",
"antd",
],
},
},
},
},
resolve: {
alias: {
"@leafygreen-ui/emotion": path.resolve(
__dirname,
"./config/leafygreen-ui/emotion",
),
...(process.env.PROFILER === "true" && {
"react-dom/client": path.resolve(
__dirname,
"../../node_modules/react-dom/profiling",
),
"scheduler/tracing": path.resolve(
__dirname,
"../../node_modules/scheduler/tracing-profiling",
),
}),
},
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],
},
plugins: [
tsconfigPaths(),
// Inject env variables
envCompatible({
prefix: "REACT_APP_",
}),
// Use emotion jsx tag instead of React.JSX
react({
jsxImportSource: "@emotion/react",
babel: {
plugins: ["@emotion/babel-plugin", "import-graphql"],
},
// exclude storybook stories
exclude: [/\.stories\.tsx?$/],
}),
// Replace the variables in our HTML files.
injectVariablesInHTML({
files: "dist/index.html",
variables: [
"%REACT_APP_VERSION%",
"%GIT_SHA%",
"%REACT_APP_RELEASE_STAGE%",
"%NODE_ENV%",
"%PROFILE_HEAD%",
],
}),
// Dynamic imports of antd styles
vitePluginImp({
optimize: true,
libList: [
{
libName: "antd",
libDirectory: "es",
style: (name) => `antd/es/${name}/style/index.js`,
},
{
libName: "lodash",
libDirectory: "",
camel2DashComponentName: false,
style: (name) => `lodash/${name}`,
},
],
}),
// Typescript checking
checker({ typescript: true }),
// Bundle analyzer
visualizer({
filename: "dist/source_map.html",
template: "treemap",
}),
sentryVitePlugin({
org: "mongodb-org",
project: "spruce",
disable: process.env.NODE_ENV === "development",
authToken: process.env.SPRUCE_SENTRY_AUTH_TOKEN,
release: {
name: process.env.npm_package_version,
},
sourcemaps: {
assets: "dist/assets/*",
},
}),
],
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true, // enable LESS {@import ...}
},
},
},
});
const vitestConfig = defineTestConfig({
test: {
environment: "jsdom",
globals: true,
globalSetup: "./config/vitest/global-setup.ts",
outputFile: { junit: "./bin/vitest/junit.xml" },
reporters: ["default", ...(process.env.CI === "true" ? ["junit"] : [])],
setupFiles: "./config/vitest/setupTests.ts",
},
});
return mergeConfig(viteConfig, vitestConfig);
};
/** `useProjectConfig` determines if we are running vite as part of a script using vite-node. If so we should return a bare bones config. */
const useProjectConfig = process.env.VITE_SCRIPT_MODE !== "1";
export default useProjectConfig ? getProjectConfig() : defineConfig({});