configuration.md 5.98 KB

项目配置

本文将介绍 Venus Admin 的配置系统和常用配置项。

配置文件

Venus Admin 使用多层配置文件来管理不同环境的配置:

apps/playground/
├── .env                    # 所有环境的基础配置
├── .env.development        # 开发环境配置
├── .env.production         # 生产环境配置
└── src/
    └── preferences.ts      # 应用偏好设置

环境变量

.env 基础配置

所有环境共享的基础配置:

# 应用标题
VITE_APP_TITLE=Venus Admin

# API 基础路径
VITE_API_BASE_URL=/api

# 是否启用 Mock
VITE_ENABLE_MOCK=true

.env.development 开发环境

# 开发环境配置
NODE_ENV=development

# 开发服务器端口
VITE_PORT=5555

# API 地址
VITE_API_BASE_URL=http://localhost:3000

# 是否启用 Mock
VITE_ENABLE_MOCK=true

.env.production 生产环境

# 生产环境配置
NODE_ENV=production

# API 地址
VITE_API_BASE_URL=https://api.yourdomain.com

# 是否启用 Mock
VITE_ENABLE_MOCK=false

# 是否启用压缩
VITE_BUILD_COMPRESS=gzip

# 是否删除 console
VITE_DROP_CONSOLE=true

应用偏好设置

在 src/preferences.ts 中配置应用的偏好设置:

import { defineOverridesPreferences } from '@venus/preferences';

export const overridesPreferences = defineOverridesPreferences({
  // 应用配置
  app: {
    name: 'Venus Admin',
    // 是否启用动态标题
    enableDynamicTitle: true,
    // 是否启用检查更新
    enableCheckUpdates: true,
    // 水印配置
    watermark: false,
  },

  // 主题配置
  theme: {
    // 主题模式: light | dark | auto
    mode: 'auto',
    // 主题色
    colorPrimary: '#646cff',
    // 圆角
    radius: '0.5rem',
  },

  // 布局配置
  layout: {
    // 布局模式: sidebar | mix | header
    mode: 'sidebar',
    // 内容区域宽度: full | fixed
    contentCompact: 'wide',
  },

  // 导航配置
  navigation: {
    // 导航风格: rounded | default
    styleType: 'rounded',
    // 导航分割菜单
    split: true,
  },

  // 侧边栏配置
  sidebar: {
    // 是否折叠
    collapsed: false,
    // 折叠时是否显示标题
    collapsedShowTitle: false,
    // 侧边栏宽度
    width: 240,
    // 折叠宽度
    collapsedWidth: 64,
  },

  // 头部配置
  header: {
    // 头部高度
    height: 50,
    // 是否显示
    hidden: false,
  },

  // 标签页配置
  tabbar: {
    // 是否启用
    enable: true,
    // 是否持久化
    persist: true,
    // 高度
    height: 40,
  },

  // 页脚配置
  footer: {
    // 是否启用
    enable: true,
    // 是否固定
    fixed: false,
  },

  // 过渡动画
  transition: {
    // 是否启用
    enable: true,
    // 动画名称: fade | fade-slide | fade-bottom | fade-scale | zoom-fade | zoom-out
    name: 'fade-slide',
  },

  // 缓存配置
  cache: {
    // 是否启用页面缓存
    enable: true,
  },
});

主题定制

颜色配置

在 tailwind.config.mjs 中配置主题颜色:

/** @type {import('tailwindcss').Config} */
export default {
  theme: {
    extend: {
      colors: {
        primary: {
          DEFAULT: '#646cff',
          50: '#f0f1ff',
          100: '#e4e5ff',
          // ...更多色阶
        },
      },
    },
  },
};

CSS 变量

在全局样式中定义 CSS 变量:

:root {
  --color-primary: #646cff;
  --color-success: #52c41a;
  --color-warning: #faad14;
  --color-error: #f5222d;
  --color-info: #1890ff;

  --border-radius: 0.5rem;
  --sidebar-width: 240px;
  --header-height: 50px;
}

路由配置

路由模式

在 src/router/index.ts 中配置路由模式:

import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
  // 使用 HTML5 History 模式
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  // 路由跳转时滚动行为
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

基础路径

在 vite.config.mts 中配置应用基础路径:

export default defineConfig({
  base: '/', // 修改为你的基础路径,如 '/admin/'
});

构建配置

Vite 配置

在 vite.config.mts 中配置构建选项:

import { defineConfig } from 'vite';

export default defineConfig({
  // 服务器配置
  server: {
    port: 5555,
    host: true,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },

  // 构建配置
  build: {
    target: 'es2015',
    outDir: 'dist',
    // 资源内联阈值
    assetsInlineLimit: 4096,
    // chunk 大小警告限制
    chunkSizeWarningLimit: 500,
    rollupOptions: {
      output: {
        // 分包策略
        manualChunks: {
          vue: ['vue', 'vue-router', 'pinia'],
          vendor: ['axios', 'dayjs'],
        },
      },
    },
  },

  // 优化配置
  optimizeDeps: {
    include: ['vue', 'vue-router', 'pinia'],
  },
});

代理配置

开发环境代理

在 vite.config.mts 中配置开发环境的 API 代理:

export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
      '/upload': {
        target: 'http://localhost:3000',
        changeOrigin: true,
      },
    },
  },
});

TypeScript 配置

在 tsconfig.json 中配置 TypeScript:

{
  "extends": "@venus/tsconfig/web-app.json",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "#/*": ["types/*"]
    }
  },
  "include": ["src/**/*", "types/**/*"],
  "exclude": ["node_modules", "dist"]
}

下一步