NapCatQQ/vite.config.ts

128 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-04-14 16:09:08 +00:00
// import PreprocessorDirectives from 'unplugin-preprocessor-directives/vite';
import obfuscator from 'rollup-plugin-obfuscator';
import cp from 'vite-plugin-cp';
import { UserConfig, defineConfig } from 'vite';
import { resolve } from 'path';
import { PluginOption, Plugin } from 'vite';
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { builtinModules } from 'module';
import os from 'node:os';
import fs from 'node:fs';
const external = ['silk-wasm', 'ws', 'express', 'uuid', 'fluent-ffmpeg', 'sqlite3', 'log4js',
2024-04-25 10:15:03 +00:00
'qrcode-terminal', 'MoeHoo'];
2024-04-14 16:09:08 +00:00
const nodeModules = [...builtinModules, builtinModules.map(m => `node:${m}`)].flat();
// let nodeModules = ["fs", "path", "events", "buffer", "url", "crypto", "fs/promise", "fsPromise", "os", "http", "net"]
// nodeModules = [...nodeModules, ...nodeModules.map(m => `node:${m}`)]
function genCpModule(module: string) {
2024-04-23 10:38:13 +00:00
return { src: `./node_modules/${module}`, dest: `dist/node_modules/${module}`, flatten: false };
2024-04-14 16:09:08 +00:00
}
2024-05-03 13:00:17 +00:00
const systemPlatform = os.platform();
let startScripts: string[] | undefined = undefined;
if (systemPlatform == "linux") {
startScripts = ['./script/napcat.sh'];
} else {
startScripts = ['./script/napcat.ps1', './script/napcat.bat', './script/napcat-utf8.bat', './script/napcat-utf8.ps1', './script/napcat-log.ps1'];
}
2024-04-14 16:09:08 +00:00
// if (os.platform() !== 'win32') {
// startScripts = ['./script/napcat.sh'];
// }
const baseConfigPlugin: PluginOption[] = [
// PreprocessorDirectives(),
cp({
targets: [
// ...external.map(genCpModule),
2024-04-27 11:06:39 +00:00
{ src: './src/napcat.json', dest: 'dist/config/' },
2024-04-23 10:38:13 +00:00
{ src: './src/onebot11/onebot11.json', dest: 'dist/config/' },
{ src: './package.json', dest: 'dist' },
{ src: './README.md', dest: 'dist' },
2024-04-28 10:22:17 +00:00
{ src: './logo.png', dest: 'dist/logs' },
2024-04-27 13:06:58 +00:00
{ src: './src/core.lib/MoeHoo-win32-x64.node', dest: 'dist' },
{ src: './src/core.lib/MoeHoo-linux-x64.node', dest: 'dist' },
2024-04-14 16:09:08 +00:00
...(startScripts.map((startScript) => {
2024-04-23 10:38:13 +00:00
return { src: startScript, dest: 'dist' };
2024-04-14 16:09:08 +00:00
})),
]
}),
nodeResolve(),
commonjs(),
];
let corePath = resolve(__dirname, './src/core/src');
if (!fs.existsSync(corePath)) {
corePath = resolve(__dirname, './src/core.lib/src');
}
const baseConfig = (mode: string = 'development') => defineConfig({
resolve: {
conditions: ['node', 'default'],
alias: {
'@/core': corePath,
'@': resolve(__dirname, './src'),
'./lib-cov/fluent-ffmpeg': './lib/fluent-ffmpeg',
},
},
build: {
target: 'esnext',
2024-04-28 13:02:56 +00:00
// minify: mode === 'production' ? 'esbuild' : false,
2024-04-28 13:05:30 +00:00
// 压缩代码出现了未知问题导致无法运行,暂时不启用
2024-04-28 13:02:56 +00:00
minify: false,
2024-04-14 16:09:08 +00:00
lib: {
2024-04-26 16:50:08 +00:00
entry: 'src/index.ts',
2024-04-14 16:09:08 +00:00
formats: ['cjs'],
fileName: () => 'napcat.cjs',
},
rollupOptions: {
// external: [ /node:*/ ],
external: [...nodeModules, ...external]
},
},
});
2024-04-23 10:38:13 +00:00
export default defineConfig(({ mode }): UserConfig => {
2024-04-14 16:09:08 +00:00
if (mode === 'production') {
return {
...baseConfig(mode),
plugins: [
...baseConfigPlugin,
{
...(obfuscator({
options: {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
deadCodeInjection: true,
deadCodeInjectionThreshold: 0.4,
debugProtection: false,
disableConsoleOutput: false,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: ['base64'],
stringArrayThreshold: 0.75,
transformObjectKeys: true,
unicodeEscapeSequence: false
},
include: ['src/**/*.js', 'src/**/*.ts'],
}) as Plugin),
enforce: 'post',
apply: 'build',
},
]
};
} else {
return {
...baseConfig(mode),
plugins: baseConfigPlugin,
};
}
});