Skip to content

Commit e15b92b

Browse files
authored
docs: update PostCSS guide (#4364)
1 parent f69df70 commit e15b92b

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

website/docs/en/guide/basic/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[
22
"cli",
3+
"css-usage",
34
"server",
45
"output-files",
5-
"css-usage",
66
"static-assets",
77
"json-files",
88
"wasm-assets",

website/docs/en/guide/basic/css-usage.mdx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Import CSS files
1+
# CSS
22

33
Rsbuild provides out-of-the-box support for CSS, including PostCSS, CSS Modules, CSS preprocessors, CSS inlining, and CSS compression.
44

@@ -55,19 +55,31 @@ When building for production, Rsbuild enables Rspack's built-in [LightningCssMin
5555

5656
Rsbuild supports transforming CSS code through [PostCSS](https://postcss.org/). You can configure PostCSS in the following ways:
5757

58-
1. Rsbuild uses [postcss-load-config](https://github.com/postcss/postcss-load-config) to load the PostCSS configuration file in the root directory of the current project, such as `postcss.config.js`:
58+
### Configuration file
5959

60-
```js
60+
Rsbuild uses [postcss-load-config](https://github.com/postcss/postcss-load-config) to load the PostCSS configuration file in the root directory of the current project, such as `postcss.config.js`:
61+
62+
```js title="postcss.config.cjs"
6163
module.exports = {
6264
'postcss-px-to-viewport': {
6365
viewportWidth: 375,
6466
},
6567
};
6668
```
6769

68-
2. Configure the postcss-loader through Rsbuild's [tools.postcss](/config/tools/postcss) option, which supports modifying the built-in configuration through a function, for example:
70+
`postcss-load-config` supports multiple file formats, including but not limited to the following file names:
6971

70-
```ts
72+
- postcss.config.js
73+
- postcss.config.mjs
74+
- postcss.config.cjs
75+
- postcss.config.ts
76+
- ...
77+
78+
### tools.postcss
79+
80+
You can also configure the postcss-loader through Rsbuild's [tools.postcss](/config/tools/postcss) option, which supports modifying the built-in configuration through a function, for example:
81+
82+
```ts title="rsbuild.config.ts"
7183
export default {
7284
tools: {
7385
postcss: (opts) => {
@@ -80,6 +92,8 @@ export default {
8092
};
8193
```
8294

95+
### Configuration priority
96+
8397
- When you configure both the `postcss.config.js` file and the `tools.postcss` option, both will take effect, and the `tools.postcss` option will take precedence.
8498
- If there is no `postcss.config.js` file in the project and the `tools.postcss` option is not configured, Rsbuild will not register `postcss-loader`.
8599

website/docs/zh/guide/basic/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[
22
"cli",
3+
"css-usage",
34
"server",
45
"output-files",
5-
"css-usage",
66
"static-assets",
77
"json-files",
88
"wasm-assets",

website/docs/zh/guide/basic/css-usage.mdx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 引用 CSS 文件
1+
# CSS
22

33
Rsbuild 对 CSS 提供开箱即用的支持,包括 Lightning CSS、PostCSS、CSS Modules、CSS 预处理器、CSS 内联和 CSS 压缩。
44

@@ -55,17 +55,29 @@ export default {
5555

5656
Rsbuild 支持通过 [PostCSS](https://postcss.org/) 来转换 CSS 代码。你可以通过以下方式来配置 PostCSS:
5757

58-
1. Rsbuild 使用 [postcss-load-config](https://github.com/postcss/postcss-load-config) 来加载当前项目根目录下的 PostCSS 配置文件,比如 `postcss.config.js`
58+
### 配置文件
5959

60-
```js title="postcss.config.js"
60+
Rsbuild 使用 [postcss-load-config](https://github.com/postcss/postcss-load-config) 来加载当前项目根目录下的 PostCSS 配置文件,比如 `postcss.config.js`
61+
62+
```js title="postcss.config.cjs"
6163
module.exports = {
6264
'postcss-px-to-viewport': {
6365
viewportWidth: 375,
6466
},
6567
};
6668
```
6769

68-
2. 通过 Rsbuild 的 [tools.postcss](/config/tools/postcss) 选项来配置 postcss-loader,该选项支持通过函数来修改内置配置,比如:
70+
`postcss-load-config` 支持多种文件格式,包括但不限于以下文件名:
71+
72+
- postcss.config.js
73+
- postcss.config.mjs
74+
- postcss.config.cjs
75+
- postcss.config.ts
76+
- ...
77+
78+
### tools.postcss
79+
80+
你也可以通过 Rsbuild 的 [tools.postcss](/config/tools/postcss) 选项来配置 postcss-loader,该选项支持通过函数来修改内置配置,比如:
6981

7082
```ts title="rsbuild.config.ts"
7183
export default {
@@ -80,6 +92,8 @@ export default {
8092
};
8193
```
8294

95+
### 配置优先级
96+
8397
- 当你同时配置 `postcss.config.js` 文件和 `tools.postcss` 选项时,两者都会生效,并且 `tools.postcss` 的优先级更高。
8498
- 如果项目中没有 `postcss.config.js` 文件,也没有配置 `tools.postcss` 选项,Rsbuild 将不会注册 `postcss-loader`
8599

0 commit comments

Comments
 (0)