vite.config.ts 2.4 KB

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