Skip to content

Bun 은 .yaml.yml 가져오기를 기본적으로 지원합니다.

yaml
database:
  host: localhost
  port: 5432
  name: myapp

server:
  port: 3000
  timeout: 30

features:
  auth: true
  rateLimit: true

다른 소스 파일과 마찬가지로 파일을 가져오세요.

ts
import config from "./config.yaml";

config.database.host; // => "localhost"
config.server.port; // => 3000
config.features.auth; // => true

named imports 를 사용하여 최상위 속성을 구조 분해할 수도 있습니다.

ts
import { database, server, features } from "./config.yaml";

console.log(database.name); // => "myapp"
console.log(server.timeout); // => 30
console.log(features.rateLimit); // => true

Bun 은 또한 Import Attributes 구문을 지원합니다.

ts
import config from "./config.yaml" with { type: "yaml" };

config.database.port; // => 5432

런타임에 YAML 문자열을 구문 분석하려면 Bun.YAML.parse() 를 사용하세요.

ts
const yamlString = `
name: John Doe
age: 30
hobbies:
  - reading
  - coding
`;

const data = Bun.YAML.parse(yamlString);
console.log(data.name); // => "John Doe"
console.log(data.hobbies); // => ["reading", "coding"]

TypeScript 지원

YAML 가져오기에 대한 TypeScript 지원을 추가하려면 YAML 파일명에 .d.ts 가 추가된 선언 파일을 만드세요 (예: config.yamlconfig.yaml.d.ts).

ts
const contents: {
  database: {
    host: string;
    port: number;
    name: string;
  };
  server: {
    port: number;
    timeout: number;
  };
  features: {
    auth: boolean;
    rateLimit: boolean;
  };
};

export = contents;

Bun 의 YAML 지원에 대한 전체 문서는 문서 > API > YAML 을 참조하세요.

Bun by www.bunjs.com.cn 편집