Skip to content

配置选项

BuildConfig

全局配置:

ts
interface BuildConfig {
  cwd?: string                           // 工作目录
  entries?: (BuildEntry | string)[]      // 构建入口
  hooks?: BuildHooks                     // 构建钩子
  watch?: WatchOptions                   // 监听配置
  exports?: ExportsConfig                // 导出配置
  css?: CssOptions                       // CSS 处理配置
  wasm?: boolean | WasmOptions           // WASM 支持
  logLevel?: LogLevel                    // 日志级别
  failOnWarn?: boolean                   // 警告时失败
  onSuccess?: string | Function          // 成功回调
  ignoreWatch?: string[]                 // 忽略监听的文件
}

BundleEntry

Bundle 模式配置:

ts
interface BundleEntry {
  type: 'bundle'
  input: string | string[] | Record<string, string>
  outDir?: string                        // 输出目录
  fileName?: string                      // 自定义输出文件名
  format?: ModuleFormat | ModuleFormat[] // esm, cjs, iife, umd
  platform?: 'browser' | 'node' | 'neutral'
  target?: string                        // es2015, es2020, esnext
  globalName?: string                    // IIFE/UMD 全局变量名

  // 优化
  minify?: boolean | 'dce-only'
  sourcemap?: boolean | 'inline' | 'hidden'
  splitting?: boolean
  treeshake?: boolean

  // 类型声明
  dts?: boolean | DtsOptions
  dtsOnly?: boolean

  // 依赖
  external?: (string | RegExp)[]
  noExternal?: (string | RegExp)[]
  skipNodeModules?: boolean

  // 代码注入
  banner?: string
  footer?: string
  env?: Record<string, string>
  define?: Record<string, string>

  // 其他
  alias?: Record<string, string>
  clean?: boolean | string[]
  stub?: boolean
  shims?: boolean | ShimsConfig
  wasm?: boolean | WasmOptions           // WASM 支持
  plugins?: RolldownPlugin[]
  rolldown?: InputOptions                // rolldown 原生配置
}

TransformEntry

Transform 模式配置:

ts
interface TransformEntry {
  type: 'transform'
  input: string                          // 输入目录
  outDir?: string                        // 输出目录

  // 优化
  minify?: boolean
  sourcemap?: boolean
  target?: string

  // 路径
  alias?: Record<string, string>
  nodeProtocol?: boolean                 // 添加 node: 前缀

  // 其他
  clean?: boolean
  stub?: boolean
  banner?: string
  footer?: string
}

WatchOptions

监听配置:

ts
interface WatchOptions {
  enabled?: boolean                      // 启用监听
  include?: string[]                     // 监听的文件
  exclude?: string[]                     // 排除的文件
  delay?: number                         // 重建延迟(毫秒)
  ignoreInitial?: boolean                // 跳过初始构建
  watchNewFiles?: boolean                // 监听新文件
}

ExportsConfig

导出配置:

ts
interface ExportsConfig {
  enabled?: boolean                      // 启用导出生成
  includeTypes?: boolean                 // 包含类型
  autoUpdate?: boolean                   // 自动更新 package.json
  custom?: Record<string, string>        // 自定义映射
}

WasmOptions

WASM 支持配置:

ts
interface WasmOptions {
  enabled?: boolean                      // 启用 WASM 支持
  maxFileSize?: number                   // 内联最大文件大小(字节,默认 14KB)
  fileName?: string                      // 输出文件名模式
  publicPath?: string                    // 非内联文件的 URL 前缀
  targetEnv?: 'auto' | 'auto-inline' | 'browser' | 'node'  // 目标环境
}

详细使用说明请参考 WASM 支持

CssOptions

CSS 处理配置:

ts
interface CssOptions {
  splitting?: boolean                    // 启用/禁用 CSS 代码分割(默认 true)
  fileName?: string                      // 禁用分割时的输出文件名(默认 'style.css')
  lightningcss?: boolean                 // 启用 LightningCSS(默认 false)
}

选项说明

选项类型默认值说明
splittingbooleantrue启用/禁用 CSS 代码分割。禁用时所有 CSS 合并为单个文件
fileNamestring'style.css'禁用分割时,合并后的 CSS 文件名
lightningcssbooleanfalse启用 LightningCSS 进行 CSS 转换、压缩和添加浏览器前缀

示例

build.config.ts
ts
import { defineConfig } from 'robuild'

// 默认配置(启用代码分割)
export default defineConfig({
  entry: ['./src/index.ts'],
})

// 禁用代码分割,合并到单个文件
export default defineConfig({
  entry: ['./src/index.ts'],
  css: {
    splitting: false,
    fileName: 'bundle.css',
  },
})

// 启用 LightningCSS(需要安装 unplugin-lightningcss)
export default defineConfig({
  entry: ['./src/index.ts'],
  target: 'es2017', // 影响 CSS 浏览器前缀
  css: {
    lightningcss: true,
  },
})

详细使用说明请参考 CSS 处理

BuildHooks

构建钩子:

ts
interface BuildHooks {
  start?: (ctx: BuildContext) => void | Promise<void>
  entries?: (entries: BuildEntry[], ctx: BuildContext) => void | Promise<void>
  rolldownConfig?: (cfg: InputOptions, ctx: BuildContext) => void | Promise<void>
  rolldownOutput?: (cfg: OutputOptions, res: RolldownBuild, ctx: BuildContext) => void | Promise<void>
  end?: (ctx: BuildContext) => void | Promise<void>
}

配置示例

基本库

build.config.ts
ts
import { defineConfig } from 'robuild'

export default defineConfig({
  entries: [
    {
      type: 'bundle',
      input: './src/index.ts',
      format: ['esm', 'cjs'],
      dts: true,
    },
  ],
})

CLI 工具

build.config.ts
ts
import { defineConfig } from 'robuild'

export default defineConfig({
  entries: [
    {
      type: 'bundle',
      input: './src/cli.ts',
      platform: 'node',
      minify: true,
    },
  ],
})

混合模式

build.config.ts
ts
import { defineConfig } from 'robuild'

export default defineConfig({
  entries: [
    {
      type: 'bundle',
      input: './src/index.ts',
      dts: true,
    },
    {
      type: 'transform',
      input: './src/runtime',
      outDir: './dist/runtime',
    },
  ],
})

下一步

Released under the MIT License.