forked from NapNeko/napcat-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
154 lines (142 loc) · 5.66 KB
/
vite.config.ts
File metadata and controls
154 lines (142 loc) · 5.66 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
import { resolve, dirname } from 'path';
import { defineConfig } from 'vite';
import nodeResolve from '@rollup/plugin-node-resolve';
import { builtinModules } from 'module';
import { fileURLToPath } from 'url';
import fs from 'fs';
import { execSync } from 'child_process';
import { napcatHmrPlugin } from 'napcat-plugin-debug-cli/vite';
const __dirname = dirname(fileURLToPath(import.meta.url));
const nodeModules = [
...builtinModules,
...builtinModules.map((m) => `node:${m}`),
].flat();
// 依赖排除(如有外部依赖需排除,在此添加)
const external: string[] = [];
/**
* 递归复制目录
*/
function copyDirRecursive(src: string, dest: string) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = resolve(src, entry.name);
const destPath = resolve(dest, entry.name);
if (entry.isDirectory()) {
copyDirRecursive(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
/**
* 构建后自动复制资源的 Vite 插件
* - 复制 webui 构建产物到 dist/webui
* - 生成精简的 package.json(只保留运行时必要字段)
* - 复制 templates 目录(如果存在)
*/
function copyAssetsPlugin() {
return {
name: 'copy-assets',
writeBundle() {
try {
const distDir = resolve(__dirname, 'dist');
// 1. 构建 WebUI 前端
const webuiRoot = resolve(__dirname, 'src/webui');
try {
// 先确保 webui 子项目的依赖已安装
if (!fs.existsSync(resolve(webuiRoot, 'node_modules'))) {
console.log('[copy-assets] (o\'v\'o) 正在安装 WebUI 依赖...');
execSync('pnpm install', {
cwd: webuiRoot,
stdio: 'pipe',
});
console.log('[copy-assets] (o\'v\'o) WebUI 依赖安装完成');
}
console.log('[copy-assets] (o\'v\'o) 正在构建 WebUI...');
const webuiEnv = { ...process.env };
delete webuiEnv.NODE_ENV;
execSync('pnpm run build', {
cwd: webuiRoot,
stdio: 'pipe',
env: webuiEnv,
});
console.log('[copy-assets] (o\'v\'o) WebUI 构建完成');
} catch (e: any) {
console.error('[copy-assets] (;_;) WebUI 构建失败:', e.stdout?.toString().slice(-300) || e.message);
}
// 2. 复制 webui 构建产物
const webuiDist = resolve(__dirname, 'src/webui/dist');
const webuiDest = resolve(distDir, 'webui');
if (fs.existsSync(webuiDist)) {
copyDirRecursive(webuiDist, webuiDest);
console.log('[copy-assets] (o\'v\'o) 已复制 webui 构建产物');
} else {
console.error('[copy-assets] (;_;) webui 构建产物不存在,请先运行 pnpm run build:webui');
}
// 3. 生成精简的 package.json(只保留运行时必要字段)
const pkgPath = resolve(__dirname, 'package.json');
if (fs.existsSync(pkgPath)) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
const distPkg: Record<string, unknown> = {
name: pkg.name,
plugin: pkg.plugin,
version: pkg.version,
type: pkg.type,
main: pkg.main,
description: pkg.description,
author: pkg.author,
dependencies: pkg.dependencies,
};
if (pkg.napcat) {
distPkg.napcat = pkg.napcat;
}
fs.writeFileSync(
resolve(distDir, 'package.json'),
JSON.stringify(distPkg, null, 2)
);
console.log('[copy-assets] (o\'v\'o) 已生成精简 package.json');
}
// 4. 复制 templates 目录(如果存在)
const templatesSrc = resolve(__dirname, 'templates');
if (fs.existsSync(templatesSrc)) {
copyDirRecursive(templatesSrc, resolve(distDir, 'templates'));
console.log('[copy-assets] (o\'v\'o) 已复制 templates 目录');
}
console.log('[copy-assets] (*\'v\'*) 资源复制完成!');
} catch (error) {
console.error('[copy-assets] (;_;) 资源复制失败:', error);
}
},
};
}
export default defineConfig({
resolve: {
conditions: ['node', 'default'],
},
build: {
sourcemap: false,
target: 'esnext',
minify: false,
lib: {
entry: resolve(__dirname, 'src/index.ts'),
formats: ['es'],
fileName: () => 'index.mjs',
},
rollupOptions: {
external: [...nodeModules, ...external],
output: {
inlineDynamicImports: true,
},
},
outDir: 'dist',
},
plugins: [nodeResolve(), copyAssetsPlugin(), napcatHmrPlugin({
webui: {
distDir: './src/webui/dist',
targetDir: 'webui',
},
})],
});