-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
35 lines (32 loc) · 995 Bytes
/
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
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { Plugin } from 'vite';
// Custom plugin to handle production-only content
const htmlEnvPlugin = (): Plugin => {
return {
name: 'html-env-plugin',
transformIndexHtml: {
enforce: 'pre',
transform(html, { server }) {
// Check if we're in production mode
const isProd = !server;
if (isProd) {
// In production: Remove the placeholder tags but keep the content
return html
.replace(/%PROD_ONLY_START%/g, '')
.replace(/%PROD_ONLY_END%/g, '');
} else {
// In development: Remove the tags and their content
return html.replace(
/%PROD_ONLY_START%[\s\S]*?%PROD_ONLY_END%/g,
'<!-- Analytics code removed in development -->',
);
}
},
},
};
};
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), htmlEnvPlugin()],
});