vite.config.ts 2.4 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 tailwindcss from "@tailwindcss/vite";
  7. import pkg from "./package.json";
  8. import dayjs from "dayjs";
  9. const { dependencies, devDependencies, name, version } = pkg;
  10. const __APP_INFO__ = {
  11. pkg: { dependencies, devDependencies, name, version },
  12. lastBuildTime: dayjs().format("YYYY-MM-DD HH:mm:ss")
  13. };
  14. // @see: https://vitejs.dev/config/
  15. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  16. const root = process.cwd();
  17. const env = loadEnv(mode, root);
  18. const viteEnv = wrapperEnv(env);
  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. extensions: [".mjs", ".js", ".mts", ".ts", ".jsx", ".tsx", ".json", ".vue"]
  28. },
  29. define: {
  30. __APP_INFO__: JSON.stringify(__APP_INFO__)
  31. },
  32. css: {
  33. preprocessorOptions: {
  34. scss: {
  35. additionalData: `@import "@/styles/var.scss";`
  36. }
  37. }
  38. },
  39. server: {
  40. host: "0.0.0.0",
  41. port: viteEnv.VITE_PORT,
  42. open: viteEnv.VITE_OPEN,
  43. cors: true,
  44. proxy: createProxy(viteEnv.VITE_PROXY)
  45. },
  46. plugins: [tailwindcss(), 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. });