You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.4 KiB

3 years ago
3 years ago
3 years ago
  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. ],
  80. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  81. },
  82. build: {
  83. sourcemap: false,
  84. brotliSize: false,
  85. // 消除打包大小超过500kb警告
  86. chunkSizeWarningLimit: 2000
  87. },
  88. define: {
  89. __INTLIFY_PROD_DEVTOOLS__: false
  90. }
  91. };
  92. };