-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
126 lines (117 loc) · 4.13 KB
/
Copy pathvite.config.ts
File metadata and controls
126 lines (117 loc) · 4.13 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
import { defineConfig, Plugin } from 'vitest/config';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';
import { fileURLToPath } from 'url';
import fs from 'node:fs';
import path from 'node:path';
function indexNowPlugin(): Plugin {
return {
name: 'indexnow-key-generator',
buildStart() {
const key = (process.env.INDEXNOW_KEY || process.env.VITE_INDEXNOW_KEY || '').trim();
if (!key) return;
const rootDir = process.cwd();
const publicDir = path.join(rootDir, 'public');
if (fs.existsSync(publicDir)) {
const publicFilePath = path.join(publicDir, `${key}.txt`);
fs.writeFileSync(publicFilePath, `${key}\n`, 'utf-8');
console.log(`[indexnow-plugin] Wrote verification file: ${publicFilePath}`);
}
},
closeBundle() {
const key = (process.env.INDEXNOW_KEY || process.env.VITE_INDEXNOW_KEY || '').trim();
if (!key) return;
const rootDir = process.cwd();
const distDir = path.join(rootDir, 'dist');
if (fs.existsSync(distDir)) {
const distFilePath = path.join(distDir, `${key}.txt`);
fs.writeFileSync(distFilePath, `${key}\n`, 'utf-8');
console.log(`[indexnow-plugin] Wrote verification file to dist: ${distFilePath}`);
}
},
};
}
export function getBasePath(): string {
if (process.env.VERCEL === '1' || process.env.VERCEL) return '/';
if (process.env.VITE_BASE_PATH) {
return ('/' + process.env.VITE_BASE_PATH + '/').replace(/\/+/g, '/');
}
return '/';
}
export default defineConfig(() => {
const base = getBasePath();
return {
plugins: [
react(),
indexNowPlugin(),
visualizer({
filename: 'artifacts/stats.html',
open: false,
gzipSize: true,
brotliSize: true,
}),
{
name: 'spa-preview-fallback',
configurePreviewServer(server) {
const baseWithSlash = base.endsWith('/') ? base : `${base}/`;
const baseNoSlash = baseWithSlash.slice(0, -1);
server.middlewares.use((req, res, next) => {
try {
const url = req.url ?? '/';
const [pathname, rest] = url.split('?');
const query = rest ? `?${rest}` : '';
if (baseNoSlash && pathname === baseNoSlash) {
req.url = `${baseWithSlash}index.html${query}`;
if (req.originalUrl && req.originalUrl.startsWith(baseNoSlash)) {
req.originalUrl = req.originalUrl.replace(baseNoSlash, baseWithSlash);
}
} else if (
pathname.startsWith(baseWithSlash) &&
!pathname.startsWith(`${baseWithSlash}assets/`) &&
!pathname.match(/\.(html|js|css|png|jpg|jpeg|webp|avif|svg|ico|woff2?|ttf|eot|map|json|txt|xml)$/)
) {
req.url = `${baseWithSlash}index.html${query}`;
}
} catch (error) {
console.error('[spa-preview-fallback] Error processing URL fallback rewrite logic:', error);
}
next();
});
},
},
],
base,
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
build: {
rollupOptions: {
output: {
entryFileNames: 'assets/[name]-[hash].js',
chunkFileNames: 'assets/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash][extname]',
manualChunks(id) {
if (
id.includes('node_modules/react/') ||
id.includes('node_modules/react-dom/') ||
id.includes('node_modules/react-router/') ||
id.includes('node_modules/react-router-dom/') ||
id.includes('node_modules/react-helmet-async/')
) {
return 'vendor-react';
}
},
},
},
},
test: {
globals: true,
environment: 'jsdom',
setupFiles: './src/test/setup.ts',
testTimeout: 15000,
exclude: ['**/node_modules/**', '**/dist/**', '**/.tmp-main/**', '**/.pnpm-store/**', '**/cypress/**', '**/node_modules-main/**'],
},
};
});