vite.config.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /// <reference types="vitest/config" />
  2. import process from "node:process";
  3. import react from "@vitejs/plugin-react";
  4. import { codeInspectorPlugin } from "code-inspector-plugin";
  5. import dayjs from "dayjs";
  6. import { defineConfig } from "vite";
  7. import { checker } from "vite-plugin-checker";
  8. import { vitePluginFakeServer } from "vite-plugin-fake-server";
  9. import svgrPlugin from "vite-plugin-svgr";
  10. import { author, dependencies, devDependencies, license, name, version } from "./package.json";
  11. const __APP_INFO__ = {
  12. pkg: { dependencies, devDependencies, name, version, license, author },
  13. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss"),
  14. };
  15. const isDev = process.env.NODE_ENV === "development";
  16. // https://vitejs.dev/config/
  17. // @ts-ignore
  18. // @ts-ignore
  19. export default defineConfig({
  20. base: isDev ? "/" : "/admin/",
  21. plugins: [
  22. react(),
  23. vitePluginFakeServer({
  24. basename: "/api",
  25. enableDev: false,
  26. enableProd: false,
  27. timeout: 1000,
  28. }),
  29. // https://github.com/pd4d10/vite-plugin-svgr#options
  30. svgrPlugin({
  31. // https://react-svgr.com/docs/options/
  32. svgrOptions: {
  33. plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"],
  34. svgoConfig: {
  35. floatPrecision: 2,
  36. },
  37. },
  38. }),
  39. checker({
  40. typescript: true,
  41. terminal: false,
  42. enableBuild: false,
  43. }),
  44. /**
  45. * 点击页面 DOM 打开 IDE 并将光标自动定位到源代码位置
  46. *
  47. * macOS 默认组合键 Option + Shift
  48. * Windows 默认组合键 Alt + Shift
  49. * 在 Web 页面上按住组合键时,移动鼠标即会在 DOM 上出现遮罩层并显示相关信息,鼠标点击一下,将自动打开 IDE 并将光标定位到元素对应的代码位置
  50. * 更多用法看 https://inspector.fe-dev.cn/guide/start.html
  51. */
  52. codeInspectorPlugin({
  53. bundler: "vite",
  54. // hideConsole: true,
  55. }),
  56. ],
  57. // test: {
  58. // globals: true,
  59. // environment: "happy-dom",
  60. // setupFiles: ["./src/setupTests.ts"],
  61. // },
  62. server: {
  63. port: 3333,
  64. // https://vitejs.dev/config/server-options#server-proxy
  65. proxy: {
  66. "/admin": {
  67. target: "http://localhost:7001",
  68. changeOrigin: true,
  69. // rewrite: path => isDev ? path.replace(/^\/api/, "") : path,
  70. },
  71. },
  72. },
  73. define: {
  74. __APP_INFO__: JSON.stringify(__APP_INFO__),
  75. },
  76. build: {
  77. outDir: "build",
  78. sourcemap: false,
  79. rollupOptions: {
  80. output: {
  81. manualChunks: {
  82. react: ["react", "react-dom", "react-router"],
  83. antd: ["antd", "@ant-design/icons"],
  84. faker: ["@faker-js/faker"],
  85. },
  86. },
  87. },
  88. },
  89. });