Skip to content

Commit ec7bdec

Browse files
committed
docs: update the doc
1 parent ebe23ee commit ec7bdec

File tree

8 files changed

+524
-4
lines changed

8 files changed

+524
-4
lines changed

tests/integration/asset/__snapshots__/index.test.ts.snap

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ export { draft_rslib_entry_namespaceObject as default };
1313
`;
1414

1515
exports[`use json 1`] = `
16-
"JSON.parse('{"foo":1,"bar":["2"]}');
16+
"var data_namespaceObject = {
17+
R: 1,
18+
K: [
19+
"2"
20+
]
21+
};
22+
console.log(data_namespaceObject.R);
23+
console.log(data_namespaceObject.K);
1724
"
1825
`;
1926

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import a from './assets/data.json';
1+
import { bar, foo } from './assets/data.json';
22

3-
a;
3+
console.log(foo);
4+
console.log(bar);

website/docs/zh/config/rsbuild/output.mdx

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
44

55
与构建产物相关的选项。
66

7+
## output.assetPrefix <RsbuildDocBadge path="/config/output/asset-prefix" text="output.charset" />
8+
9+
使用该选项设置静态资源的 URL 前缀,比如设置为 CDN 地址。
10+
11+
[format](/config/lib/format)`cjs``esm` 时,Rslib 默认会将 `output.assetPrefix` 设置为 `"auto"`
12+
713
## output.charset <RsbuildDocBadge path="/config/output/charset" text="output.charset" />
814

915
指定输出文件的 [字符编码](https://developer.mozilla.org/en-US/docs/Glossary/Character_encoding),以确保它们在不同的环境中能够正确显示。
@@ -24,6 +30,8 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
2430

2531
设置图片、字体、媒体等静态资源被自动内联为 base64 的体积阈值。
2632

33+
[format](/config/lib/format)`cjs``esm` 时,Rslib 默认会将 `output.dataUriLimit` 设置为 `0`,不内联任何静态资源,以便于应用侧的构建工具处理和优化。
34+
2735
## output.distPath <RsbuildDocBadge path="/config/output/dist-path" text="output.distPath" />
2836

2937
设置构建产物的输出目录,Rsbuild 会根据产物的类型输出到对应的子目录下。

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

+3
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
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
```js
44+
import { name } from './example.json';
45+
46+
console.log(name); // 'foo';
47+
48+
````
49+
</Tab>
50+
<Tab label="src/example.json">
51+
```json title="example.json"
52+
{
53+
"name": "foo",
54+
"items": [1, 2]
55+
}
56+
````
57+
58+
</Tab>
59+
</Tabs>
60+
61+
会根据配置文件中的 [产物结构](/guide/basic/output-structure) 配置,打包出如下产物:
62+
63+
<Tabs>
64+
<Tab label="Bundle">
65+
<Tabs>
66+
<Tab label="dist/index.js">
67+
```tsx
68+
var example_namespaceObject = {
69+
"name": "foo"
70+
};
71+
console.log(example_namespaceObject.name);
72+
```
73+
74+
</Tab>
75+
</Tabs>
76+
</Tab>
77+
78+
<Tab label="Bundleless">
79+
80+
<Tabs>
81+
<Tab label="dist/index.js">
82+
83+
```tsx
84+
import * as example from './example.js';
85+
86+
console.log(example.name);
87+
```
88+
89+
</Tab>
90+
<Tab label="dist/example.js">
91+
```tsx
92+
var example_namespaceObject = JSON.parse('{"name":"foo","items":[1,2]}');
93+
var __webpack_exports__items = example_namespaceObject.items;
94+
var __webpack_exports__name = example_namespaceObject.name;
95+
export { __webpack_exports__items as items, __webpack_exports__name as name };
96+
```
97+
</Tab>
98+
</Tabs>
99+
</Tab>
100+
101+
</Tabs>

0 commit comments

Comments
 (0)