-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathvite.config.js
More file actions
370 lines (347 loc) · 11.3 KB
/
vite.config.js
File metadata and controls
370 lines (347 loc) · 11.3 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/// <reference types="vitest" />
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import basicSsl from '@vitejs/plugin-basic-ssl';
import svgLoader from 'vite-svg-loader';
import viteCompression from 'vite-plugin-compression';
import { fileURLToPath, URL } from 'node:url';
import path from 'node:path';
import fs from 'node:fs';
// Plugin to resolve directory imports to index.js (like Webpack does)
function resolveDirectoryIndex() {
return {
name: 'resolve-directory-index',
resolveId(source, importer) {
if (!importer || source.startsWith('\0')) return null;
// Resolve the full path
let resolved;
if (source.startsWith('@/')) {
resolved = path.resolve(__dirname, 'src', source.slice(2));
} else if (source.startsWith('./') || source.startsWith('../')) {
resolved = path.resolve(path.dirname(importer), source);
} else {
return null;
}
// Check if it's a directory with an index.js
try {
const stats = fs.statSync(resolved);
if (stats.isDirectory()) {
const indexPath = path.join(resolved, 'index.js');
if (fs.existsSync(indexPath)) {
return indexPath;
}
}
} catch {
// Path doesn't exist, let Vite handle it
}
return null;
},
};
}
export default defineConfig(({ mode }) => {
// Load env file based on `mode` in the current working directory.
const env = loadEnv(mode, process.cwd(), '');
const envName = env.VITE_ENV_NAME;
const hasCustomStyles = env.CUSTOM_STYLES === 'true';
const hasCustomStore = env.CUSTOM_STORE === 'true';
const hasCustomRouter = env.CUSTOM_ROUTER === 'true';
const hasCustomAppNav = env.CUSTOM_APP_NAV === 'true';
// Build SCSS additionalData for prepending imports
const scssAdditionalData = (() => {
if (hasCustomStyles && envName !== undefined) {
return `
@import "@/assets/styles/bmc/helpers";
@import "@/env/assets/styles/_${envName}";
@import "@/assets/styles/bootstrap/_helpers";
`;
} else {
return `
@import "@/assets/styles/bmc/helpers";
@import "@/assets/styles/bootstrap/_helpers";
`;
}
})();
// Build custom aliases for environment-specific overrides
const customAliases = {};
if (envName !== undefined) {
if (hasCustomStore) {
// If env has custom store, resolve all store modules
customAliases['./store'] = path.resolve(
__dirname,
`src/env/store/${envName}.js`,
);
customAliases['../store'] = path.resolve(
__dirname,
`src/env/store/${envName}.js`,
);
}
if (hasCustomRouter) {
// If env has custom router, resolve routes
customAliases['./routes'] = path.resolve(
__dirname,
`src/env/router/${envName}.js`,
);
}
if (hasCustomAppNav) {
// If env has custom AppNavigation
customAliases['./AppNavigationMixin'] = path.resolve(
__dirname,
`src/env/components/AppNavigation/${envName}.js`,
);
}
}
// Helper to inject auth token from cookie
const injectAuthToken = (proxyReq, req) => {
const cookies = req.headers.cookie;
if (cookies) {
const match = cookies.match(/X-Auth-Token=([^;]+)/);
if (match) {
proxyReq.setHeader('X-Auth-Token', match[1]);
}
}
};
// Helper to remove HSTS header
const removeHsts = (proxyRes) => {
delete proxyRes.headers['strict-transport-security'];
};
// Check if HTTPS should be enabled (default: true)
const useHttps = env.DEV_HTTPS !== 'false';
return {
plugins: [
resolveDirectoryIndex(),
vue(),
svgLoader({
defaultImport: 'component',
}),
// Enable HTTPS with auto-generated self-signed certificate
...(useHttps ? [basicSsl()] : []),
// Compression for production builds
...(mode === 'production'
? [
viteCompression({
deleteOriginFile: true,
algorithm: 'gzip',
}),
]
: []),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
...customAliases,
},
// Allow importing without extensions (like Vue CLI did)
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
},
css: {
preprocessorOptions: {
scss: {
additionalData: scssAdditionalData,
// Silence Sass deprecation warnings from Bootstrap and other dependencies.
// Bootstrap is working on a long-term fix for Dart Sass compatibility.
// See: https://getbootstrap.com/docs/5.3/customize/sass/#importing
silenceDeprecations: ['import'],
quietDeps: true,
},
sass: {
additionalData: scssAdditionalData,
silenceDeprecations: ['import'],
quietDeps: true,
},
},
},
define: {
// Vue 3 compile-time feature flags
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false,
// Expose session storage toggle to client code
'import.meta.env.VITE_STORE_SESSION': JSON.stringify(
env.VITE_STORE_SESSION || env.STORE_SESSION || '',
),
},
server: {
port: 8000,
// HTTPS is enabled via basicSsl plugin above
// Disable HMR WebSocket to avoid conflicts with app websockets
hmr: {
path: '/ws_hmr',
},
proxy: {
'/redfish': {
target: env.BASE_URL,
changeOrigin: true,
secure: false,
configure: (proxy) => {
proxy.on('proxyReq', (proxyReq, req) => {
injectAuthToken(proxyReq, req);
// Detect if this is a browser navigation vs an API call
const isApiCall =
req.headers['x-requested-with'] === 'XMLHttpRequest';
if (!isApiCall) {
proxyReq.setHeader(
'Accept',
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
);
proxyReq.removeHeader('accept-encoding');
}
// Fix referer to match BMC host
if (req.headers.referer && env.BASE_URL) {
try {
const refererUrl = new URL(req.headers.referer);
const bmcUrl = new URL(env.BASE_URL);
refererUrl.protocol = bmcUrl.protocol;
refererUrl.hostname = bmcUrl.hostname;
refererUrl.port = bmcUrl.port;
proxyReq.setHeader('Referer', refererUrl.toString());
} catch (e) {
// If URL parsing fails, leave referer unchanged
}
}
// Remove x-forwarded headers
proxyReq.removeHeader('x-forwarded-host');
proxyReq.removeHeader('x-forwarded-proto');
proxyReq.removeHeader('x-forwarded-port');
proxyReq.removeHeader('x-forwarded-for');
});
proxy.on('proxyRes', (proxyRes) => {
removeHsts(proxyRes);
delete proxyRes.headers['content-encoding'];
});
},
},
'/login': {
target: env.BASE_URL,
changeOrigin: true,
secure: false,
configure: (proxy) => {
proxy.on('proxyRes', removeHsts);
},
},
'/kvm': {
target: env.BASE_URL,
changeOrigin: true,
secure: false,
ws: true,
configure: (proxy) => {
proxy.on('proxyRes', removeHsts);
proxy.on('proxyReqWs', (proxyReq, req) => {
const cookies = req.headers.cookie;
if (cookies) {
const match = cookies.match(/X-Auth-Token=([^;]+)/);
if (match) {
proxyReq.setHeader('X-Auth-Token', match[1]);
}
}
});
proxy.on('error', (err) => {
console.error('[vite] /kvm proxy error:', err.message);
});
},
},
'/console': {
target: env.BASE_URL,
changeOrigin: true,
secure: false,
ws: true,
configure: (proxy) => {
proxy.on('proxyRes', removeHsts);
proxy.on('proxyReqWs', (proxyReq, req) => {
// Forward the auth token from cookies for WebSocket connections
const cookies = req.headers.cookie;
if (cookies) {
const match = cookies.match(/X-Auth-Token=([^;]+)/);
if (match) {
proxyReq.setHeader('X-Auth-Token', match[1]);
}
}
});
proxy.on('error', (err) => {
console.error('[vite] /console proxy error:', err.message);
});
},
},
'/vm': {
target: env.BASE_URL,
changeOrigin: true,
secure: false,
ws: true,
configure: (proxy) => {
proxy.on('proxyRes', removeHsts);
proxy.on('proxyReqWs', (proxyReq, req) => {
const cookies = req.headers.cookie;
if (cookies) {
const match = cookies.match(/X-Auth-Token=([^;]+)/);
if (match) {
proxyReq.setHeader('X-Auth-Token', match[1]);
}
}
});
proxy.on('error', (err) => {
console.error('[vite] /vm proxy error:', err.message);
});
},
},
'/styles/redfish.css': {
target: env.BASE_URL,
changeOrigin: true,
secure: false,
configure: (proxy) => {
proxy.on('proxyReq', injectAuthToken);
proxy.on('proxyRes', removeHsts);
},
},
'/images/DMTF_Redfish_logo_2017.svg': {
target: env.BASE_URL,
changeOrigin: true,
secure: false,
configure: (proxy) => {
proxy.on('proxyReq', injectAuthToken);
proxy.on('proxyRes', removeHsts);
},
},
},
},
build: {
// Generate hashed filenames
rollupOptions: {
output: {
// Single chunk output (like LimitChunkCountPlugin with maxChunks: 1)
manualChunks: undefined,
entryFileNames: 'js/[name].[hash].js',
chunkFileNames: 'js/[name].[hash].js',
assetFileNames: (assetInfo) => {
if (assetInfo.name?.endsWith('.css')) {
return 'css/[name].[hash][extname]';
}
return 'assets/[name].[hash][extname]';
},
},
},
// Disable source maps in production
sourcemap: false,
// Performance hints
chunkSizeWarningLimit: 512,
},
// Handle .ico files
assetsInclude: ['**/*.ico'],
// Vitest configuration
test: {
globals: true,
environment: 'happy-dom',
setupFiles: ['./tests/vitest.setup.js'],
include: ['tests/unit/**/*.spec.js'],
css: false,
snapshotSerializers: ['vue3-snapshot-serializer'],
server: {
deps: {
inline: ['@carbon/icons-vue'],
},
},
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
},
},
};
});