Skip to content

Commit 04fa848

Browse files
oljcTimeless0911
andauthored
docs(json): add documents for YAML and TOML file support (#873)
Co-authored-by: Timeless0911 <[email protected]>
1 parent 905edd7 commit 04fa848

File tree

2 files changed

+232
-2
lines changed

2 files changed

+232
-2
lines changed

website/docs/en/guide/advanced/json-files.mdx

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Import JSON files
22

3-
Rslib supports import JSON files in code.
3+
Rslib supports importing JSON files in code, and also supports importing [YAML](https://yaml.org/) and [TOML](https://toml.io/en/) files and converting them to JSON format.
44

55
## JSON file
66

@@ -104,3 +104,118 @@ export { __webpack_exports__items as items, __webpack_exports__name as name };
104104
</Tab>
105105

106106
</Tabs>
107+
108+
## YAML file
109+
110+
[YAML](https://yaml.org/) is a data serialization language commonly used for writing configuration files.
111+
112+
By adding the [@rsbuild/plugin-yaml](https://github.com/rspack-contrib/rsbuild-plugin-yaml) plugin, you can import `.yaml` or `.yml` files in JavaScript and they will be automatically converted to JavaScript objects.
113+
114+
import { PackageManagerTabs } from '@theme';
115+
116+
<PackageManagerTabs command="add @rsbuild/plugin-yaml -D" />
117+
118+
### Register plugin
119+
120+
You can register the plugin in the `rslib.config.ts` file:
121+
122+
```ts title="rslib.config.ts"
123+
import { pluginYaml } from '@rsbuild/plugin-yaml';
124+
125+
export default {
126+
plugins: [pluginYaml()],
127+
};
128+
```
129+
130+
### Example
131+
132+
<Tabs>
133+
<Tab label="src/index.ts">
134+
135+
```js
136+
import { name } from './example.yaml';
137+
138+
console.log(name); // 'foo';
139+
```
140+
141+
</Tab>
142+
<Tab label="src/example.yaml">
143+
144+
```yaml
145+
name: foo
146+
items:
147+
- 1
148+
- 2
149+
```
150+
151+
</Tab>
152+
</Tabs>
153+
154+
## TOML file
155+
156+
[TOML](https://toml.io/) is a semantically explicit, easy-to-read configuration file format.
157+
158+
By adding the [@rsbuild/plugin-toml](https://github.com/rspack-contrib/rsbuild-plugin-toml) plugin, you can import `.toml` files in JavaScript and it will be automatically converted to JavaScript objects.
159+
160+
<PackageManagerTabs command="add @rsbuild/plugin-toml -D" />
161+
162+
### Register plugin
163+
164+
You can register the plugin in the `rslib.config.ts` file:
165+
166+
```ts title="rslib.config.ts"
167+
import { pluginToml } from '@rsbuild/plugin-toml';
168+
169+
export default {
170+
plugins: [pluginToml()],
171+
};
172+
```
173+
174+
### Example
175+
176+
<Tabs>
177+
<Tab label="src/index.ts">
178+
179+
```js
180+
import { name } from './example.toml';
181+
182+
console.log(name); // 'foo';
183+
```
184+
185+
</Tab>
186+
<Tab label="src/example.toml">
187+
188+
```toml
189+
name = "foo"
190+
items = [1, 2]
191+
```
192+
193+
</Tab>
194+
</Tabs>
195+
196+
## Type declaration
197+
198+
When you import YAML or Toml files in TypeScript code, please create a `src/env.d.ts` file in your project and add the corresponding type declarations.
199+
200+
- Method 1: If the `@rslib/core` package is installed, you can reference the **preset types** provided by `@rslib/core`:
201+
202+
```ts title="src/env.d.ts"
203+
/// <reference types="@rslib/core/types" />
204+
```
205+
206+
- Method 2: Manually add the required type declarations:
207+
208+
```ts title="src/env.d.ts"
209+
declare module '*.yaml' {
210+
const content: Record<string, any>;
211+
export default content;
212+
}
213+
declare module '*.yml' {
214+
const content: Record<string, any>;
215+
export default content;
216+
}
217+
declare module '*.toml' {
218+
const content: Record<string, any>;
219+
export default content;
220+
}
221+
```

website/docs/zh/guide/advanced/json-files.mdx

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 引用 JSON 文件
22

3-
Rslib 支持在代码中引用 JSON 文件。
3+
Rslib 支持在代码中引用 JSON 文件,同时也支持引用 [YAML](https://yaml.org/)[TOML](https://toml.io/cn/) 文件并将其转换为 JSON 格式
44

55
## JSON 文件
66

@@ -101,3 +101,118 @@ export { __webpack_exports__items as items, __webpack_exports__name as name };
101101
</Tab>
102102

103103
</Tabs>
104+
105+
## YAML 文件
106+
107+
[YAML](https://yaml.org/) 是一种数据序列化语言,通常用于编写配置文件。
108+
109+
通过添加 [@rsbuild/plugin-yaml](https://github.com/rspack-contrib/rsbuild-plugin-yaml) 插件,你可以在 JavaScript 中引用 `.yaml``.yml` 文件,它们会被自动转换为 JavaScript 对象。
110+
111+
import { PackageManagerTabs } from '@theme';
112+
113+
<PackageManagerTabs command="add @rsbuild/plugin-yaml -D" />
114+
115+
### 注册插件
116+
117+
你可以在 `rslib.config.ts` 文件中注册插件:
118+
119+
```ts title="rslib.config.ts"
120+
import { pluginYaml } from '@rsbuild/plugin-yaml';
121+
122+
export default {
123+
plugins: [pluginYaml()],
124+
};
125+
```
126+
127+
### 示例
128+
129+
<Tabs>
130+
<Tab label="src/index.ts">
131+
132+
```js
133+
import { name } from './example.yaml';
134+
135+
console.log(name); // 'foo';
136+
```
137+
138+
</Tab>
139+
<Tab label="src/example.yaml">
140+
141+
```yaml
142+
name: foo
143+
items:
144+
- 1
145+
- 2
146+
```
147+
148+
</Tab>
149+
</Tabs>
150+
151+
## TOML 文件
152+
153+
[TOML](https://toml.io/cn/) 是一种数据序列化语言,通常用于编写配置文件。
154+
155+
通过添加 [@rsbuild/plugin-toml](https://github.com/rspack-contrib/rsbuild-plugin-toml) 插件,你可以在 JavaScript 中引用 `.toml` 文件,它们会被自动转换为 JavaScript 对象。
156+
157+
<PackageManagerTabs command="add @rsbuild/plugin-toml -D" />
158+
159+
### 注册插件
160+
161+
你可以在 `rslib.config.ts` 文件中注册插件:
162+
163+
```ts title="rslib.config.ts"
164+
import { pluginToml } from '@rsbuild/plugin-toml';
165+
166+
export default {
167+
plugins: [pluginToml()],
168+
};
169+
```
170+
171+
### 示例
172+
173+
<Tabs>
174+
<Tab label="src/index.ts">
175+
176+
```js
177+
import { name } from './example.toml';
178+
179+
console.log(name); // 'foo';
180+
```
181+
182+
</Tab>
183+
<Tab label="src/example.toml">
184+
185+
```toml
186+
name = "foo"
187+
items = [1, 2]
188+
```
189+
190+
</Tab>
191+
</Tabs>
192+
193+
## 类型声明
194+
195+
当你在 TypeScript 代码中引用 YAML 或 TOML 文件时,请在项目中创建 src/env.d.ts 文件,并添加相应的类型声明。
196+
197+
- 方法一:如果项目里安装了 `@rslib/core` 包,你可以直接引用 `@rslib/core` 提供的**预设类型**:
198+
199+
```ts title="src/env.d.ts"
200+
/// <reference types="@rslib/core/types" />
201+
```
202+
203+
- 方法二:手动添加需要的类型声明:
204+
205+
```ts title="src/env.d.ts"
206+
declare module '*.yaml' {
207+
const content: Record<string, any>;
208+
export default content;
209+
}
210+
declare module '*.yml' {
211+
const content: Record<string, any>;
212+
export default content;
213+
}
214+
declare module '*.toml' {
215+
const content: Record<string, any>;
216+
export default content;
217+
}
218+
```

0 commit comments

Comments
 (0)