vite.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite";
  2. import { resolve } from "path";
  3. import { wrapperEnv } from "./build/getEnv";
  4. import { createProxy } from "./build/proxy";
  5. import { createVitePlugins } from "./build/plugins";
  6. import pkg from "./package.json";
  7. import dayjs from "dayjs";
  8. const { dependencies, devDependencies, name, version } = pkg;
  9. const __APP_INFO__ = {
  10. pkg: { dependencies, devDependencies, name, version },
  11. lastBuildTime: dayjs().format("YYYY-MM-DD HH:mm:ss")
  12. };
  13. // @see: https://vitejs.dev/config/
  14. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  15. const root = process.cwd();
  16. const env = loadEnv(mode, root);
  17. const viteEnv = wrapperEnv(env);
  18. console.log(createProxy(viteEnv.VITE_PROXY));
  19. return {
  20. base: viteEnv.VITE_PUBLIC_PATH,
  21. root,
  22. resolve: {
  23. alias: {
  24. "@": resolve(__dirname, "./src"),
  25. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  26. }
  27. },
  28. define: {
  29. __APP_INFO__: JSON.stringify(__APP_INFO__)
  30. },
  31. css: {
  32. preprocessorOptions: {
  33. scss: {
  34. additionalData: `@import "@/styles/var.scss";`
  35. }
  36. }
  37. },
  38. server: {
  39. host: "0.0.0.0",
  40. port: viteEnv.VITE_PORT,
  41. open: viteEnv.VITE_OPEN,
  42. cors: true,
  43. // Load proxy configuration from .env.development
  44. proxy: createProxy(viteEnv.VITE_PROXY)
  45. },
  46. plugins: createVitePlugins(viteEnv),
  47. esbuild: {
  48. pure: viteEnv.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
  49. },
  50. build: {
  51. outDir: "dist",
  52. minify: "esbuild",
  53. // esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
  54. // minify: "terser",
  55. // terserOptions: {
  56. // compress: {
  57. // drop_console: viteEnv.VITE_DROP_CONSOLE,
  58. // drop_debugger: true
  59. // }
  60. // },
  61. sourcemap: false,
  62. // 禁用 gzip 压缩大小报告,可略微减少打包时间
  63. reportCompressedSize: false,
  64. // 规定触发警告的 chunk 大小
  65. chunkSizeWarningLimit: 2000,
  66. rollupOptions: {
  67. output: {
  68. // Static resource classification and packaging
  69. chunkFileNames: "assets/js/[name]-[hash].js",
  70. entryFileNames: "assets/js/[name]-[hash].js",
  71. assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
  72. }
  73. }
  74. }
  75. };
  76. });