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.

105 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
  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. ],
  87. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  88. },
  89. build: {
  90. sourcemap: false,
  91. brotliSize: false,
  92. // 消除打包大小超过500kb警告
  93. chunkSizeWarningLimit: 2000
  94. },
  95. define: {
  96. __INTLIFY_PROD_DEVTOOLS__: false,
  97. __APP_INFO__: JSON.stringify(__APP_INFO__)
  98. }
  99. };
  100. };