Skip to content

Commit cc5ff2e

Browse files
SoonIterTimeless0911
authored andcommitted
docs: complete en
1 parent 551bfb5 commit cc5ff2e

File tree

4 files changed

+508
-0
lines changed

4 files changed

+508
-0
lines changed

website/docs/en/guide/advanced/_meta.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"third-party-deps",
33
"output-compatibility",
44
"dts",
5+
"static-assets",
6+
"svgr-files",
7+
"json-files",
58
"module-federation",
69
"storybook"
710
]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# 引用 JSON 文件
2+
3+
Rslib 支持在代码中引用 JSON 文件。
4+
5+
## JSON 文件
6+
7+
你可以直接在 JavaScript 文件中引用 JSON 文件。
8+
9+
### 默认引用
10+
11+
```json title="example.json"
12+
{
13+
"name": "foo",
14+
"items": [1, 2]
15+
}
16+
```
17+
18+
```js title="index.js"
19+
import example from './example.json';
20+
21+
console.log(example.name); // 'foo';
22+
console.log(example.items); // [1, 2];
23+
```
24+
25+
:::warning
26+
27+
在 Bundle 模式下,JSON 文件支持默认引用和具名引用。
28+
29+
在 Bundleless 模式下,JSON 文件仅支持具名引用。
30+
31+
:::
32+
33+
### 具名引用
34+
35+
Rslib 同样支持通过 named import 来引用 JSON 文件:
36+
37+
下面是一个使用示例,假设源码如下:
38+
39+
import { Tabs, Tab } from '@theme';
40+
41+
<Tabs>
42+
<Tab label="src/index.ts">
43+
44+
```js
45+
import { name } from './example.json';
46+
47+
console.log(name); // 'foo';
48+
```
49+
50+
</Tab>
51+
<Tab label="src/example.json">
52+
53+
```json
54+
{
55+
"name": "foo",
56+
"items": [1, 2]
57+
}
58+
```
59+
60+
</Tab>
61+
</Tabs>
62+
63+
会根据配置文件中的 [产物结构](/guide/basic/output-structure) 配置,打包出如下产物:
64+
65+
<Tabs>
66+
<Tab label="Bundle">
67+
<Tabs>
68+
<Tab label="dist/index.js">
69+
```tsx
70+
var example_namespaceObject = {
71+
"name": "foo"
72+
};
73+
console.log(example_namespaceObject.name);
74+
```
75+
76+
</Tab>
77+
</Tabs>
78+
</Tab>
79+
80+
<Tab label="Bundleless">
81+
82+
<Tabs>
83+
<Tab label="dist/index.js">
84+
85+
```tsx
86+
import * as example from './example.js';
87+
88+
console.log(example.name);
89+
```
90+
91+
</Tab>
92+
<Tab label="dist/example.js">
93+
```tsx
94+
var example_namespaceObject = JSON.parse('{"name":"foo","items":[1,2]}');
95+
var __webpack_exports__items = example_namespaceObject.items;
96+
var __webpack_exports__name = example_namespaceObject.name;
97+
export { __webpack_exports__items as items, __webpack_exports__name as name };
98+
```
99+
</Tab>
100+
</Tabs>
101+
</Tab>
102+
103+
</Tabs>

0 commit comments

Comments
 (0)