import { defineConfig, loadEnv } from 'vite'; import uni from '@dcloudio/vite-plugin-uni'; import path from 'path'; import obfuscatorPlugin from 'vite-plugin-javascript-obfuscator'; export default defineConfig(({ mode }) => { const env = loadEnv(mode, path.resolve(__dirname, 'env'), ''); console.log(`运行环境:${process.env.UNI_PLATFORM}`); console.log(`baseUrl:${env.VITE_APP_API_BASE_URL}`); /** js代码混淆配置 */ const obfuscatorPluginConfig = { options: { // 基础配置 compact: true, controlFlowFlattening: true, controlFlowFlatteningThreshold: 0.75, deadCodeInjection: true, deadCodeInjectionThreshold: 0.4, debugProtection: true, debugProtectionInterval: 4000, disableConsoleOutput: true, // 标识符混淆 identifierNamesGenerator: 'hexadecimal', log: false, numbersToExpressions: true, renameGlobals: false, selfDefending: true, // 字符串混淆 simplify: true, splitStrings: true, splitStringsChunkLength: 10, // 变换配置 transformObjectKeys: true, unicodeEscapeSequence: false }, // 排除文件 exclude: ['node_modules/**/*', 'uni_modules/**/*'] }; return { base: process.env.UNI_PLATFORM === 'h5' ? '/KasiteWeb/' : '/', envDir: 'env', plugins: [ uni(), { name: 'workspace-uni-app-base-full-reload', apply: 'serve', handleHotUpdate({ file, server }) { const baseDir = path.resolve(__dirname, 'uni-app-base'); if (file.startsWith(baseDir)) { server.ws.send({ type: 'full-reload' }); return []; } } }, // 代码混淆插件配置(仅非开发环境且指定平台时启用) process.env.NODE_ENV != 'development' && ['mp-gongzhonghao'].includes(process.env.UNI_PLATFORM) ? obfuscatorPlugin(obfuscatorPluginConfig) : null ], resolve: { alias: { "@": path.resolve(__dirname, ""), }, preserveSymlinks: true }, optimizeDeps: { exclude: ['@kasite/uni-app-base'] }, server: { host: true, watch: { followSymlinks: true }, proxy: { '/api': { target: env.VITE_APP_PROXY_API_BASE_URL, changeOrigin: true, secure: false, headers: { Origin: env.VITE_APP_PROXY_API_BASE_URL, Referer: env.VITE_APP_PROXY_API_BASE_URL }, cookieDomainRewrite: '', rewrite: (path) => path.replace(/^\/api/, "") } } } }; });