文档中心

探索 DWeb 框架的无限可能,构建下一代高性能 Web 应用

配置管理 (Config)

DWeb 框架提供了灵活的配置加载机制,支持单应用和多应用模式。配置文件使用 TypeScript,提供完整的类型支持。

加载配置

框架会自动查找并加载 dweb.config.ts 配置文件。你也可以手动加载配置:

Code
import { loadConfig } from '@dreamer/dweb';

// 加载默认配置
const { config, configDir } = await loadConfig();

// 加载指定配置文件
const { config } = await loadConfig('./dweb.config.ts');

// 多应用模式
const { config } = await loadConfig('./dweb.config.ts', 'app-name');

基本配置

单应用模式的基本配置示例:

Code
// dweb.config.ts
import type { DWebConfig } from '@dreamer/dweb';
import { tailwind, cors } from '@dreamer/dweb';

const config: DWebConfig = {
  server: {
    port: 3000,
    host: 'localhost',
  },
  routes: {
    dir: 'routes',
  },
  static: {
    dir: 'assets',
    prefix: '/assets',
  },
  plugins: [
    tailwind({ version: 'v4' }),
    cors({ origin: '*' }),
  ],
};

export default config;

配置选项

  • server - 服务器配置(端口、主机等)
  • routes - 路由配置(目录、忽略规则等)
  • static - 静态资源配置(目录、前缀、缓存等)
  • plugins - 插件列表
  • middleware - 中间件列表
  • cookie - Cookie 配置
  • session - Session 配置
  • database - 数据库配置

多应用模式

多应用模式允许你在一个配置文件中定义多个应用,每个应用有独立的服务器、路由和插件配置:

Code
// dweb.config.ts
import type { DWebConfig } from '@dreamer/dweb';
import { tailwind, cors } from '@dreamer/dweb';

const config: DWebConfig = {
  cookie: {
    secret: 'your-secret-key',
  },
  session: {
    secret: 'your-session-secret',
    store: 'memory',
  },
  apps: [
    {
      name: 'frontend',
      server: { port: 3000 },
      routes: { dir: 'frontend/routes' },
      plugins: [tailwind()],
    },
    {
      name: 'backend',
      server: { port: 3001 },
      routes: { dir: 'backend/routes' },
      plugins: [cors()],
    },
  ],
};

export default config;

启动指定应用:deno run -A jsr:@dreamer/dweb/cli dev:app-name

环境变量

可以在配置文件中使用环境变量,方便在不同环境中使用不同的配置:

Code
// dweb.config.ts
import type { DWebConfig } from '@dreamer/dweb';

const config: DWebConfig = {
  server: {
    port: parseInt(Deno.env.get('PORT') || '3000'),
    host: Deno.env.get('HOST') || 'localhost',
  },
  // ... 其他配置
};

export default config;

配置规范化

使用 normalizeRouteConfig 函数规范化路由配置:

Code
import { normalizeRouteConfig } from "@dreamer/dweb";

// 规范化路由配置
const routeConfig = normalizeRouteConfig({
  dir: "routes",
  ignore: ["**/*.test.ts"],
  cache: true,
  priority: "specific-first",
  apiDir: "routes/api",
});

配置文件位置

框架会按以下顺序查找配置文件:

  1. 当前工作目录的 dweb.config.ts
  2. 当前工作目录的 dweb.config.js
  3. 如果使用 loadConfig(path),则加载指定路径的配置文件

API 参考

loadConfig

Code
function loadConfig(
  configPath?: string,
  appName?: string
): Promise<{ config: AppConfig; configDir: string }>

**参数:**

  • configPath: 配置文件路径(可选,默认为 dweb.config.ts
  • appName: 应用名称(多应用模式使用)

**返回:**

  • config: 配置对象
  • configDir: 配置文件所在目录

normalizeRouteConfig

Code
function normalizeRouteConfig(
  routes: string | {
    dir: string;
    ignore?: string[];
    cache?: boolean;
    priority?: "specific-first" | "order";
    apiDir?: string;
  }
): {
  dir: string;
  ignore: string[];
  cache: boolean;
  priority: "specific-first" | "order";
  apiDir: string;
}

**参数:**

  • routes: 路由配置(字符串或配置对象)

**返回:**规范化后的路由配置对象

相关文档