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.

95 lines
2.4 KiB

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. const { dependencies, devDependencies, name, version } = pkg;
  19. const __APP_INFO__ = {
  20. pkg: { dependencies, devDependencies, name, version },
  21. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  22. };
  23. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  24. const {
  25. VITE_PORT,
  26. VITE_LEGACY,
  27. VITE_PUBLIC_PATH,
  28. VITE_PROXY_DOMAIN,
  29. VITE_PROXY_DOMAIN_REAL
  30. } = warpperEnv(loadEnv(mode, root));
  31. return {
  32. base: VITE_PUBLIC_PATH,
  33. root,
  34. resolve: {
  35. alias
  36. },
  37. css: {
  38. // https://github.com/vitejs/vite/issues/5833
  39. postcss: {
  40. plugins: [
  41. {
  42. postcssPlugin: "internal:charset-removal",
  43. AtRule: {
  44. charset: atRule => {
  45. if (atRule.name === "charset") {
  46. atRule.remove();
  47. }
  48. }
  49. }
  50. }
  51. ]
  52. }
  53. },
  54. // 服务端渲染
  55. server: {
  56. // 是否开启 https
  57. https: false,
  58. // 端口号
  59. port: VITE_PORT,
  60. host: "0.0.0.0",
  61. // 本地跨域代理
  62. proxy:
  63. VITE_PROXY_DOMAIN_REAL.length > 0
  64. ? {
  65. [VITE_PROXY_DOMAIN]: {
  66. target: VITE_PROXY_DOMAIN_REAL,
  67. // ws: true,
  68. changeOrigin: true,
  69. rewrite: (path: string) => regExps(path, VITE_PROXY_DOMAIN)
  70. }
  71. }
  72. : null
  73. },
  74. plugins: getPluginsList(command, VITE_LEGACY),
  75. optimizeDeps: {
  76. include: ["pinia", "vue-i18n", "lodash-es", "@vueuse/core"],
  77. exclude: ["@pureadmin/theme/dist/browser-utils"]
  78. },
  79. build: {
  80. sourcemap: false,
  81. brotliSize: false,
  82. // 消除打包大小超过500kb警告
  83. chunkSizeWarningLimit: 4000
  84. },
  85. define: {
  86. __INTLIFY_PROD_DEVTOOLS__: false,
  87. __APP_INFO__: JSON.stringify(__APP_INFO__)
  88. }
  89. };
  90. };