-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathvite.config.ts
More file actions
97 lines (86 loc) · 2.54 KB
/
vite.config.ts
File metadata and controls
97 lines (86 loc) · 2.54 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
import { defineConfig } from 'vite'
import { builtinModules } from 'module'
import cp from 'vite-plugin-cp'
import { version } from './src/version'
import path from 'node:path'
import fs from 'node:fs'
function writeVersion() {
const pkgJsonPath = './package-dist.json'
const pkgJsonRaw = fs.readFileSync(pkgJsonPath, 'utf8')
const packageJson = JSON.parse(pkgJsonRaw)
packageJson.version = version
fs.writeFileSync(pkgJsonPath, JSON.stringify(packageJson), 'utf8')
}
writeVersion()
function getModuleDependencies(moduleName: string, basePath = path.join(__dirname, 'node_modules'), seen = new Set<string>()) {
if (seen.has(moduleName)) {
return []
}
seen.add(moduleName)
const pkgPath = path.join(basePath, moduleName, 'package.json')
let pkg
try {
const content = fs.readFileSync(pkgPath, 'utf-8')
pkg = JSON.parse(content)
} catch (err) {
// 找不到 package.json 或 JSON 解析失败时,跳过
return []
}
const deps = Object.keys(pkg.dependencies || {})
for (const dep of deps) {
getModuleDependencies(dep, basePath, seen)
}
const result = Array.from(seen)
return result
}
const external = [
'ws',
'silk-wasm',
'@minatojs/sql.js',
'has-flag',
...getModuleDependencies('reggol'),
...getModuleDependencies('file-type'),
]
// console.log(external)
function genCpModule(module: string | RegExp) {
return { src: `./node_modules/${module}`, dest: `dist/node_modules/${module}`, flatten: false }
}
export default defineConfig({
define: {
__IS_BROWSER__: false, // 确保在 Node.js 环境中运行
'process.env': 'process.env', // 防止 Vite 替换 process.env
// 'import.meta.env.MODE': '"production"'
},
build: {
sourcemap: true,
minify: false,
outDir: 'dist',
target: 'node22',
rolldownOptions: {
platform: 'node',
external: [...external, ...builtinModules, /^node:/],
input: 'src/main/main.ts',
output: {
entryFileNames: 'llbot.js',
format: 'es',
},
plugins: [
cp({
targets: [
...external.map(genCpModule),
{ src: './src/common/default_config.json', dest: 'dist/' },
{ src: './package-dist.json', dest: 'dist/', rename: 'package.json' },
{ src: './doc/使用说明.txt', dest: 'dist/' },
{ src: './doc/更新日志.txt', dest: 'dist/' },
],
}),
],
},
},
resolve: {
alias: {
'qrcode': path.resolve(import.meta.dirname, 'node_modules/qrcode/lib/server.js'),
},
tsconfigPaths: true
},
})