For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/tech/json.md.
close

JSON

Rspack 内置了对 JSON 的支持,你可以直接导入 JSON 文件。

默认导入

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

具名导入

在非 .mjs 的非严格 ESM 文件中,你可以直接导入 JSON 中的属性。

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

使用 import attributes

Rspack 支持 import attributes,你可以通过 import attributes 来引入 JSON:

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