vite.config.ts 2.4 KB

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