引用 JSON 文件

Rslib 支持在代码中引用 JSON 文件。

JSON 文件

你可以直接在 JavaScript 文件中引用 JSON 文件。

WARNING

在 bundle 模式下,JSON 文件支持默认引用和具名引用。

在 bundleless 模式下,JSON 文件仅支持具名引用。

默认引用

example.json
{
  "name": "foo",
  "items": [1, 2]
}
index.js
import example from './example.json';

console.log(example.name); // 'foo';
console.log(example.items); // [1, 2];

具名引用

Rslib 同样支持通过 named import 来引用 JSON 文件。

下面是一个使用示例,假设源码如下:

src/index.ts
src/example.json
import { name } from './example.json';

console.log(name); // 'foo';

会根据配置文件中的 产物结构 配置,输出如下产物:

bundle
bundleless
dist/index.js
var example_namespaceObject = {
  u: 'foo',
};
console.log(example_namespaceObject.u);