vite.config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {
  2. defineConfig,
  3. loadEnv
  4. } from 'vite';
  5. import uni from '@dcloudio/vite-plugin-uni';
  6. import path from 'path';
  7. import obfuscatorPlugin from 'vite-plugin-javascript-obfuscator';
  8. export default defineConfig(({ mode }) => {
  9. const env = loadEnv(mode, path.resolve(__dirname, 'env'), '');
  10. console.log(`运行环境:${process.env.UNI_PLATFORM}`);
  11. console.log(`baseUrl:${env.VITE_APP_API_BASE_URL}`);
  12. /** js代码混淆配置 */
  13. const obfuscatorPluginConfig = {
  14. options: {
  15. // 基础配置
  16. compact: true,
  17. controlFlowFlattening: true,
  18. controlFlowFlatteningThreshold: 0.75,
  19. deadCodeInjection: true,
  20. deadCodeInjectionThreshold: 0.4,
  21. debugProtection: true,
  22. debugProtectionInterval: 4000,
  23. disableConsoleOutput: true,
  24. // 标识符混淆
  25. identifierNamesGenerator: 'hexadecimal',
  26. log: false,
  27. numbersToExpressions: true,
  28. renameGlobals: false,
  29. selfDefending: true,
  30. // 字符串混淆
  31. simplify: true,
  32. splitStrings: true,
  33. splitStringsChunkLength: 10,
  34. // 变换配置
  35. transformObjectKeys: true,
  36. unicodeEscapeSequence: false
  37. },
  38. // 排除文件
  39. exclude: ['node_modules/**/*', 'uni_modules/**/*']
  40. };
  41. return {
  42. base: process.env.UNI_PLATFORM === 'h5' ? '/KasiteWeb/' : '/',
  43. envDir: 'env',
  44. plugins: [
  45. uni(),
  46. {
  47. name: 'workspace-uni-app-base-full-reload',
  48. apply: 'serve',
  49. handleHotUpdate({ file, server }) {
  50. const baseDir = path.resolve(__dirname, 'uni-app-base');
  51. if (file.startsWith(baseDir)) {
  52. server.ws.send({
  53. type: 'full-reload'
  54. });
  55. return [];
  56. }
  57. }
  58. },
  59. // 代码混淆插件配置(仅非开发环境且指定平台时启用)
  60. process.env.NODE_ENV != 'development' && ['mp-gongzhonghao'].includes(process.env.UNI_PLATFORM)
  61. ? obfuscatorPlugin(obfuscatorPluginConfig)
  62. : null
  63. ],
  64. resolve: {
  65. alias: {
  66. "@": path.resolve(__dirname, ""),
  67. },
  68. preserveSymlinks: true
  69. },
  70. optimizeDeps: {
  71. exclude: ['@kasite/uni-app-base']
  72. },
  73. server: {
  74. host: true,
  75. watch: {
  76. followSymlinks: true
  77. },
  78. proxy: {
  79. '/api': {
  80. target: env.VITE_APP_PROXY_API_BASE_URL,
  81. changeOrigin: true,
  82. secure: false,
  83. headers: {
  84. Origin: env.VITE_APP_PROXY_API_BASE_URL,
  85. Referer: env.VITE_APP_PROXY_API_BASE_URL
  86. },
  87. cookieDomainRewrite: '',
  88. rewrite: (path) => path.replace(/^\/api/, "")
  89. }
  90. }
  91. }
  92. };
  93. });