Skip to content

Commit 53a7644

Browse files
docs(assets): add assets, json, svgr usage document (#741)
Co-authored-by: Timeless0911 <[email protected]>
1 parent 8d3ac10 commit 53a7644

File tree

13 files changed

+1043
-6
lines changed

13 files changed

+1043
-6
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/en/config/rsbuild/output.mdx

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

55
Options for build outputs.
66

7-
{/* ## output.assetPrefix <RsbuildDocBadge path="/config/output/asset-prefix" text="output.assetPrefix" /> */}
7+
## output.assetPrefix <RsbuildDocBadge path="/config/output/asset-prefix" text="output.assetPrefix" />
88

9-
{/* In [production mode](https://rsbuild.dev/config/mode), use this option to set the URL prefix for static assets, such as setting it to a CDN URL. */}
9+
Use this option to set the URL prefix for static assets, such as setting it to a CDN URL.
10+
11+
When [format](/config/lib/format) is set to `cjs` or `esm`, Rslib defaults to setting `output.assetPrefix` to `"auto"`.
1012

1113
## output.charset <RsbuildDocBadge path="/config/output/charset" text="output.charset" />
1214

@@ -28,6 +30,8 @@ For custom CSS Modules configuration.
2830

2931
Set the size threshold to inline static assets such as images and fonts.
3032

33+
When [format](/config/lib/format) is set to `cjs` or `esm`, Rslib defaults to setting `output.dataUriLimit` to `0`, without inlining any static assets, so that build tools on the application side can handle and optimize them.
34+
3135
## output.distPath <RsbuildDocBadge path="/config/output/dist-path" text="output.distPath" />
3236

3337
Set the directory of the dist files. Rsbuild will output files to the corresponding subdirectory according to the file type.

website/docs/en/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,106 @@
1+
# Import JSON files
2+
3+
Rslib supports import JSON files in code.
4+
5+
## JSON file
6+
7+
You can directly import JSON files in JavaScript files.
8+
9+
:::warning
10+
11+
In bundle mode, JSON files support both default and named import.
12+
13+
In bundleless mode, JSON files only support named import.
14+
15+
:::
16+
17+
### Default import
18+
19+
```json title="example.json"
20+
{
21+
"name": "foo",
22+
"items": [1, 2]
23+
}
24+
```
25+
26+
```js title="index.js"
27+
import example from './example.json';
28+
29+
console.log(example.name); // 'foo';
30+
console.log(example.items); // [1, 2];
31+
```
32+
33+
### Named import
34+
35+
Rslib also supports importing JSON files through named import.
36+
37+
Here is an example, assuming the source code is as follows:
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+
Based on the configuration in the [output structure](/guide/basic/output-structure) specified in the configuration file, the following outputs will be emitted:
64+
65+
<Tabs>
66+
<Tab label="bundle">
67+
<Tabs>
68+
<Tab label="dist/index.js">
69+
70+
```tsx
71+
var example_namespaceObject = {
72+
u: 'foo',
73+
};
74+
console.log(example_namespaceObject.u);
75+
```
76+
77+
</Tab>
78+
</Tabs>
79+
</Tab>
80+
81+
<Tab label="bundleless">
82+
83+
<Tabs>
84+
<Tab label="dist/index.js">
85+
86+
```tsx
87+
import * as example from './example.js';
88+
89+
console.log(example.name);
90+
```
91+
92+
</Tab>
93+
<Tab label="dist/example.js">
94+
95+
```tsx
96+
var example_namespaceObject = JSON.parse('{"name":"foo","items":[1,2]}');
97+
var __webpack_exports__items = example_namespaceObject.items;
98+
var __webpack_exports__name = example_namespaceObject.name;
99+
export { __webpack_exports__items as items, __webpack_exports__name as name };
100+
```
101+
102+
</Tab>
103+
</Tabs>
104+
</Tab>
105+
106+
</Tabs>

0 commit comments

Comments
 (0)