-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
78 lines (75 loc) · 2.38 KB
/
vite.config.ts
File metadata and controls
78 lines (75 loc) · 2.38 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
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';
import { copyFileSync, mkdirSync, existsSync, rmSync, readFileSync, writeFileSync } from 'fs';
export default defineConfig({
plugins: [
react(),
{
name: 'copy-manifest',
closeBundle() {
// 确保dist目录存在
if (!existsSync('dist')) {
mkdirSync('dist', { recursive: true });
}
// 复制manifest.json到dist目录
copyFileSync('public/manifest.json', 'dist/manifest.json');
// 复制icons目录
if (!existsSync('dist/icons')) {
mkdirSync('dist/icons', { recursive: true });
}
copyFileSync('public/icons/icon.svg', 'dist/icons/icon.svg');
// 移动HTML文件到根目录并修复路径
if (existsSync('dist/src/popup/index.html')) {
let html = readFileSync('dist/src/popup/index.html', 'utf-8');
// 修复相对路径
html = html.replace(/\.\.\/\.\.\//g, './');
writeFileSync('dist/popup.html', html);
// 清理空目录
try {
rmSync('dist/src', { recursive: true, force: true });
} catch (e) {
// 忽略错误
}
}
},
},
],
base: './',
build: {
outDir: 'dist',
emptyOutDir: true,
rollupOptions: {
input: {
popup: resolve(__dirname, 'src/popup/index.html'),
background: resolve(__dirname, 'src/background/service-worker.ts'),
content: resolve(__dirname, 'src/content/content.ts'),
},
output: {
entryFileNames: (chunkInfo) => {
const facadeModuleId = chunkInfo.facadeModuleId || '';
if (facadeModuleId.includes('service-worker')) {
return 'background.js';
}
if (facadeModuleId.includes('content.ts')) {
return 'content.js';
}
return '[name].js';
},
chunkFileNames: (chunkInfo) => {
return '[name].js';
},
assetFileNames: (assetInfo) => {
if (assetInfo.name === 'index.html') {
return 'popup.html';
}
return '[name].[ext]';
},
// 对于content和background,使用IIFE格式
format: 'es',
},
// 每个入口独立打包,不共享代码
preserveEntrySignatures: 'strict',
},
},
});