vite.config.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. return {
  19. base: viteEnv.VITE_PUBLIC_PATH,
  20. root,
  21. resolve: {
  22. alias: {
  23. "@": resolve(__dirname, "./src"),
  24. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  25. }
  26. },
  27. define: {
  28. __APP_INFO__: JSON.stringify(__APP_INFO__)
  29. },
  30. css: {
  31. preprocessorOptions: {
  32. scss: {
  33. additionalData: `@import "@/styles/var.scss";`
  34. }
  35. }
  36. },
  37. plugins: createVitePlugins(viteEnv),
  38. esbuild: {
  39. pure: viteEnv.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
  40. },
  41. build: {
  42. outDir: "dist",
  43. minify: "esbuild",
  44. // esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
  45. // minify: "terser",
  46. // terserOptions: {
  47. // compress: {
  48. // drop_console: viteEnv.VITE_DROP_CONSOLE,
  49. // drop_debugger: true
  50. // }
  51. // },
  52. sourcemap: false,
  53. // 禁用 gzip 压缩大小报告,可略微减少打包时间
  54. reportCompressedSize: false,
  55. // 规定触发警告的 chunk 大小
  56. chunkSizeWarningLimit: 2000,
  57. rollupOptions: {
  58. output: {
  59. // Static resource classification and packaging
  60. chunkFileNames: "assets/js/[name]-[hash].js",
  61. entryFileNames: "assets/js/[name]-[hash].js",
  62. assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
  63. }
  64. }
  65. }
  66. };
  67. });