vite.config.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: tel 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. "/aapi": {
  51. // 这里填写后端地址
  52. // target: "https://logic-executor-api.kexiaoshuang.com",
  53. target: "http://logic-executor-api.kexiaoshuang.com",
  54. changeOrigin: true,
  55. rewrite: path => path.replace(/^\/aapi/, "")
  56. }
  57. }
  58. },
  59. plugins: getPluginsList(command, VITE_CDN, VITE_COMPRESSION),
  60. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  61. optimizeDeps: {
  62. include,
  63. exclude
  64. },
  65. build: {
  66. target: "esnext",
  67. sourcemap: false,
  68. // 消除打包大小超过500kb警告
  69. chunkSizeWarningLimit: 4000,
  70. rollupOptions: {
  71. input: {
  72. index: pathResolve("index.html")
  73. },
  74. // 静态资源分类打包
  75. output: {
  76. chunkFileNames: "static/js/[name]-[hash].js",
  77. entryFileNames: "static/js/[name]-[hash].js",
  78. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  79. }
  80. }
  81. },
  82. define: {
  83. __INTLIFY_PROD_DEVTOOLS__: false,
  84. __APP_INFO__: JSON.stringify(__APP_INFO__)
  85. }
  86. };
  87. };