-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
230 lines (223 loc) · 8.45 KB
/
vite.config.ts
File metadata and controls
230 lines (223 loc) · 8.45 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
/**
* Copyright (c) 2025-2099 GitCoffee All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve, dirname } from 'path'
import { copyFileSync, mkdirSync, existsSync, writeFileSync } from 'fs'
import { globSync } from 'glob'
import { execSync } from 'child_process'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const isFastBuild = mode === 'fast'
return {
plugins: [
vue(),
// 构建完成后复制插件目录
{
name: 'copy-plugins',
writeBundle() {
// 只在生产构建时复制插件
if (process.env.NODE_ENV === 'production') {
const pluginFiles = globSync('plugins/**/*.js')
const distPluginDir = resolve(__dirname, 'dist/plugins')
// 创建目标目录
if (!existsSync(distPluginDir)) {
mkdirSync(distPluginDir, { recursive: true })
}
// 复制所有插件文件
pluginFiles.forEach(file => {
const dest = resolve(distPluginDir, file.replace('plugins/', ''))
// 确保目标目录存在
const destDir = dirname(dest)
if (!existsSync(destDir)) {
mkdirSync(destDir, { recursive: true })
}
copyFileSync(file, dest)
})
}
}
},
// 开发模式下的编译文件输出插件
// {
// name: 'dev-build-output',
// buildStart() {
// console.log('🚀 Starting dev-build-output plugin')
// },
// transform(code, id) {
// // 只在开发模式下启用
// if (process.env.NODE_ENV === 'development') {
// console.log(`🔄 Processing file: ${id}`)
// const devOutputDir = resolve(__dirname, 'dist')
//
// // 确保开发输出目录存在
// if (!existsSync(devOutputDir)) {
// console.log(`📁 Creating dev output directory: ${devOutputDir}`)
// mkdirSync(devOutputDir, { recursive: true })
// }
//
// // 处理背景脚本
// if (id.includes('background')) {
// const outputPath = resolve(devOutputDir, 'assets', 'background-dev.js')
// mkdirSync(dirname(outputPath), { recursive: true })
// writeFileSync(outputPath, code)
// console.log(`✅ Written background script to: ${outputPath}`)
// }
//
// // 处理内容脚本
// if (id.includes('content')) {
// const outputPath = resolve(devOutputDir, 'assets', 'content-dev.js')
// mkdirSync(dirname(outputPath), { recursive: true })
// writeFileSync(outputPath, code)
// console.log(`✅ Written content script to: ${outputPath}`)
// }
//
// // 处理侧边栏脚本
// if (id.includes('sidepanel')) {
// const outputPath = resolve(devOutputDir, 'assets', 'sidepanel-dev.js')
// mkdirSync(dirname(outputPath), { recursive: true })
// writeFileSync(outputPath, code)
// console.log(`✅ Written sidepanel script to: ${outputPath}`)
// }
//
// // 处理主脚本 main.tsx
// if (id.includes('main.tsx')) {
// const outputPath = resolve(devOutputDir, 'assets', 'main-dev.js')
// mkdirSync(dirname(outputPath), { recursive: true })
// writeFileSync(outputPath, code)
// console.log(`✅ Written main script to: ${outputPath}`)
// }
// }
// return code
// }
// },
// 开发模式下的热更新插件
{
name: 'vite-to-plasmo-hot-reload',
configureServer(server) {
// 只在开发模式下启用
if (process.env.NODE_ENV === 'development') {
// 监听文件变化
server.watcher.on('change', (file) => {
// 忽略 node_modules、dist、build 和 .plasmo 目录
if (file.includes('node_modules') || file.includes('dist') || file.includes('build') || file.includes('.plasmo')) {
return
}
// 只处理 TypeScript、JavaScript、Vue、HTML 文件
if (/(ts|js|vue|html)$/.test(file)) {
console.log(`\n🔄 File changed: ${file}`)
try {
// 执行 vite-to-plasmo.js 脚本
execSync('node scripts/vite-to-plasmo.js --dev', {
stdio: 'inherit',
cwd: resolve(__dirname)
})
console.log('✅ Plasmo hot reload triggered')
} catch (error) {
console.error('❌ Failed to trigger plasmo hot reload:', error.message)
}
}
})
// 监听 workspace 中其他包的文件变化
const workspacePackages = ['packages/ui/search-ui', 'packages/core/i18n']
workspacePackages.forEach(packagePath => {
const fullPath = resolve(__dirname, '..', '..', packagePath)
server.watcher.add(fullPath)
console.log(`✅ Added watcher for workspace package: ${packagePath}`)
})
}
}
}
],
resolve: {
alias: {
'~': resolve(__dirname, 'src'),
'~/*': resolve(__dirname, 'src/*'),
'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js',
}
},
build: {
outDir: 'dist',
emptyOutDir: true,
// 启用多线程构建
minify: isFastBuild ? false : 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true
}
},
// 启用 gzip 压缩
reportCompressedSize: true,
// 配置缓存
cacheDir: './node_modules/.vite-cache',
// 快速构建模式下禁用源映射
sourcemap: !isFastBuild,
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
options: resolve(__dirname, 'src/options/index.html'),
sidepanel: resolve(__dirname, 'src/sidepanel/index.vue'),
background: resolve(__dirname, 'src/background/index.ts'),
content: resolve(__dirname, 'src/content/index.ts')
},
output: {
assetFileNames: 'assets/[name]-[hash][extname]',
chunkFileNames: 'assets/[name]-[hash].js',
entryFileNames: (chunkInfo) => {
// 为 background 脚本使用 CommonJS 格式
if (chunkInfo.name === 'background') {
return 'assets/[name]-[hash].cjs'
}
return 'assets/[name]-[hash].js'
},
format: 'es', // 使用ES模块格式
inlineDynamicImports: false,
// 配置代码分割
manualChunks: isFastBuild ? undefined : {
// 将第三方依赖打包到单独的 chunk
vendor: ['vue', 'vue-router', 'ant-design-vue'],
// 将国际化相关代码打包到单独的 chunk
i18n: ['vue-i18n'],
// 将 UI 组件打包到单独的 chunk
ui: ['@gitcoffee/search-ui', '@gitcoffee/chatbot-ui']
}
}
},
assetsDir: 'assets',
base: './'
},
server: {
port: 5173,
open: false,
// 开发服务器优化
fs: {
strict: false
},
watch: {
ignored: ['**/node_modules/**', '**/dist/**']
},
proxy: {
// 配置 code.exmay.com 搜索 API 的代理
'/api/search': {
target: 'https://code.exmay.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api\/search/, '/exmay/api/ugc/article/search'),
secure: false
}
}
}
}
})