Skip to content

Commit 482f225

Browse files
Claude Botclaude
andcommitted
Improve YAML documentation with Bun.YAML.stringify and TypeScript typing
- Add comprehensive Bun.YAML.stringify() documentation with examples - Explain spacing parameter (null, 2) for readable format vs (null, 0) for condensed - Document how YAML can represent shared values more compactly than JSON - Add practical example for generating YAML configuration files - Clarify TypeScript typing requirements (unlike JSON, not automatic) - Add both per-file (.d.ts) and global type declaration approaches - Include Bun.YAML.stringify(obj, null, 2) in examples like JSON.stringify 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6f8138b commit 482f225

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

docs/guides/runtime/import-yaml.mdx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ console.log(data.hobbies); // => ["reading", "coding"]
7777

7878
## TypeScript Support
7979

80-
To add TypeScript support for your YAML imports, create a declaration file with `.d.ts` appended to the YAML filename (e.g., `config.yaml``config.yaml.d.ts`);
80+
Unlike JSON files which TypeScript handles automatically, YAML files require manual type definitions. You have two options for adding TypeScript support:
81+
82+
### Option 1: Per-File Declaration (Recommended)
83+
84+
Create a declaration file with `.d.ts` appended to the YAML filename (e.g., `config.yaml``config.yaml.d.ts`):
8185

8286
```ts config.yaml.d.ts icon="/icons/typescript.svg"
8387
const contents: {
@@ -99,6 +103,26 @@ const contents: {
99103
export = contents;
100104
```
101105

106+
This approach gives you precise types for each YAML file.
107+
108+
### Option 2: Global Declaration
109+
110+
For a global type definition (applies to all `.yaml` and `.yml` files), create a declaration file anywhere in your project:
111+
112+
```ts global.d.ts icon="/icons/typescript.svg"
113+
declare module "*.yaml" {
114+
const content: any;
115+
export default content;
116+
}
117+
118+
declare module "*.yml" {
119+
const content: any;
120+
export default content;
121+
}
122+
```
123+
124+
**Note:** TypeScript doesn't automatically type YAML imports like it does for JSON. You must explicitly add type definitions using one of the methods above.
125+
102126
---
103127

104128
See [Docs > API > YAML](https://bun.com/docs/api/yaml) for complete documentation on YAML support in Bun.

docs/runtime/yaml.mdx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,61 @@ Bun's YAML parser currently passes over 90% of the official YAML test suite. Whi
2323

2424
Parse a YAML string into a JavaScript object.
2525

26+
### `Bun.YAML.stringify()`
27+
28+
Convert a JavaScript object into a YAML string.
29+
30+
```ts
31+
import { YAML } from "bun";
32+
33+
const obj = {
34+
name: "John Doe",
35+
age: 30,
36+
37+
hobbies: ["reading", "coding", "hiking"]
38+
};
39+
40+
const yaml = Bun.YAML.stringify(obj, null, 2);
41+
console.log(yaml);
42+
// name: John Doe
43+
// age: 30
44+
45+
// hobbies:
46+
// - reading
47+
// - coding
48+
// - hiking
49+
```
50+
51+
The signature is similar to `JSON.stringify()`:
52+
53+
```ts
54+
Bun.YAML.stringify(value, replacer?, space?)
55+
```
56+
57+
- `value`: The JavaScript value to convert to YAML
58+
- `replacer`: Optional function to transform values (same as `JSON.stringify`)
59+
- `space`: Number of spaces for indentation (default: 2)
60+
61+
#### Spacing Options
62+
63+
The `space` parameter controls indentation. Like `JSON.stringify`, you can pass a number for spaces:
64+
65+
```ts
66+
// Readable format with 2 spaces (recommended)
67+
const yaml = Bun.YAML.stringify(obj, null, 2);
68+
69+
// More condensed format with 0 spaces
70+
// YAML can represent shared values more compactly than JSON
71+
const compact = Bun.YAML.stringify(obj, null, 0);
72+
console.log(compact);
73+
// name: John Doe
74+
// age: 30
75+
76+
// hobbies: [reading, coding, hiking]
77+
```
78+
79+
With `space: 0`, YAML uses inline notation for arrays and objects where appropriate, making the output more compact than equivalent JSON. This is particularly useful when YAML detects shared/repeated values in your data structure.
80+
2681
```ts
2782
import { YAML } from "bun";
2883
const text = `
@@ -435,6 +490,49 @@ if (parseConfig(migrations).autoRun === "true") {
435490
}
436491
```
437492

493+
### Generating YAML Files
494+
495+
You can use `Bun.YAML.stringify()` to programmatically create YAML configuration files:
496+
497+
```ts generate-config.ts icon="/icons/typescript.svg"
498+
import { YAML } from "bun";
499+
500+
const config = {
501+
server: {
502+
port: 3000,
503+
host: "localhost",
504+
},
505+
database: {
506+
type: "postgres",
507+
host: "localhost",
508+
port: 5432,
509+
name: "myapp",
510+
pool: {
511+
min: 2,
512+
max: 10,
513+
},
514+
},
515+
features: {
516+
auth: true,
517+
rateLimit: true,
518+
analytics: false,
519+
},
520+
};
521+
522+
// Generate readable YAML with 2-space indentation
523+
const yaml = Bun.YAML.stringify(config, null, 2);
524+
await Bun.write("config.yaml", yaml);
525+
526+
console.log("Generated config.yaml:");
527+
console.log(yaml);
528+
```
529+
530+
This is particularly useful for:
531+
- Generating configuration files from templates
532+
- Exporting settings from your application
533+
- Creating config files for CI/CD pipelines
534+
- Building development tools that output YAML
535+
438536
### Bundler Integration
439537

440538
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)