-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
142 lines (134 loc) · 3.93 KB
/
vite.config.ts
File metadata and controls
142 lines (134 loc) · 3.93 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
import { execSync } from 'node:child_process';
import { copyFileSync, existsSync, mkdirSync } from 'node:fs';
import { resolve } from 'node:path';
import { cwd } from 'node:process';
import tailwindcss from '@tailwindcss/vite';
import react from '@vitejs/plugin-react';
import { defineConfig, type UserConfig, type ViteDevServer } from 'vite';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';
const BUNDLE_FILE = 'widget.mjs';
const ENTRY_FILE = 'src/index.tsx';
const getBuildConfig = (options: { minify: boolean | 'terser'; outDir: string }): UserConfig => ({
mode: 'production',
resolve: { alias: { '@': resolve(cwd(), 'src') } },
define: {
'process.env.NODE_ENV': '"production"',
'process.env': '{}',
'global.process': 'undefined',
process: 'undefined',
__BUILD_COMMIT__: JSON.stringify(process.env.BUILD_COMMIT || 'dev'),
},
css: { devSourcemap: false },
build: {
outDir: options.outDir,
emptyOutDir: true,
sourcemap: true,
minify: options.minify,
target: 'esnext',
codeSplitting: false,
cssCodeSplit: false,
lib: {
entry: ENTRY_FILE,
formats: ['es'],
fileName: 'widget',
},
rollupOptions: {
external: ['react', 'react-dom', 'react-dom/client', 'react/jsx-runtime'],
output: {
entryFileNames: BUNDLE_FILE,
format: 'es',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'react-dom/client': 'ReactDOMClient',
'react/jsx-runtime': 'jsxRuntime',
},
},
},
...(options.minify === 'terser' && {
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log', 'console.info', 'console.debug'],
},
format: {
comments: false,
},
},
}),
},
plugins: [
react({
jsxRuntime: 'automatic',
jsxImportSource: 'react',
}),
tailwindcss(),
cssInjectedByJsPlugin(),
copyIndexHtmlPlugin(options.outDir),
typescriptDeclarationPlugin(),
],
});
const copyIndexHtmlPlugin = (outDir: string) => {
return {
name: 'copy-index-html',
closeBundle() {
const indexPath = resolve(cwd(), 'index.html');
const destDir = resolve(cwd(), outDir);
const destPath = resolve(destDir, 'index.html');
if (existsSync(indexPath)) {
// Ensure destination directory exists
if (!existsSync(destDir)) {
mkdirSync(destDir, { recursive: true });
}
copyFileSync(indexPath, destPath);
console.log(`✓ Copied index.html to ${outDir}/`);
}
},
};
};
const typescriptDeclarationPlugin = () => {
return {
name: 'typescript-declarations',
async closeBundle() {
try {
console.log('Generating TypeScript declarations...');
execSync('tsc -p tsconfig.build.json', { stdio: 'inherit', cwd: cwd() });
console.log('✓ TypeScript declarations generated');
} catch (error) {
console.error('TypeScript declaration generation failed');
throw error;
}
},
};
};
export default defineConfig(({ command }) => {
const isProduction = command === 'build';
if (isProduction) {
return getBuildConfig({ minify: 'terser', outDir: 'dist' });
}
return {
resolve: { alias: { '@': resolve(cwd(), 'src') } },
plugins: [
react(),
tailwindcss(),
// Rewrite /widget.mjs to the source entry so the production URL works in dev
{
name: 'widget-dev-routing',
configureServer(server: ViteDevServer) {
server.middlewares.use((req, _res, next) => {
if (req.url === '/widget.mjs') req.url = '/src/index.tsx';
next();
});
},
},
],
appType: 'mpa',
root: '.',
server: {
port: parseInt(process.env.PORT || process.env.VITE_PORT || '9001', 10),
cors: true,
headers: { 'Access-Control-Allow-Origin': '*' },
},
};
});