Compare commits

...

16 Commits

Author SHA1 Message Date
autofix-ci[bot]
62d0b5a5b0 [autofix.ci] apply automated fixes 2025-11-10 22:48:49 +00:00
Claude Bot
2ec2466e05 docs: address review feedback on YAML docs
- Use ```txt blocks for YAML input and console output
- Wrap replacer parameter note in <Note> component
- Complete incomplete sentence about hot reloading

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 22:47:03 +00:00
Claude Bot
a3d4aa0a6a docs: clarify that replacer parameter is not implemented in Bun.YAML.stringify
Add note that the replacer parameter currently has no effect.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 05:31:40 +00:00
Claude Bot
3408c70760 Merge remote-tracking branch 'origin/main' into claude/fix-yaml-docs 2025-11-09 05:25:14 +00:00
Claude Bot
0f585a8529 Fix CRLF to LF line endings in yaml.mdx
Convert line endings from CRLF to LF to match repository standards and prevent unnecessary diffs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 05:24:47 +00:00
Michael H
267e6c83c8 Merge branch 'main' into claude/fix-yaml-docs 2025-11-09 16:17:13 +11:00
autofix-ci[bot]
26622deb19 [autofix.ci] apply automated fixes (attempt 3/3) 2025-11-08 05:14:34 +00:00
autofix-ci[bot]
e51920576a [autofix.ci] apply automated fixes (attempt 2/3) 2025-11-08 05:12:10 +00:00
autofix-ci[bot]
1a4cc6d650 [autofix.ci] apply automated fixes 2025-11-08 05:10:31 +00:00
Michael H
2f03ee849b ?? 2025-11-08 16:07:53 +11:00
autofix-ci[bot]
7251fef2ff [autofix.ci] apply automated fixes (attempt 2/3) 2025-11-08 05:07:30 +00:00
autofix-ci[bot]
71e5f74f9b [autofix.ci] apply automated fixes 2025-11-08 05:05:53 +00:00
Michael H
c35e6a0f52 . 2025-11-08 16:04:06 +11:00
autofix-ci[bot]
3184405763 [autofix.ci] apply automated fixes (attempt 2/3) 2025-11-08 05:03:48 +00:00
autofix-ci[bot]
7010ec7df6 [autofix.ci] apply automated fixes 2025-11-08 05:02:13 +00:00
Claude Bot
32a5b0788a 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 compact flow style
- Add TypeScript section in Module Import explaining manual .d.ts requirement
- Clarify that unlike JSON, TypeScript doesn't automatically type YAML imports
- Show comprehensive example with nested objects, arrays, and objects in arrays
- Demonstrate YAML anchors & aliases with shared object references
- Show round-trip conversion verification with deepEqual
- Fix heading structure: parse examples under parse section, stringify as separate section
- Use actual tested Bun output for all examples

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 05:00:42 +00:00

View File

@@ -5,7 +5,7 @@ description: Use Bun's built-in support for YAML files through both runtime APIs
In Bun, YAML is a first-class citizen alongside JSON and TOML. You can:
- Parse YAML strings with `Bun.YAML.parse`
- Parse YAML strings with `Bun.YAML.parse` and stringify objects with `Bun.YAML.stringify`
- `import` & `require` YAML files as modules at runtime (including hot reloading & watch mode support)
- `import` & `require` YAML files in frontend apps via bun's bundler
@@ -23,9 +23,7 @@ Bun's YAML parser currently passes over 90% of the official YAML test suite. Whi
Parse a YAML string into a JavaScript object.
```ts
import { YAML } from "bun";
const text = `
```txt
name: John Doe
age: 30
email: john@example.com
@@ -33,16 +31,16 @@ hobbies:
- reading
- coding
- hiking
`;
```
```ts
import { YAML } from "bun";
const data = YAML.parse(text);
console.log(data);
// {
// name: "John Doe",
// age: 30,
// email: "john@example.com",
// hobbies: ["reading", "coding", "hiking"]
// }
```
```txt
{ name: "John Doe", age: 30, email: "john@example.com", hobbies: ["reading", "coding", "hiking"]}
```
#### Multi-document YAML
@@ -121,6 +119,50 @@ try {
}
```
### `Bun.YAML.stringify()`
Convert a JavaScript object into a YAML string.
```ts
import { YAML } from "bun";
const obj = {
name: "John Doe",
age: 30,
email: "john@example.com",
hobbies: ["reading", "coding", "hiking"],
};
const yaml = Bun.YAML.stringify(obj, null, 2);
console.log(yaml);
// name: John Doe
// age: 30
// email: john@example.com
// hobbies:
// - reading
// - coding
// - hiking
```
The signature is similar to `JSON.stringify()`:
```ts
Bun.YAML.stringify(value, replacer?, space?)
```
<Note>The `replacer` parameter is currently not implemented and has no effect.</Note>
The `space` parameter controls indentation (default: 2). With `space: 0`, YAML outputs in compact flow style on a single line:
```ts
const compact = Bun.YAML.stringify(obj, null, 0);
console.log(compact);
```
```txt
{ name: "John Doe", age: 30, email: "john@example.com", hobbies: ["reading", "coding", "hiking"]}
```
---
## Module Import
@@ -193,11 +235,35 @@ const { database, redis } = require("./config.yaml");
console.log(database.port); // 5432
```
### TypeScript
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:
```ts config.yaml.d.ts icon="/icons/typescript.svg"
const contents: {
database: {
host: string;
port: number;
name: string;
};
server: {
port: number;
timeout: number;
};
features: {
auth: boolean;
rateLimit: boolean;
};
};
export = contents;
```
---
## Hot Reloading with YAML
One of the most powerful features of Bun's YAML support is hot reloading. When you run your application with `bun --hot`, changes to YAML files are automatically detected and reloaded without closing connections
One of the most powerful features of Bun's YAML support is hot reloading. When you run your application with `bun --hot`, changes to YAML files are automatically detected and reloaded without restarting your application or closing existing connections.
### Configuration Hot Reloading
@@ -435,6 +501,79 @@ if (parseConfig(migrations).autoRun === "true") {
}
```
### Generating YAML Files
You can use `Bun.YAML.stringify()` to programmatically create YAML configuration files. It handles complex data structures including nested objects, arrays, and objects within arrays. When you reference the same object multiple times, YAML automatically creates anchors (`&`) and aliases (`*`) to avoid duplication:
<CodeGroup>
```ts generate-config.ts icon="/icons/typescript.svg"
import { YAML } from "bun";
const defaults = {
timeout: 30000,
retries: 3,
};
const config = {
defaults,
development: {
defaults,
host: "localhost",
port: 3000,
},
production: {
defaults,
host: "api.example.com",
port: 443,
},
servers: [
{ name: "server1", url: "https://api1.example.com" },
{ name: "server2", url: "https://api2.example.com" },
],
features: {
enabled: ["auth", "logging", "metrics"],
disabled: ["experimental"],
},
};
// Generate readable YAML with 2-space indentation
const yaml = Bun.YAML.stringify(config, null, 2);
await Bun.write("config.yaml", yaml);
// The output is valid YAML that parses back to the original object
import { deepEqual } from "node:assert";
deepEqual(config, Bun.YAML.parse(yaml));
```
```yaml output.yaml icon="file-code"
defaults: &defaults
timeout: 30000
retries: 3
development:
defaults: *defaults
host: localhost
port: 3000
production:
defaults: *defaults
host: api.example.com
port: 443
servers:
- name: server1
url: https://api1.example.com
- name: server2
url: https://api2.example.com
features:
enabled:
- auth
- logging
- metrics
disabled:
- experimental
```
</CodeGroup>
### Bundler Integration
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: