Skip to content

Commit 49f2198

Browse files
SoonIterTimeless0911
authored andcommitted
docs: complete en static assets
1 parent cc5ff2e commit 49f2198

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

website/docs/en/guide/advanced/static-assets.mdx

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
# Import static assets
22

3-
Rslib 支持在代码中引用图片、字体、音频、视频等类型的静态资源。
3+
Rslib supports import static assets, including images, fonts, audio and video.
44

5-
## 静态资源格式
5+
## Assets format
66

7-
以下是 Rslib 默认支持的静态资源格式:
7+
The following are the formats supported by Rslib by default:
88

9-
- **图片**pngjpgjpeggifsvgbmpwebpicoapngaviftiftiffjfifpjpegpjpcur
10-
- **字体**woffwoff2eotttfotfttc
11-
- **音频**mp3wavflacaacm4aopus
12-
- **视频**mp4webmoggmov
9+
- **images**: png, jpg, jpeg, gif, svg, bmp, webp, ico, apng, avif, tif, tiff, jfif, pjpeg, pjp, cur.
10+
- **fonts**: woff, woff2, eot, ttf, otf, ttc.
11+
- **audio**: mp3, wav, flac, aac, m4a, opus.
12+
- **video**: mp4, webm, ogg, mov.
1313

14-
如果你需要引用其他格式的静态资源,请参考 [扩展静态资源类型](#扩展静态资源类型)
14+
To import assets in other formats, refer to [Extend Asset Types](#extend-asset-types).
1515

16-
## JS 文件中引用
16+
## Import assets in JS file
1717

18-
JS 文件中,可以直接通过 `import` 的方式引用相对路径下的静态资源:
18+
In JS files, you can directly refer to static assets with relative paths through `import`:
1919

2020
```tsx
21-
// 引用 src/assets 目录下的 logo.png 图片
21+
// Import the logo.png image in the 'src/assets' directory
2222
import logo from './assets/logo.png';
2323

2424
console.log(logo); // "/static/logo.[hash].png"
2525

2626
export default = () => <img src={logo} />;
2727
```
2828

29-
也可以使用**路径别名**来引用:
29+
Import with **alias** is also available:
3030

3131
```tsx
3232
import logo from '@/assets/logo.png';
@@ -36,9 +36,9 @@ console.log(logo); // "/static/logo.[hash].png"
3636
export default = () => <img src={logo} />;
3737
```
3838

39-
[format](/config/lib/format) `cjs` `esm` 时,Rslib 将产物视为会被其他打包工具再次消费的中间产物。Rslib 会在代码转换时将源文件转化为一个 JS 文件和一个根据 [output.distPath](/config/rsbuild/output#outputdistpath) 输出的静态资源文件,用于保留对静态资源的 `import` 语句。
39+
When the [format](/config/lib/format) is set to `cjs` or `esm`, Rslib treats the output as an mid-level artifact that will be consumed by other build tools again. During the code transformation, Rslib transforms the source file into a JS file and a static asset file that is output according to [output.distPath](/config/rsbuild/output#outputdistpath), which is used to preserve the `import` statements for static assets.
4040

41-
下面是一个使用示例,假设源码如下:
41+
The following is an example of usage, assuming the source code is as follows:
4242

4343
import { Tabs, Tab } from '@theme';
4444

@@ -57,7 +57,7 @@ console.log(logo);
5757
</Tab>
5858
</Tabs>
5959

60-
会根据配置文件中的 [产物结构](/guide/basic/output-structure) 配置,打包出如下产物:
60+
Based on the configuration in the [output structure](/guide/basic/output-structure) in the configuration file, the following outputs will be packaged:
6161

6262
<Tabs>
6363
<Tab label="Bundle">
@@ -105,27 +105,27 @@ export { logo_rslib_entry_namespaceObject as default };
105105

106106
</Tabs>
107107

108-
## CSS 文件中引用
108+
## Import assets in CSS file
109109

110-
CSS 文件中,可以引用相对路径下的静态资源:
110+
In CSS files, you can import static assets with relative paths:
111111

112112
```css title="src/index.css"
113113
.logo {
114114
background-image: url('./assets/logo.png');
115115
}
116116
```
117117

118-
也支持使用**路径别名**来引用:
118+
Import with **alias** are also supported:
119119

120120
```css title="src/index.css"
121121
.logo {
122122
background-image: url('@/assets/logo.png');
123123
}
124124
```
125125

126-
[format](/config/lib/format) `cjs` `esm` 时,Rslib 将产物视为会被其他打包工具再次消费的中间产物。Rslib 会通过设置 [output.assetPrefix](/config/rsbuild/output#outputassetprefix) `"auto"` 来使 CSS 产物中保留相对引用路径。
126+
When the [format](/config/lib/format) is `cjs` or `esm`, Rslib treats the output as an intermediate product that will be consumed again by other packaging tools. Rslib preserves relative reference paths in CSS outputs by setting [output.assetPrefix](/config/rsbuild/output#outputassetprefix) to `"auto"`.
127127

128-
下面是一个使用示例,假设源码如下:
128+
The following is an example of usage, assuming the source code is as follows:
129129

130130
<Tabs>
131131
<Tab label="src/index.css">
@@ -147,7 +147,7 @@ export { logo_rslib_entry_namespaceObject as default };
147147
</Tab>
148148
</Tabs>
149149

150-
会打包出如下产物:
150+
The following output will be packaged:
151151

152152
<Tabs>
153153

@@ -174,9 +174,9 @@ export { logo_rslib_entry_namespaceObject as default };
174174

175175
---
176176

177-
### 在 CSS 中忽略某些文件引用
177+
### Ignore some assets imported in CSS
178178

179-
如果需要在 CSS 文件中引用绝对路径下的静态资源:
179+
If you need to import a static asset with an absolute path in a CSS file:
180180

181181
```css
182182
@font-face {
@@ -185,7 +185,7 @@ export { logo_rslib_entry_namespaceObject as default };
185185
}
186186
```
187187

188-
默认情况下,Rslib 内置的 `css-loader` 会解析 `url()` 中的绝对路径并寻找指定的模块。如果你希望跳过绝对路径的解析,可以配置 [`tools.cssLoader`](/config/rsbuild/tools#toolscssloader) 来过滤指定的路径,被过滤的路径将被原样保留在代码中。
188+
By default, the built-in `css-loader` in Rslib will resolve absolute paths in `url()` and look for the specified modules. If you want to skip resolving absolute paths, you can configure [`tools.cssLoader`](/config/rsbuild/tools#toolscssloader) to filter out the specified paths. The filtered paths are left as they are in the code.
189189

190190
```ts
191191
export default {
@@ -204,54 +204,54 @@ export default {
204204
};
205205
```
206206

207-
## 静态资源内联
207+
## Inline static assets
208208

209-
在开发组件库这种会被其他打包工具再次消费的中间产物时,一般不会内联静态资源,交给应用侧的构建工具处理和优化。
209+
When developing libraries that will be consumed again by other build tools as intermediate products, static assets are typically not inlined, leaving the handling and optimization to the build tools on the application side.
210210

211-
自动内联的体积阈值可以通过 [output.dataUriLimit](/config/rsbuild/output#outputdataurilimit) 配置项修改,在 [format](/config/lib/format) `cjs` `esm` 时,Rslib 默认会将 `output.dataUriLimit` 设置为 `0`,不内联任何静态资源。
211+
The automatic inlining size threshold can be modified via the [output.dataUriLimit](/config/rsbuild/output#outputdataurilimit) configuration option. When the [format](/config/lib/format) is `cjs` or `esm`, Rslib defaults the `output.dataUriLimit` to `0`, not inlining any static assets.
212212

213-
## 构建产物目录
213+
## Build Output Directory
214214

215-
当静态资源被引用后,会自动被输出到构建产物的目录下,你可以:
215+
Once static assets are referenced, they will automatically be output to the build output directory. You can:
216216

217-
- 通过 [output.filename](/config/rsbuild/output#outputfilename) 来修改产物的文件名。
218-
- 通过 [output.distPath](/config/rsbuild/output#outputdistpath) 来修改产物的输出路径。
217+
- Modify the filename of the output through [output.filename](/config/rsbuild/output#outputfilename).
218+
- Change the output path of the product through [output.distPath](/config/rsbuild/output#outputdistpath).
219219

220-
## 类型声明
220+
## Type declaration
221221

222-
当你在 TypeScript 代码中引用静态资源时,TypeScript 可能会提示该模块缺少类型定义:
222+
When you import static assets in TypeScript code, TypeScript may prompt that the module is missing a type definition:
223223

224224
```
225225
TS2307: Cannot find module './logo.png' or its corresponding type declarations.
226226
```
227227

228-
此时你需要为静态资源添加类型声明文件,请在项目中创建 `src/env.d.ts` 文件,并添加相应的类型声明。
228+
To fix this, you need to add a type declaration file for the static assets, please create a `src/env.d.ts` file, and add the corresponding type declaration.
229229

230-
- 方法一:如果项目里安装了 `@rslib/core` 包,你可以直接引用 `@rslib/core` 提供的**预设类型**
230+
- Method 1: If the `@rslib/core` package is installed, you can reference the **preset types** provided by `@rslib/core`:
231231

232232
```ts
233233
/// <reference types="@rslib/core/types" />
234234
```
235235

236-
- 方法二:手动添加需要的类型声明:
236+
- Method 2: Manually add the required type declarations:
237237

238238
```ts title="src/env.d.ts"
239-
// png 图片为例
239+
// Taking png images as an example
240240
declare module '*.png' {
241-
const url: string;
242-
export default url;
241+
const content: string;
242+
export default content;
243243
}
244244
```
245245

246-
添加类型声明后,如果依然存在上述错误提示,请尝试重启当前 IDE,或者调整 `env.d.ts` 所在的目录,使 TypeScript 能够正确识别类型定义。
246+
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where `env.d.ts` is located, making sure the TypeScript can correctly identify the type definition.
247247

248-
## 扩展静态资源类型
248+
## Extend asset types
249249

250-
如果 Rslib 内置的静态资源类型不能满足你的需求,可以通过以下方式扩展额外的静态资源类型。
250+
If the built-in static assets type of Rslib does not meet your needs, you can extend the additional static assets type in the following ways.
251251

252-
### 使用 `source.assetsInclude`
252+
### Use `source.assetsInclude`
253253

254-
通过 [source.assetsInclude](/config/rsbuild/source#sourceassetsinclude) 配置项,你可以指定需要被视为静态资源的额外文件类型。
254+
By using the [source.assetsInclude](/config/rsbuild/source#sourceassetsinclude) config, you can specify additional file types to be treated as static assets.
255255

256256
```ts title="rslib.config.ts"
257257
export default {
@@ -261,19 +261,19 @@ export default {
261261
};
262262
```
263263

264-
添加以上配置后,你就可以在代码里引用 `*.pdf` 文件了,比如:
264+
After adding the above configuration, you can import `*.pdf` files in your code, for example:
265265

266266
```js
267267
import myFile from './static/myFile.pdf';
268268

269-
console.log(myFile); // "/static/assets/myFile.[hash].pdf"
269+
console.log(myFile);
270270
```
271271

272-
### 使用 `tools.rspack`
272+
### Use `tools.rspack`
273273

274-
可以通过 [tools.rspack](/config/rsbuild/tools#toolsrspack) 来修改内置的 Rspack 配置,并添加自定义的静态资源处理规则。
274+
You can modify the built-in Rspack configuration and add custom static assets handling rules via [tools.rspack](/config/rsbuild/tools#toolsrspack).
275275

276-
比如,把 `*.pdf` 文件当做静态资源输出到产物目录,可以添加以下配置:
276+
For example, to treat `*.pdf` files as assets and output them to the dist directory, you can add the following configuration:
277277

278278
```ts title="rslib.config.ts"
279279
export default {
@@ -294,4 +294,4 @@ export default {
294294
};
295295
```
296296

297-
关于 asset modules 的更多介绍,请参考 [Rspack - Asset modules](https://rspack.dev/guide/features/asset-module)
297+
For more information about asset modules, please refer to [Rspack - Asset modules](https://rspack.dev/guide/features/asset-module).

0 commit comments

Comments
 (0)