-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
272 lines (238 loc) · 8.62 KB
/
vite.config.ts
File metadata and controls
272 lines (238 loc) · 8.62 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import react from '@vitejs/plugin-react';
import pino from 'pino';
import { defineConfig, type Plugin, type PluginOption } from 'vite';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import tsconfigPaths from 'vite-tsconfig-paths';
import { extensionReloadPlugin } from './src/utils/extension-reload-plugin';
// Logger setup
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'HH:MM:ss',
ignore: 'pid,hostname',
},
},
});
const copyBrowserExtensionFiles = (targetBrowser: string, outDir: string, isDev: boolean = false): void => {
logger.info({ outDir }, 'Copying browser extension files');
// Ensure build directory exists
mkdirSync(outDir, { recursive: true });
// Copy index.html as popup.html
const indexSrc = resolve(outDir, 'src/index.html');
const popupDest = resolve(outDir, 'popup.html');
if (existsSync(indexSrc)) {
copyFileSync(indexSrc, popupDest);
}
// Copy background.html as background.html
const backgroundSrc = resolve(outDir, 'src/background.html');
const backgroundDest = resolve(outDir, 'background.html');
if (existsSync(backgroundSrc)) {
copyFileSync(backgroundSrc, backgroundDest);
}
// Copy files from source directory if they exist
const srcDir = resolve(outDir, 'src');
if (existsSync(srcDir)) {
const files = readdirSync(srcDir);
files.forEach((file: string) => {
if (file.endsWith('.html')) {
const srcFile = resolve(srcDir, file);
const destFile = resolve(outDir, file);
copyFileSync(srcFile, destFile);
}
});
}
// Check if background.bundle.js exists from the separate build
const backgroundBundlePath = resolve(outDir, 'background.bundle.js');
if (existsSync(backgroundBundlePath)) {
logger.info('Background bundle already exists in target directory');
} else {
logger.warn('Background bundle not found in target directory');
}
// Ensure img directory exists and copy assets
const imgSrc = resolve(outDir, 'src/assets/img');
const imgDest = resolve(outDir, 'img');
if (existsSync(imgSrc)) {
mkdirSync(imgDest, { recursive: true });
const files = readdirSync(imgSrc);
files.forEach((file: string) => {
const srcFile = resolve(imgSrc, file);
const destFile = resolve(imgDest, file);
copyFileSync(srcFile, destFile);
});
}
// Copy manifest file LAST to ensure it's available only when everything is ready
let manifestSrc: string;
if (isDev) {
manifestSrc = 'manifest.dev.json';
} else {
manifestSrc = targetBrowser === 'firefox' ? 'manifest.firefox.json' : 'manifest.chromium.json';
}
const manifestDest = resolve(outDir, 'manifest.json');
logger.info({ manifestSrc, manifestDest }, 'Copying manifest');
if (existsSync(manifestSrc)) {
copyFileSync(manifestSrc, manifestDest);
// For Firefox, convert service_worker to scripts format
if (targetBrowser === 'firefox') {
const manifestContent = JSON.parse(readFileSync(manifestDest, 'utf-8'));
// Convert background.service_worker to background.scripts for Firefox
if (manifestContent.background?.service_worker) {
const serviceWorkerPath = manifestContent.background.service_worker;
manifestContent.background = {
scripts: [serviceWorkerPath],
};
logger.info('Converted background.service_worker to background.scripts for Firefox');
}
// Update CSP for Firefox if needed (add moz-extension: support)
if (manifestContent.content_security_policy?.extension_pages) {
let csp = manifestContent.content_security_policy.extension_pages;
if (!csp.includes('moz-extension:')) {
// Update img-src to include moz-extension:
if (csp.includes("img-src 'self'")) {
csp = csp.replace(/img-src 'self'([^;]*);/, (match: string, rest: string) => {
if (!rest.includes('moz-extension:')) {
return `img-src 'self'${rest} moz-extension:;`;
}
return match;
});
}
// Update connect-src to include moz-extension: and preserve ws://localhost for dev
if (csp.includes("connect-src 'self'")) {
csp = csp.replace(/connect-src 'self'([^;]*);/, (match: string, rest: string) => {
let updated = rest;
if (!updated.includes('moz-extension:')) {
updated += ' moz-extension:';
}
if (!updated.includes('data:')) {
updated += ' data:';
}
if (!updated.includes('blob:')) {
updated += ' blob:';
}
return `connect-src 'self'${updated};`;
});
}
manifestContent.content_security_policy.extension_pages = csp;
logger.info('Updated CSP for Firefox compatibility');
}
}
// Add extension ID for production Firefox builds
if (!isDev) {
const extensionId = process.env.FIREFOX_EXTENSION_ID;
if (extensionId) {
if (!manifestContent.browser_specific_settings) {
manifestContent.browser_specific_settings = {
gecko: {
id: extensionId,
},
};
logger.info({ extensionId }, 'Added extension ID to Firefox manifest');
}
} else {
logger.warn('FIREFOX_EXTENSION_ID environment variable not set');
}
}
writeFileSync(manifestDest, JSON.stringify(manifestContent, null, 2), 'utf-8');
}
logger.info('Manifest copied successfully');
} else {
logger.warn({ manifestSrc }, 'Manifest source not found');
}
};
const browserExtensionPlugin = (isDev: boolean = false): Plugin => ({
name: 'browser-extension-build',
writeBundle(options: { dir?: string }) {
const targetBrowser = process.env.BROWSER || 'chrome';
const outDir = options.dir || `build/${targetBrowser}`;
logger.info({ outDir }, 'Browser extension plugin: copying files');
copyBrowserExtensionFiles(targetBrowser, outDir, isDev);
},
});
// eslint-disable-next-line import/no-default-export
export default defineConfig(({ mode }) => {
const isProduction = mode === 'production';
const targetBrowser = process.env.BROWSER || 'chrome';
const plugins: PluginOption[] = [
react({
jsxRuntime: 'automatic',
}),
viteStaticCopy({
targets: [
{
src: 'node_modules/@snack-uikit/icons/dist/esm/sprite/svg/sprite.symbol.svg',
dest: '',
},
],
}),
tsconfigPaths(),
];
// Add auto-reload plugin only in dev mode
if (!isProduction) {
plugins.push(extensionReloadPlugin());
}
plugins.push(browserExtensionPlugin(!isProduction));
return {
plugins,
base: './',
define: {
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.BROWSER': JSON.stringify(targetBrowser),
},
build: {
outDir: `build/${targetBrowser}`,
emptyOutDir: false, // Do not clear the folder during build to avoid deleting background.bundle.js
sourcemap: !isProduction,
chunkSizeWarningLimit: 1000,
minify: isProduction ? 'terser' : false,
terserOptions: isProduction
? {
compress: {
drop_console: true,
drop_debugger: true,
},
}
: undefined,
rollupOptions: {
input: {
popup: resolve(__dirname, 'src/index.html'),
},
output: {
entryFileNames: chunk => {
if (chunk.name === 'popup') {
return 'popup.bundle.js';
}
return '[name].bundle.js';
},
chunkFileNames: '[name].chunk.js',
assetFileNames: assetInfo => {
if (assetInfo.names?.some(name => name.includes('index.html'))) {
return 'popup.html';
}
if (assetInfo.names?.some(name => name.endsWith('.css'))) {
return 'styles.css';
}
return '[name].[ext]';
},
manualChunks: id => {
// Create chunks for the popup bundle
if (id.includes('node_modules')) {
return 'vendor';
}
return undefined;
},
},
external: [],
},
},
server: {
port: targetBrowser === 'firefox' ? 3002 : 3001,
},
assetsInclude: ['**/*.otf', '**/*.ttf', '**/*.woff', '**/*.woff2'],
publicDir: 'src/assets',
copyPublicDir: true,
};
});