Rsbuild Core

本章节描述了 Rsbuild 提供的一些核心方法。

createRsbuild

创建一个 Rsbuild 实例对象。

import { createRsbuild } from '@rsbuild/core';

const rsbuild = await createRsbuild({
  // options
});

options

createRsbuild 的第一个参数是一个配置对象,你可以传入以下选项:

type RsbuildEntry = Record<string, string | string[]>;

type RsbuildTarget = 'web' | 'node' | 'web-worker';

type CreateRsbuildOptions = {
  cwd?: string;
  target?: RsbuildTarget | RsbuildTarget[];
  provider?: RsbuildProvider;
  configPath?: string | null;
  rsbuildConfig?: RsbuildConfig;
};

各个选项的作用:

  • cwd: 当前执行构建的根路径,默认值为 process.cwd()
  • target: 构建产物类型,默认值为 ['web'],详见 构建产物类型 章节。
  • configPath: 上层解决方案配置文件的路径(绝对路径),该参数影响构建缓存更新
  • provider: 用于切换底层的打包工具。
  • rsbuildConfig:Rsbuild 配置对象。Rsbuild 提供了丰富的配置项,允许你对构建行为进行灵活定制,你可以在 配置 中找到所有可用的配置项。

mergeRsbuildConfig

用于合并多份 Rsbuild 配置对象。

mergeRsbuildConfig 函数接收多个配置对象作为参数,它会将每一个配置对象进行深层合并,自动将多个函数项合并为顺序执行的函数数组,返回一个合并后的配置对象。

  • 类型
function mergeRsbuildConfig(...configs: RsbuildConfig[]): RsbuildConfig;
  • Example
import { mergeRsbuildConfig } from '@rsbuild/core';

const config1 = {
  dev: {
    https: false,
  },
};
const config2 = {
  dev: {
    https: true,
  },
};

const mergedConfig = mergeRsbuildConfig(config1, config2);

console.log(mergedConfig); // { dev: { https: true } }

该方法不会修改入参中的 config 对象。