YAML Plugin

Used to import YAML files and convert them into JavaScript objects.

TIP

YAML is a data serialization language commonly used for writing configuration files.

Quick Start

Install the Plugin

You can install the plugin using the following command:

npm
yarn
pnpm
bun
npm add @rsbuild/plugin-yaml -D

Register Plugin

You can register the plugin in the rsbuild.config.ts file:

rsbuild.config.ts
import { pluginYaml } from '@rsbuild/plugin-yaml';

export default {
  plugins: [pluginYaml()],
};

Example

Suppose the project has the following code in example.yaml:

example.yaml
hello = "world"

[foo]
bar = "baz"

After using the YAML plugin, you can reference it as follows:

import example from './example.yaml';

console.log(example.hello); // 'world';
console.log(example.foo); // { bar: 'baz' };

Type Declaration

When you import YAML files in TypeScript code, please create a src/env.d.ts file in your project and add the corresponding type declarations.

  • Method 1: If the @rsbuild/core package is installed, you can directly reference the type declarations provided by @rsbuild/core:
src/env.d.ts
/// <reference types="@rsbuild/core/types" />
  • Method 2: Manually add the required type declarations:
src/env.d.ts
declare module '*.yaml' {
  const content: Record<string, any>;
  export default content;
}