Back to Rspack

JSON

website/docs/en/guide/tech/json.mdx

2.0.1943 B
Original Source

import { ApiMeta, Stability } from '@components/ApiMeta.tsx';

JSON

Rspack has built-in support for JSON, and you can import JSON files directly.

Default import

json
{
  "foo": "bar"
}
ts
import json from './example.json';
console.log(json.foo); // "bar"

Named import

In non-.mjs non-strict ESM files, you can directly import properties from JSON.

json
{
  "foo": "bar"
}
ts
import { foo } from './example.json';
console.log(foo); // "bar"

Import attributes

<ApiMeta addedVersion={'1.0.0-beta.1'} />

Rspack supports import attributes, and you can use import attributes to import JSON:

ts
import json from './example.json' with { type: 'json' };
import('./example.json', { with: { type: 'json' } });