vite.config.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. wrapperEnv,
  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. wrapperEnv(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://192.168.1.168:20000",
  30. changeOrigin: true,
  31. rewrite: path => {
  32. console.log(path);
  33. return path.replace(/^\/api/, "");
  34. }
  35. }
  36. },
  37. // 预热文件以提前转换和缓存结果,降低启动期间的初始页面加载时长并防止转换瀑布
  38. warmup: {
  39. clientFiles: ["./index.html", "./src/{views,components}/*"]
  40. }
  41. },
  42. plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
  43. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  44. optimizeDeps: {
  45. include,
  46. exclude
  47. },
  48. build: {
  49. // https://cn.vitejs.dev/guide/build.html#browser-compatibility
  50. target: "es2015",
  51. sourcemap: false,
  52. // 消除打包大小超过500kb警告
  53. chunkSizeWarningLimit: 4000,
  54. rollupOptions: {
  55. input: {
  56. index: pathResolve("./index.html", import.meta.url)
  57. },
  58. // 静态资源分类打包
  59. output: {
  60. chunkFileNames: "static/js/[name]-[hash].js",
  61. entryFileNames: "static/js/[name]-[hash].js",
  62. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  63. }
  64. }
  65. },
  66. define: {
  67. __INTLIFY_PROD_DEVTOOLS__: false,
  68. __APP_INFO__: JSON.stringify(__APP_INFO__)
  69. }
  70. };
  71. };