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.

90 lines
2.4 KiB

  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_CDN,
  26. VITE_PORT,
  27. VITE_LEGACY,
  28. VITE_COMPRESSION,
  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. // 服务端渲染
  40. server: {
  41. // 是否开启 https
  42. https: false,
  43. // 端口号
  44. port: VITE_PORT,
  45. host: "0.0.0.0",
  46. // 本地跨域代理
  47. proxy:
  48. VITE_PROXY_DOMAIN_REAL.length > 0
  49. ? {
  50. [VITE_PROXY_DOMAIN]: {
  51. target: VITE_PROXY_DOMAIN_REAL,
  52. // ws: true,
  53. changeOrigin: true,
  54. rewrite: (path: string) => regExps(path, VITE_PROXY_DOMAIN)
  55. }
  56. }
  57. : null
  58. },
  59. plugins: getPluginsList(command, VITE_LEGACY, VITE_CDN, VITE_COMPRESSION),
  60. optimizeDeps: {
  61. include: ["pinia", "vue-i18n", "lodash-es", "@vueuse/core", "dayjs"],
  62. exclude: ["@pureadmin/theme/dist/browser-utils"]
  63. },
  64. build: {
  65. sourcemap: false,
  66. // 消除打包大小超过500kb警告
  67. chunkSizeWarningLimit: 4000,
  68. rollupOptions: {
  69. input: {
  70. index: pathResolve("index.html")
  71. },
  72. // 静态资源分类打包
  73. output: {
  74. chunkFileNames: "static/js/[name]-[hash].js",
  75. entryFileNames: "static/js/[name]-[hash].js",
  76. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  77. }
  78. }
  79. },
  80. define: {
  81. __INTLIFY_PROD_DEVTOOLS__: false,
  82. __APP_INFO__: JSON.stringify(__APP_INFO__)
  83. }
  84. };
  85. };