-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
119 lines (111 loc) · 3.06 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
import path from "path";
import { defineConfig, loadEnv, Plugin, ConfigEnv, UserConfig, PluginOption } from "vite";
import react from "@vitejs/plugin-react";
import { startMockupServer } from "./mockup-server";
// https://vitejs.dev/config/
export default defineConfig(async ({ mode, command }: ConfigEnv): Promise<UserConfig> => {
const env = loadEnv(mode, process.cwd(), "");
const useMockup = env.VITE_MODE === "mockup";
const isBuild = command === 'build';
// Start mockup server if VITE_MODE is set to mockup AND we're not in build mode
let mockupServer = null;
if (useMockup && !isBuild) {
mockupServer = await startMockupServer();
console.log(`🔶 Using mockup server at ${mockupServer.url}`);
}
// API target - use mockup server or the configured API URL
const apiTarget = (useMockup && !isBuild)
? mockupServer?.url
: env.VITE_API_URL;
// Create plugins array
const plugins: PluginOption[] = [react()];
// Add mockup plugin only if mockup mode is enabled and we're not building
if (useMockup && !isBuild) {
const mockupPlugin: Plugin = {
name: 'mockup-mode-plugin',
configureServer(server) {
server.middlewares.use((req, res, next) => {
// Add an indicator in dev tools that we're in mockup mode
if (req.url?.endsWith('.html')) {
server.transformIndexHtml(req.url, `
<!-- Running in mockup mode -->
<script>
console.log('%c🔶 MOCKUP MODE ACTIVE', 'background: #FFC107; color: #000; padding: 4px 8px; border-radius: 4px;');
</script>
`).then(html => {
res.setHeader('Content-Type', 'text/html');
res.end(html);
});
return;
}
next();
});
},
// This will be called when Vite is closing
closeBundle() {
if (mockupServer) {
mockupServer.close().catch(err => {
console.error('Error closing mockup server:', err);
});
console.log('🔶 Mockup server closed');
}
}
};
plugins.push(mockupPlugin);
}
// Configure proxy differently based on whether we're in mockup mode
const proxyConfig = (useMockup && !isBuild)
? {
// In mockup mode, proxy everything to the mockup server
"/rest.php": {
target: apiTarget,
changeOrigin: true,
secure: false,
},
"/api": {
target: apiTarget,
changeOrigin: true,
secure: false,
rewrite: (p: string) => p.replace(/^\/api/, ""),
}
}
: {
// Normal proxy configuration
"/api": {
target: apiTarget,
changeOrigin: true,
secure: false,
rewrite: (p: string) => p.replace(/^\/api/, ""),
}
};
return {
define: {
// We don't expose the mockup mode to client code
},
build: {
assetsInlineLimit: 8096,
rollupOptions: {
output: {
format: "iife",
entryFileNames: `assets/[name].js`,
chunkFileNames: `assets/[name].js`,
assetFileNames: `assets/[name].[ext]`,
},
},
},
server: {
proxy: proxyConfig,
cors: false,
},
preview: {
proxy: proxyConfig,
cors: false,
},
plugins,
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
};
});