Skip to content

Commit 1f66caa

Browse files
authored
docs: enhance output.assetPrefix (#866)
1 parent 10dafaa commit 1f66caa

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

tests/integration/style/sass/bundle-false/rslib.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,5 @@ export default defineConfig({
3939
],
4040
output: {
4141
target: 'web',
42-
assetPrefix: 'auto',
4342
},
4443
});

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ Options for build outputs.
88

99
Use this option to set the URL prefix for static assets, such as setting it to a CDN URL.
1010

11-
When [format](/config/lib/format) is set to `cjs` or `esm`, Rslib defaults to setting `output.assetPrefix` to `"auto"`.
11+
In Rslib, the default value for this option depends on [format](/config/lib/format):
12+
13+
- When `format` is `cjs` or `esm`, the default value is `"auto"`.
14+
- When `format` is `mf` or `umd`, the default value is `"/"`.
15+
16+
When `output.assetPrefix` is set to `"auto"`, Rslib defaults to setting [importMode](https://rspack.dev/config/module#modulegeneratorassetimportmode) to `"preserve"`, which preserves the `import` or `require` statements for static assets in JavaScript files. Additionally, the static asset references in CSS files remain as relative paths, see [Import static assets](/guide/advanced/static-assets) for more details.
17+
18+
When `output.assetPrefix` is set to a specific path, the static asset import statements in JavaScript files will no longer be preserved and will be replaced with URLs prefixed by that path. Additionally, the static asset paths in CSS files will be substituted with paths that include this prefix.
1219

1320
## output.charset <RsbuildDocBadge path="/config/output/charset" text="output.charset" />
1421

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ The following are the formats supported by Rslib by default:
1313

1414
To import assets in other formats, refer to [Extend Asset Types](#extend-asset-types).
1515

16-
## Import assets in JS file
16+
## Import assets in JavaScript file
1717

18-
In JS files, you can directly import static assets with relative paths through `import`:
18+
In JavaScript files, you can directly import static assets with relative paths through `import`:
1919

2020
```tsx
2121
// Import the logo.png image in the 'src/assets' directory
@@ -36,7 +36,7 @@ console.log(logo); // "/static/logo.[hash].png"
3636
export default = () => <img src={logo} />;
3737
```
3838

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 and transforms the source file into a JS file and a static asset file that is emitted according to [output.distPath](/config/rsbuild/output#outputdistpath) by default, which is used to preserve the `import` statements for static assets.
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 and transforms the source file into a JavaScript file and a static asset file that is emitted according to [output.distPath](/config/rsbuild/output#outputdistpath) by default with preserving the `import` or `require` statements for static assets.
4040

4141
The following is an example of usage, assuming the source code is as follows:
4242

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
88

99
使用该选项设置静态资源的 URL 前缀,比如设置为 CDN 地址。
1010

11-
[format](/config/lib/format)`cjs``esm` 时,Rslib 默认会将 `output.assetPrefix` 设置为 `"auto"`
11+
在 Rslib 中,该选项的默认值由 [format](/config/lib/format) 决定:
12+
13+
-`format``cjs``esm` 时,默认值为 `"auto"`
14+
-`format``mf``umd` 时,默认值为 `"/"`
15+
16+
`output.assetPrefix` 设置为 `"auto"` 时,Rslib 会自动将 [importMode](https://rspack.dev/zh/config/module#modulegeneratorassetimportmode) 设置为 `"preserve"`,以保留 JavaScript 文件中引用静态资源的 `import``require` 语句。同时,CSS 文件中的静态资源会保留相对引用路径,详情可以查看 [引用静态资源](/guide/advanced/static-assets)
17+
18+
`output.assetPrefix` 设置为具体的路径时,JavaScript 文件中引用静态资源的 `import``require` 语句会被替换为包含该前缀的 URL。同时,CSS 文件中的静态资源会被直接替换为带有该前缀的路径。
1219

1320
## output.charset <RsbuildDocBadge path="/config/output/charset" text="output.charset" />
1421

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Rslib 支持在代码中引用图片、字体、音频、视频等类型的静
1313

1414
如果你需要引用其他格式的静态资源,请参考 [扩展静态资源类型](#扩展静态资源类型)
1515

16-
## JS 文件中引用
16+
## JavaScript 文件中引用
1717

18-
JS 文件中,可以直接通过 `import` 的方式引用相对路径下的静态资源:
18+
JavaScript 文件中,可以直接通过 `import` 的方式引用相对路径下的静态资源:
1919

2020
```tsx
2121
// 引用 src/assets 目录下的 logo.png 图片
@@ -36,7 +36,7 @@ console.log(logo); // "/static/logo.[hash].png"
3636
export default = () => <img src={logo} />;
3737
```
3838

39-
[format](/config/lib/format)`cjs``esm` 时,Rslib 将产物视为会被其他打包工具再次消费的中间产物,默认会在代码转换时将源文件转化为一个 JS 文件和一个根据 [output.distPath](/config/rsbuild/output#outputdistpath) 输出的静态资源文件,从而保留引用静态资源的 `import` 语句。
39+
[format](/config/lib/format)`cjs``esm` 时,Rslib 将产物视为会被其他打包工具再次消费的中间产物,默认会在代码转换时将源文件转化为一个 JavaScript 文件和一个根据 [output.distPath](/config/rsbuild/output#outputdistpath) 输出的静态资源文件,并保留引用静态资源的 `import``require` 语句。
4040

4141
下面是一个使用示例,假设源码如下:
4242

0 commit comments

Comments
 (0)