vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {defineConfig, loadEnv, ConfigEnv, UserConfig} from "vite";
  2. import {resolve} from "path";
  3. import {wrapperEnv} from "./build/getEnv";
  4. import {createVitePlugins} from "./build/plugins";
  5. import pkg from "./package.json";
  6. import dayjs from "dayjs";
  7. import javascriptObfuscator from "vite-plugin-javascript-obfuscator";
  8. // 扩展代理配置类型
  9. interface CustomProxyOptions {
  10. target: string
  11. changeOrigin?: boolean
  12. secure?: boolean
  13. pathRewrite?: Record<string, string>
  14. }
  15. const {dependencies, devDependencies, name, version} = pkg;
  16. const __APP_INFO__ = {
  17. pkg: {dependencies, devDependencies, name, version},
  18. lastBuildTime: dayjs().format("YYYY-MM-DD HH:mm:ss")
  19. };
  20. const timeStamp = new Date().getTime();
  21. // @see: https://vitejs.dev/config/
  22. export default defineConfig(({mode}: ConfigEnv): UserConfig => {
  23. const root = process.cwd();
  24. const env = loadEnv(mode, root);
  25. const viteEnv = wrapperEnv(env);
  26. return {
  27. base: "/",// viteEnv.VITE_PUBLIC_PATH,
  28. root,
  29. resolve: {
  30. alias: {
  31. "@": resolve(__dirname, "./src"),
  32. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  33. }
  34. },
  35. define: {
  36. __APP_INFO__: JSON.stringify(__APP_INFO__)
  37. },
  38. css: {
  39. preprocessorOptions: {
  40. scss: {
  41. additionalData: `@use "@/styles/var.scss";`,
  42. quietDeps: true // 忽略依赖警告
  43. }
  44. }
  45. },
  46. // 应用代理配置
  47. server: {
  48. port: 8080,
  49. host: '0.0.0.0',
  50. proxy: {
  51. "/admin": {
  52. target: "http://127.0.0.1:2022",
  53. changeOrigin: true,
  54. secure: false
  55. } as CustomProxyOptions // 类型断言
  56. }
  57. },
  58. // plugins: createVitePlugins(viteEnv),
  59. plugins: [
  60. ...createVitePlugins(viteEnv),
  61. javascriptObfuscator({
  62. include: ['src/api/**/*.js', 'src/api/**/*.ts'],
  63. options: {
  64. compact: true,
  65. controlFlowFlattening: true,
  66. deadCodeInjection: false,
  67. rotateStringArray: true,
  68. stringArray: true,
  69. stringArrayEncoding: ['rc4'], // rc4 或 'base64'
  70. stringArrayThreshold: 1
  71. }
  72. })
  73. ],
  74. esbuild: {
  75. // pure: viteEnv.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
  76. },
  77. build: {
  78. outDir: "dist",
  79. minify: "esbuild",
  80. // esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
  81. // minify: "terser",
  82. // terserOptions: {
  83. // compress: {
  84. // drop_console: viteEnv.VITE_DROP_CONSOLE,
  85. // drop_debugger: true
  86. // }
  87. // },
  88. sourcemap: false,
  89. // 禁用 gzip 压缩大小报告,可略微减少打包时间
  90. reportCompressedSize: false,
  91. // 规定触发警告的 chunk 大小
  92. chunkSizeWarningLimit: 5000,
  93. rollupOptions: {
  94. output: {
  95. // Static resource classification and packaging
  96. chunkFileNames: `assets/js/[name]-[hash].${timeStamp}.js`,
  97. entryFileNames: `assets/js/[name]-[hash].${timeStamp}.js`,
  98. assetFileNames: `assets/[ext]/[name]-[hash].${timeStamp}.[ext]`
  99. }
  100. }
  101. }
  102. };
  103. });