Import JSON files

Rslib supports import JSON files in code.

JSON file

You can directly import JSON files in JavaScript files.

WARNING

In bundle mode, JSON files support both default and named import.

In bundleless mode, JSON files only support named import.

Default import

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];

Named import

Rslib also supports importing JSON files through named import.

Here is an example, assuming the source code is as follows:

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

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

Based on the configuration in the output structure specified in the configuration file, the following outputs will be emitted:

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