vite.config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. envDir: 'env',
  43. plugins: [
  44. uni(),
  45. {
  46. name: 'workspace-uni-app-base-full-reload',
  47. apply: 'serve',
  48. handleHotUpdate({ file, server }) {
  49. const baseDir = path.resolve(__dirname, 'uni-app-base');
  50. if (file.startsWith(baseDir)) {
  51. server.ws.send({
  52. type: 'full-reload'
  53. });
  54. return [];
  55. }
  56. }
  57. },
  58. // 代码混淆插件配置(仅非开发环境且指定平台时启用)
  59. process.env.NODE_ENV != 'development' && ['mp-gongzhonghao'].includes(process.env.UNI_PLATFORM)
  60. ? obfuscatorPlugin(obfuscatorPluginConfig)
  61. : null
  62. ],
  63. resolve: {
  64. alias: {
  65. "@": path.resolve(__dirname, ""),
  66. },
  67. preserveSymlinks: true
  68. },
  69. optimizeDeps: {
  70. exclude: ['@kasite/uni-app-base']
  71. },
  72. server: {
  73. host: true,
  74. watch: {
  75. followSymlinks: true
  76. },
  77. proxy: {
  78. '/api': {
  79. target: env.VITE_APP_PROXY_API_BASE_URL,
  80. changeOrigin: true,
  81. secure: false,
  82. headers: {
  83. Origin: env.VITE_APP_PROXY_API_BASE_URL,
  84. Referer: env.VITE_APP_PROXY_API_BASE_URL
  85. },
  86. cookieDomainRewrite: '',
  87. rewrite: (path) => path.replace(/^\/api/, "")
  88. }
  89. }
  90. }
  91. };
  92. });