vite.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import dayjs from "dayjs";
  2. import { resolve } from "path";
  3. import pkg from "./package.json";
  4. import { warpperEnv, regExps } from "./build";
  5. import { getPluginsList } from "./build/plugins";
  6. import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  7. // 当前执行node命令时文件夹的地址(工作目录)
  8. const root: string = process.cwd();
  9. // 路径查找
  10. const pathResolve = (dir: string): string => {
  11. return resolve(__dirname, ".", dir);
  12. };
  13. // 设置别名
  14. const alias: Record<string, string> = {
  15. "/@": pathResolve("src"),
  16. "@build": pathResolve("build"),
  17. //解决开发环境下的警告
  18. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  19. };
  20. const { dependencies, devDependencies, name, version } = pkg;
  21. const __APP_INFO__ = {
  22. pkg: { dependencies, devDependencies, name, version },
  23. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  24. };
  25. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  26. const {
  27. VITE_PORT,
  28. VITE_LEGACY,
  29. VITE_PUBLIC_PATH,
  30. VITE_PROXY_DOMAIN,
  31. VITE_PROXY_DOMAIN_REAL
  32. } = warpperEnv(loadEnv(mode, root));
  33. return {
  34. base: VITE_PUBLIC_PATH,
  35. root,
  36. resolve: {
  37. alias
  38. },
  39. css: {
  40. // https://github.com/vitejs/vite/issues/5833
  41. postcss: {
  42. plugins: [
  43. {
  44. postcssPlugin: "internal:charset-removal",
  45. AtRule: {
  46. charset: atRule => {
  47. if (atRule.name === "charset") {
  48. atRule.remove();
  49. }
  50. }
  51. }
  52. }
  53. ]
  54. }
  55. },
  56. // 服务端渲染
  57. server: {
  58. // 是否开启 https
  59. https: false,
  60. // 端口号
  61. port: VITE_PORT,
  62. host: "0.0.0.0",
  63. // 本地跨域代理
  64. proxy:
  65. VITE_PROXY_DOMAIN_REAL.length > 0
  66. ? {
  67. [VITE_PROXY_DOMAIN]: {
  68. target: VITE_PROXY_DOMAIN_REAL,
  69. // ws: true,
  70. changeOrigin: true,
  71. rewrite: (path: string) => regExps(path, VITE_PROXY_DOMAIN)
  72. }
  73. }
  74. : null
  75. },
  76. plugins: getPluginsList(command, VITE_LEGACY),
  77. optimizeDeps: {
  78. include: [
  79. "pinia",
  80. "vue-i18n",
  81. "lodash-es",
  82. "@vueuse/core",
  83. "@iconify/vue",
  84. "element-plus/lib/locale/lang/en",
  85. "element-plus/lib/locale/lang/zh-cn",
  86. "vxe-table/lib/locale/lang/zh-CN",
  87. "vxe-table/lib/locale/lang/en-US"
  88. ],
  89. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  90. },
  91. build: {
  92. sourcemap: false,
  93. brotliSize: false,
  94. // 消除打包大小超过500kb警告
  95. chunkSizeWarningLimit: 2000
  96. },
  97. define: {
  98. __INTLIFY_PROD_DEVTOOLS__: false,
  99. __APP_INFO__: JSON.stringify(__APP_INFO__)
  100. }
  101. };
  102. };