vite.config.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * @Author: Gui
  3. * @Date: 2023-03-01 19:20:44
  4. * @LastEditors: guicheng 1625811865@qq.com
  5. * @LastEditTime: 2024-07-12 15:08:30
  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. target: "esnext",
  66. sourcemap: false,
  67. // 消除打包大小超过500kb警告
  68. chunkSizeWarningLimit: 4000,
  69. rollupOptions: {
  70. input: {
  71. index: pathResolve("index.html")
  72. },
  73. // 静态资源分类打包
  74. output: {
  75. chunkFileNames: "static/js/[name]-[hash].js",
  76. entryFileNames: "static/js/[name]-[hash].js",
  77. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  78. }
  79. }
  80. },
  81. define: {
  82. __INTLIFY_PROD_DEVTOOLS__: false,
  83. __APP_INFO__: JSON.stringify(__APP_INFO__)
  84. }
  85. };
  86. };