vite.config.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { getPluginsList } from "./build/plugins";
  2. import { include, exclude } from "./build/optimize";
  3. import { type UserConfigExport, type ConfigEnv, loadEnv } from "vite";
  4. import {
  5. root,
  6. alias,
  7. warpperEnv,
  8. pathResolve,
  9. __APP_INFO__
  10. } from "./build/utils";
  11. export default ({ mode }: ConfigEnv): UserConfigExport => {
  12. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  13. warpperEnv(loadEnv(mode, root));
  14. return {
  15. base: VITE_PUBLIC_PATH,
  16. root,
  17. resolve: {
  18. alias
  19. },
  20. // 服务端渲染
  21. server: {
  22. // 端口号
  23. port: VITE_PORT,
  24. host: "0.0.0.0",
  25. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  26. proxy: {
  27. "/api": {
  28. // 这里填写后端地址
  29. target: "http://116.148.231.158:9087",
  30. changeOrigin: true,
  31. rewrite: path => path.replace(/^\/api/, "")
  32. }
  33. },
  34. // 预热文件以提前转换和缓存结果,降低启动期间的初始页面加载时长并防止转换瀑布
  35. warmup: {
  36. clientFiles: ["./index.html", "./src/{views,components}/*"]
  37. }
  38. },
  39. plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
  40. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  41. optimizeDeps: {
  42. include,
  43. exclude
  44. },
  45. build: {
  46. // https://cn.vitejs.dev/guide/build.html#browser-compatibility
  47. target: "es2015",
  48. sourcemap: false,
  49. // 消除打包大小超过500kb警告
  50. chunkSizeWarningLimit: 4000,
  51. rollupOptions: {
  52. input: {
  53. index: pathResolve("./index.html", import.meta.url)
  54. },
  55. // 静态资源分类打包
  56. output: {
  57. chunkFileNames: "static/js/[name]-[hash].js",
  58. entryFileNames: "static/js/[name]-[hash].js",
  59. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  60. }
  61. }
  62. },
  63. define: {
  64. __INTLIFY_PROD_DEVTOOLS__: false,
  65. __APP_INFO__: JSON.stringify(__APP_INFO__)
  66. }
  67. };
  68. };