vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import dayjs from "dayjs";
  2. import { resolve } from "path";
  3. import pkg from "./package.json";
  4. import { wrapperEnv } from "./build";
  5. import { getPluginsList } from "./build/plugins";
  6. import { include, exclude } from "./build/optimize";
  7. import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  8. /** 当前执行node命令时文件夹的地址(工作目录) */
  9. const root: string = process.cwd();
  10. /** 路径查找 */
  11. const pathResolve = (dir: string): string => {
  12. return resolve(__dirname, ".", dir);
  13. };
  14. /** 设置别名 */
  15. const alias: Record<string, string> = {
  16. "@": pathResolve("src"),
  17. "@build": pathResolve("build")
  18. };
  19. const { dependencies, devDependencies, name, version } = pkg;
  20. const __APP_INFO__ = {
  21. pkg: { dependencies, devDependencies, name, version },
  22. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  23. };
  24. export default ({ mode }: ConfigEnv): UserConfigExport => {
  25. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  26. wrapperEnv(loadEnv(mode, root));
  27. return {
  28. base: VITE_PUBLIC_PATH,
  29. root,
  30. resolve: {
  31. alias
  32. },
  33. // 服务端渲染
  34. server: {
  35. // 是否开启 https
  36. https: false,
  37. // 端口号
  38. port: VITE_PORT,
  39. host: "0.0.0.0",
  40. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  41. proxy: {
  42. "/dev-api": {
  43. target: "http://localhost:8080",
  44. changeOrigin: true,
  45. rewrite: path => path.replace(/^\/dev-api/, "")
  46. }
  47. }
  48. },
  49. plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
  50. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  51. optimizeDeps: {
  52. include,
  53. exclude
  54. },
  55. build: {
  56. sourcemap: false,
  57. // 消除打包大小超过500kb警告
  58. chunkSizeWarningLimit: 4000,
  59. rollupOptions: {
  60. input: {
  61. index: pathResolve("index.html")
  62. },
  63. // 静态资源分类打包
  64. output: {
  65. chunkFileNames: "static/js/[name]-[hash].js",
  66. entryFileNames: "static/js/[name]-[hash].js",
  67. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  68. }
  69. }
  70. },
  71. define: {
  72. __INTLIFY_PROD_DEVTOOLS__: false,
  73. __APP_INFO__: JSON.stringify(__APP_INFO__)
  74. }
  75. };
  76. };