Skip to content

Commit 0d5e22c

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

File tree

12 files changed

+536
-36
lines changed

12 files changed

+536
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"items": [1, 2]
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
import type React from 'react';
2-
import { CounterButton } from './components/CounterButton';
3-
import { useCounter } from './useCounter';
4-
import './index.scss';
1+
import { name } from './example.json';
52

6-
export const Counter: React.FC = () => {
7-
const { count, increment, decrement } = useCounter();
8-
9-
return (
10-
<div>
11-
<h1 className="counter-title">React</h1>
12-
<h2 className="counter-text">Counter: {count}</h2>
13-
<CounterButton onClick={decrement} label="-" />
14-
<CounterButton onClick={increment} label="+" />
15-
</div>
16-
);
17-
};
3+
console.log(name); // 'foo';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"items": [1, 2]
4+
}
+2-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
import type React from 'react';
2-
import { CounterButton } from './components/CounterButton';
3-
import { useCounter } from './useCounter';
4-
import './index.scss';
1+
import { name } from './example.json';
52

6-
export const Counter: React.FC = () => {
7-
const { count, increment, decrement } = useCounter();
8-
9-
return (
10-
<div>
11-
<h1 className="counter-title">React</h1>
12-
<h2 className="counter-text">Counter: {count}</h2>
13-
<CounterButton onClick={decrement} label="-" />
14-
<CounterButton onClick={increment} label="+" />
15-
</div>
16-
);
17-
};
3+
console.log(name); // 'foo';

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+
36+
Rslib 同样支持通过 named import 来引用 JSON 文件:
37+
38+
下面是一个使用示例,假设源码如下:
39+
40+
import { Tabs, Tab } from '@theme';
41+
42+
<Tabs>
43+
<Tab label="src/index.ts">
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+
```json title="example.json"
53+
{
54+
"name": "foo",
55+
"items": [1, 2]
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)