vite.config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. import zipPack from "vite-plugin-zip-pack";
  8. import { createHash } from "crypto"
  9. // 当前执行node命令时文件夹的地址(工作目录)
  10. const root: string = process.cwd();
  11. // 路径查找
  12. const pathResolve = (dir: string): string => {
  13. return resolve(__dirname, ".", dir);
  14. };
  15. // 设置别名
  16. const alias: Record<string, string> = {
  17. "/@": pathResolve("src"),
  18. "@build": pathResolve("build"),
  19. "@views": pathResolve("src/views")
  20. };
  21. const { dependencies, devDependencies, name, version } = pkg;
  22. const __APP_INFO__ = {
  23. pkg: { dependencies, devDependencies, name, version },
  24. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  25. };
  26. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  27. const {
  28. VITE_PORT,
  29. VITE_LEGACY,
  30. VITE_PUBLIC_PATH,
  31. VITE_PROXY_DOMAIN,
  32. VITE_PROXY_DOMAIN_REAL
  33. } = warpperEnv(loadEnv(mode, root));
  34. return {
  35. base: VITE_PUBLIC_PATH,
  36. root,
  37. resolve: {
  38. alias
  39. },
  40. // 服务端渲染
  41. server: {
  42. // 是否开启 https
  43. https: false,
  44. // 端口号
  45. port: VITE_PORT,
  46. host: "0.0.0.0",
  47. // 本地跨域代理
  48. proxy:
  49. VITE_PROXY_DOMAIN_REAL.length > 0
  50. ? {
  51. [VITE_PROXY_DOMAIN]: {
  52. target: VITE_PROXY_DOMAIN_REAL,
  53. // ws: true,
  54. changeOrigin: true,
  55. rewrite: (path: string) => regExps(path, VITE_PROXY_DOMAIN)
  56. }
  57. }
  58. : null
  59. },
  60. plugins: [ ...getPluginsList(command, VITE_LEGACY)
  61. // zipPack({
  62. // inDir:'dist',
  63. // outFileName:'build.zip',
  64. // pathPrefix:''
  65. // })
  66. ],
  67. optimizeDeps: {
  68. include: ["pinia", "lodash-es", "@vueuse/core"],
  69. exclude: ["@pureadmin/theme/dist/browser-utils"]
  70. },
  71. build: {
  72. rollupOptions: {
  73. output: {
  74. entryFileNames: 'assets/[name].js',
  75. chunkFileNames: chunkInfo => {
  76. if (chunkInfo.isDynamicEntry) {
  77. const hash = createHash('sha256')
  78. .update(Object.values(chunkInfo.modules).map(m => m.code).join())
  79. .digest('hex')
  80. .substr(0, 6)
  81. return 'assets/[name].' + hash + '.js'
  82. } else {
  83. return 'assets/[name].[hash].js'
  84. }
  85. }
  86. },
  87. }
  88. },
  89. define: {
  90. __INTLIFY_PROD_DEVTOOLS__: false,
  91. __APP_INFO__: JSON.stringify(__APP_INFO__)
  92. }
  93. };
  94. };