vite.config.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * @Author: Gui
  3. * @Date: 2023-03-01 19:20:44
  4. * @LastEditors: guicheng 1625811865@qq.com
  5. * @LastEditTime: 2024-04-10 15:54:56
  6. * @Description: kxs files
  7. * @filePath:
  8. */
  9. import dayjs from "dayjs";
  10. import { resolve } from "path";
  11. import pkg from "./package.json";
  12. import { warpperEnv } from "./build";
  13. import { getPluginsList } from "./build/plugins";
  14. import { include, exclude } from "./build/optimize";
  15. import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  16. /** 当前执行node命令时文件夹的地址(工作目录) */
  17. const root: string = process.cwd();
  18. /** 路径查找 */
  19. const pathResolve = (dir: string): string => {
  20. return resolve(__dirname, ".", dir);
  21. };
  22. /** 设置别名 */
  23. const alias: Record<string, string> = {
  24. "@": pathResolve("src"),
  25. "@build": pathResolve("build")
  26. };
  27. const { dependencies, devDependencies, name, version } = pkg;
  28. const __APP_INFO__ = {
  29. pkg: { dependencies, devDependencies, name, version },
  30. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  31. };
  32. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  33. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  34. warpperEnv(loadEnv(mode, root));
  35. return {
  36. base: VITE_PUBLIC_PATH,
  37. root,
  38. resolve: {
  39. alias
  40. },
  41. // 服务端渲染
  42. server: {
  43. // 是否开启 https
  44. https: false,
  45. // 端口号
  46. port: VITE_PORT,
  47. host: "0.0.0.0",
  48. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  49. proxy: {
  50. // "/api": {
  51. // // 这里填写后端地址
  52. // target: "http://test.apigateway.shuangkebang.com",
  53. // changeOrigin: true,
  54. // rewrite: path => path.replace(/^\/api/, "")
  55. // }
  56. }
  57. },
  58. plugins: getPluginsList(command, VITE_CDN, VITE_COMPRESSION),
  59. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  60. optimizeDeps: {
  61. include,
  62. exclude
  63. },
  64. build: {
  65. sourcemap: false,
  66. // 消除打包大小超过500kb警告
  67. chunkSizeWarningLimit: 4000,
  68. rollupOptions: {
  69. input: {
  70. index: pathResolve("index.html")
  71. },
  72. // 静态资源分类打包
  73. output: {
  74. chunkFileNames: "static/js/[name]-[hash].js",
  75. entryFileNames: "static/js/[name]-[hash].js",
  76. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  77. }
  78. }
  79. },
  80. define: {
  81. __INTLIFY_PROD_DEVTOOLS__: false,
  82. __APP_INFO__: JSON.stringify(__APP_INFO__)
  83. }
  84. };
  85. };