Skip to content

Commit 5cde6d1

Browse files
docs: add how to import absolute path in CSS files (#3816)
Co-authored-by: neverland <[email protected]>
1 parent 650004c commit 5cde6d1

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ Import with [alias](/guide/advanced/alias) are also supported:
6666
}
6767
```
6868

69+
If you want to reference static assets in absolute paths in CSS files:
70+
71+
```css
72+
@font-face {
73+
font-family: DingTalk;
74+
src: url('/image/font/foo.ttf');
75+
}
76+
```
77+
78+
By default, the built-in `css-loader` in Rsbuild 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/tools/css-loader#toolscssloader) to filter out the specified paths. The filtered paths are left as they are in the code.
79+
80+
```ts
81+
export default {
82+
tools: {
83+
cssLoader: {
84+
url: {
85+
filter: (url) => {
86+
if (/\/image\/font/.test(url)) {
87+
return false;
88+
}
89+
return true;
90+
},
91+
},
92+
},
93+
},
94+
};
95+
```
96+
6997
## Import Results
7098

7199
The result of importing static assets depends on the file size:

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ export default = () => <img src={logo} />;
6666
}
6767
```
6868

69+
如果需要在 CSS 文件中引用绝对路径下的静态资源:
70+
71+
```css
72+
@font-face {
73+
font-family: DingTalk;
74+
src: url('/image/font/foo.ttf');
75+
}
76+
```
77+
78+
默认情况下,Rsbuild 内置的 `css-loader` 会解析 `url()` 中的绝对路径并寻找指定的模块。如果你希望跳过绝对路径的解析,可以配置 [`tools.cssLoader`](/config/tools/css-loader#toolscssloader) 来过滤指定的路径,被过滤的路径将被原样保留在代码中。
79+
80+
```ts
81+
export default {
82+
tools: {
83+
cssLoader: {
84+
url: {
85+
filter: (url) => {
86+
if (/\/image\/font/.test(url)) {
87+
return false;
88+
}
89+
return true;
90+
},
91+
},
92+
},
93+
},
94+
};
95+
```
96+
6997
## 引用结果
7098

7199
引用静态资源的结果取决于文件体积:

0 commit comments

Comments
 (0)