vite.config.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite";
  2. import { resolve } from "path";
  3. import { wrapperEnv } from "./viteConfig/getEnv";
  4. import { createProxy } from "./viteConfig/proxy";
  5. import { createVitePlugins } from "./viteConfig/plugins";
  6. import pkg from "./package.json";
  7. import dayjs from "dayjs";
  8. // import postCssPxToRem from "postcss-pxtorem"
  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. },
  28. define: {
  29. __APP_INFO__: JSON.stringify(__APP_INFO__)
  30. },
  31. css: {
  32. preprocessorOptions: {
  33. scss: {
  34. additionalData: `@import "@/styles/var/index.scss";@import "@/styles/mixins.scss";`
  35. }
  36. }
  37. // postcss: {
  38. // plugins: [
  39. // postCssPxToRem({
  40. // rootValue: 16, // 1rem的大小
  41. // propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
  42. // })
  43. // ]
  44. // }
  45. },
  46. server: {
  47. host: "0.0.0.0",
  48. port: viteEnv.VITE_PORT,
  49. open: viteEnv.VITE_OPEN,
  50. cors: true,
  51. // Load proxy configuration from .env.development
  52. proxy: createProxy(viteEnv.VITE_PROXY),
  53. hmr: true
  54. },
  55. plugins: createVitePlugins(viteEnv),
  56. esbuild: {
  57. pure: viteEnv.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
  58. },
  59. build: {
  60. outDir: "dist",
  61. minify: "esbuild",
  62. // esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
  63. // minify: "terser",
  64. // terserOptions: {
  65. // compress: {
  66. // drop_console: viteEnv.VITE_DROP_CONSOLE,
  67. // drop_debugger: true
  68. // }
  69. // },
  70. // 禁用 gzip 压缩大小报告,可略微减少打包时间
  71. reportCompressedSize: false,
  72. // 规定触发警告的 chunk 大小
  73. chunkSizeWarningLimit: 2000,
  74. rollupOptions: {
  75. output: {
  76. // Static resource classification and packaging
  77. chunkFileNames: "assets/js/[name]-[hash].js",
  78. entryFileNames: "assets/js/[name]-[hash].js",
  79. assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
  80. }
  81. }
  82. }
  83. };
  84. });