vite.config.ts 2.0 KB

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