forked from mue/mue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.mjs
More file actions
183 lines (161 loc) · 6.48 KB
/
vite.config.mjs
File metadata and controls
183 lines (161 loc) · 6.48 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
/**
* Vite Configuration for Mue Browser Extension
*
* This configuration handles building the extension for multiple browsers:
* - Chrome/Edge (Manifest V3)
* - Firefox (Manifest V3)
* - Safari (via Xcode project)
*
* Build Process:
* 1. Vite bundles the React app into /dist
* 2. Assets are copied to dist/src/assets for proper path resolution
* 3. Browser-specific builds are created in /build/{browser}
* 4. Distribution .zip files are generated for Chrome and Firefox
*
* Build Commands:
* - `bun run build` - Production build for all browsers
* - `bun run dev` - Development server with HMR
*/
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react-swc';
import path from 'path';
import fs from 'fs';
import ADMZip from 'adm-zip';
import * as pkg from './package.json';
import progress from 'vite-plugin-progress';
const isProd = process.env.NODE_ENV === 'production';
const prepareBuilds = () => ({
name: 'prepareBuilds',
closeBundle() {
if (isProd) {
console.log('📦 Building extension packages...');
// Ensure base directories exist
fs.mkdirSync(path.resolve(__dirname, './build'), { recursive: true });
fs.mkdirSync(path.resolve(__dirname, './dist'), { recursive: true });
// Copy assets to dist for proper bundling
const distAssetsPath = path.resolve(__dirname, './dist/src/assets');
fs.mkdirSync(distAssetsPath, { recursive: true });
fs.cpSync(path.resolve(__dirname, './src/assets'), distAssetsPath, { recursive: true });
// Chrome Build
console.log('🔨 Building Chrome extension...');
const chromeBuildPath = path.resolve(__dirname, './build/chrome');
fs.mkdirSync(chromeBuildPath, { recursive: true });
// Copy manifest and background script
fs.copyFileSync(
path.resolve(__dirname, './manifest/chrome.json'),
path.resolve(chromeBuildPath, 'manifest.json'),
);
fs.copyFileSync(
path.resolve(__dirname, './manifest/background.js'),
path.resolve(chromeBuildPath, 'background.js'),
);
// Copy locales
fs.cpSync(
path.resolve(__dirname, './manifest/_locales'),
path.resolve(chromeBuildPath, '_locales'),
{ recursive: true },
);
// Copy dist (bundled app)
fs.cpSync(path.resolve(__dirname, './dist'), chromeBuildPath, { recursive: true });
// Copy extension icons (separate from bundled assets)
fs.cpSync(
path.resolve(__dirname, './src/assets/icons'),
path.resolve(chromeBuildPath, 'icons'),
{ recursive: true },
);
// Firefox Build
console.log('🦊 Building Firefox extension...');
const firefoxBuildPath = path.resolve(__dirname, './build/firefox');
fs.mkdirSync(firefoxBuildPath, { recursive: true });
// Copy manifest and background script
fs.copyFileSync(
path.resolve(__dirname, './manifest/firefox.json'),
path.resolve(firefoxBuildPath, 'manifest.json'),
);
fs.copyFileSync(
path.resolve(__dirname, './manifest/background.js'),
path.resolve(firefoxBuildPath, 'background.js'),
);
// Copy dist (bundled app)
fs.cpSync(path.resolve(__dirname, './dist'), firefoxBuildPath, { recursive: true });
// Copy extension icons (separate from bundled assets)
fs.cpSync(
path.resolve(__dirname, './src/assets/icons'),
path.resolve(firefoxBuildPath, 'icons'),
{ recursive: true },
);
// Safari Build
console.log('🧭 Building Safari extension...');
const safariResourcesPath = path.resolve(__dirname, './safari/Mue Extension/Resources');
fs.mkdirSync(safariResourcesPath, { recursive: true });
// Copy background script
fs.copyFileSync(
path.resolve(__dirname, './manifest/background.js'),
path.resolve(safariResourcesPath, 'background.js'),
);
// Copy built files from dist (excluding manifest.json which Safari manages separately)
fs.cpSync(path.resolve(__dirname, './dist'), safariResourcesPath, {
recursive: true,
filter: (src) => !src.endsWith('manifest.json'),
});
// Copy extension icons
fs.cpSync(
path.resolve(__dirname, './src/assets/icons'),
path.resolve(safariResourcesPath, 'icons'),
{ recursive: true },
);
// Copy locales
fs.cpSync(
path.resolve(__dirname, './manifest/_locales'),
path.resolve(safariResourcesPath, '_locales'),
{ recursive: true },
);
// Create distribution zips
console.log('📦 Creating distribution packages...');
const chromeZip = new ADMZip();
chromeZip.addLocalFolder(chromeBuildPath);
chromeZip.writeZip(path.resolve(__dirname, `./build/chrome-${pkg.version}.zip`));
console.log(`✅ Chrome: chrome-${pkg.version}.zip`);
const firefoxZip = new ADMZip();
firefoxZip.addLocalFolder(firefoxBuildPath);
firefoxZip.writeZip(path.resolve(__dirname, `./build/firefox-${pkg.version}.zip`));
console.log(`✅ Firefox: firefox-${pkg.version}.zip`);
console.log('✨ Build complete!');
}
},
});
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '');
return {
define: { __APP_ENV__: JSON.stringify(env.APP_ENV) },
plugins: [react(), prepareBuilds(), progress()],
server: { open: true, hmr: { protocol: 'ws', host: 'localhost' } },
build: {
target: 'esnext', // Use modern JavaScript features
minify: isProd ? 'esbuild' : false,
sourcemap: !isProd,
rollupOptions: {
output: {
manualChunks: undefined, // Let Vite handle chunking automatically to avoid circular dependency issues
},
},
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {
'@': path.resolve(__dirname, './src'),
i18n: path.resolve(__dirname, './src/i18n'),
components: path.resolve(__dirname, './src/components'),
contexts: path.resolve(__dirname, './src/contexts'),
assets: path.resolve(__dirname, './src/assets'),
config: path.resolve(__dirname, './src/config'),
features: path.resolve(__dirname, './src/features'),
lib: path.resolve(__dirname, './src/lib'),
scss: path.resolve(__dirname, './src/scss'),
translations: path.resolve(__dirname, './src/i18n/locales'),
utils: path.resolve(__dirname, './src/utils'),
},
},
css: { preprocessorOptions: { scss: { api: 'modern-compiler' } } },
};
});