Skip to content

Commit 15c5fec

Browse files
Claude Botclaude
andcommitted
Improve YAML documentation with Bun.YAML.stringify and TypeScript typing
- Add Bun.YAML.stringify() documentation with examples using (null, 2) for spacing - Document space parameter: 2 for readable format, 0 for condensed inline notation - Add TypeScript section in Module Import explaining manual .d.ts requirement - Clarify that unlike JSON, TypeScript doesn't automatically type YAML imports - Show generating YAML with shared values using CodeGroup tabs - Fix heading structure: parse examples under parse, stringify as separate section 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6f8138b commit 15c5fec

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

docs/runtime/yaml.mdx

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,47 @@ try {
121121
}
122122
```
123123

124+
### `Bun.YAML.stringify()`
125+
126+
Convert a JavaScript object into a YAML string.
127+
128+
```ts
129+
import { YAML } from "bun";
130+
131+
const obj = {
132+
name: "John Doe",
133+
age: 30,
134+
135+
hobbies: ["reading", "coding", "hiking"]
136+
};
137+
138+
const yaml = Bun.YAML.stringify(obj, null, 2);
139+
console.log(yaml);
140+
// name: John Doe
141+
// age: 30
142+
143+
// hobbies:
144+
// - reading
145+
// - coding
146+
// - hiking
147+
```
148+
149+
The signature is similar to `JSON.stringify()`:
150+
151+
```ts
152+
Bun.YAML.stringify(value, replacer?, space?)
153+
```
154+
155+
The `space` parameter controls indentation (default: 2). With `space: 0`, YAML uses inline notation making output more condensed than JSON:
156+
157+
```ts
158+
const compact = Bun.YAML.stringify(obj, null, 0);
159+
// name: John Doe
160+
// age: 30
161+
162+
// hobbies: [reading, coding, hiking]
163+
```
164+
124165
---
125166
126167
## Module Import
@@ -193,6 +234,30 @@ const { database, redis } = require("./config.yaml");
193234
console.log(database.port); // 5432
194235
```
195236
237+
### TypeScript
238+
239+
Unlike JSON files, TypeScript doesn't automatically type YAML imports. Add type definitions by creating a `.d.ts` file with the same name as your YAML file:
240+
241+
```ts config.yaml.d.ts icon="/icons/typescript.svg"
242+
const contents: {
243+
database: {
244+
host: string;
245+
port: number;
246+
name: string;
247+
};
248+
server: {
249+
port: number;
250+
timeout: number;
251+
};
252+
features: {
253+
auth: boolean;
254+
rateLimit: boolean;
255+
};
256+
};
257+
258+
export = contents;
259+
```
260+
196261
---
197262
198263
## Hot Reloading with YAML
@@ -435,6 +500,51 @@ if (parseConfig(migrations).autoRun === "true") {
435500
}
436501
```
437502

503+
### Generating YAML Files
504+
505+
You can use `Bun.YAML.stringify()` to programmatically create YAML configuration files:
506+
507+
<CodeGroup>
508+
```ts generate-config.ts icon="/icons/typescript.svg"
509+
import { YAML } from "bun";
510+
511+
const defaults = {
512+
timeout: 30000,
513+
retries: 3,
514+
};
515+
516+
const config = {
517+
development: {
518+
...defaults,
519+
host: "localhost",
520+
port: 3000,
521+
},
522+
production: {
523+
...defaults,
524+
host: "api.example.com",
525+
port: 443,
526+
},
527+
};
528+
529+
// Generate readable YAML with 2-space indentation
530+
const yaml = Bun.YAML.stringify(config, null, 2);
531+
await Bun.write("config.yaml", yaml);
532+
```
533+
534+
```yaml config.yaml icon="file-code"
535+
development: &id001
536+
timeout: 30000
537+
retries: 3
538+
host: localhost
539+
port: 3000
540+
production:
541+
timeout: 30000
542+
retries: 3
543+
host: api.example.com
544+
port: 443
545+
```
546+
</CodeGroup>
547+
438548
### Bundler Integration
439549
440550
When you import YAML files in your application and bundle it with Bun, the YAML is parsed at build time and included as a JavaScript module:

0 commit comments

Comments
 (0)