Skip to content

Commit b9745db

Browse files
committed
feat: remove tailwindPlugin and add doc
1 parent c8f93d5 commit b9745db

File tree

27 files changed

+786
-132
lines changed

27 files changed

+786
-132
lines changed

packages/document/main-doc/docs/en/guides/basic-features/css/tailwindcss.mdx

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,91 @@
22

33
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
44

5-
To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, you can follow the steps below:
5+
## Using Tailwind CSS in Modern.js
66

7-
1. Run `pnpm run new` in the root directory of your project and make the following selections:
7+
To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, you only need to configure it according to the Rsbuild steps. Rsbuild supports Tailwind CSS versions v3 and v4:
88

9-
```text
10-
? Please select the operation you want: Enable features
11-
? Please select the feature name: Enable Tailwind CSS
12-
```
9+
- [Tailwind CSS v3](https://rsbuild.rs/zh/guide/styling/tailwindcss-v3#tailwind-css-v3)
10+
- [Tailwind CSS v4](https://rsbuild.rs/zh/guide/styling/tailwindcss)
1311

14-
After successful initialization, you will see that the `package.json` has added dependencies for `tailwindcss` and `@modern-js/plugin-tailwindcss`.
12+
## Tailwind Plugin Migration
1513

16-
2. Register the Tailwind plugin in `modern.config.ts`:
14+
### Migration Background
1715

18-
```ts title="modern.config.ts"
19-
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
16+
As Modern.js evolves, to provide a more unified build experience and stronger configuration flexibility, we have adjusted the way Tailwind CSS is supported. Modern.js V3 recommends integrating Tailwind CSS natively through Rsbuild, no longer relying on the `@modern-js/plugin-tailwindcss` plugin, thereby fully leveraging Rsbuild’s more flexible configuration capabilities and better build experience.
2017

21-
export default defineConfig({
22-
plugins: [..., tailwindcssPlugin()],
23-
});
24-
```
18+
### Migration Steps
2519

26-
3. Create a `index.css` file and add the following code:
20+
Taking Tailwind CSS V3 as an example, the migration steps are as follows:
2721

28-
```css title="index.css"
29-
@tailwind base;
30-
@tailwind components;
31-
@tailwind utilities;
32-
```
22+
#### 1. Remove the Old Plugin
23+
24+
- Remove the `@modern-js/plugin-tailwindcss` dependency
25+
- Remove the import and usage of the `@modern-js/plugin-tailwindcss` plugin in the `modern.config.ts` file
3326

34-
:::info
35-
Depending on your needs, you can selectively import the CSS styles provided by Tailwind CSS. Please refer to the [`@tailwind` documentation](https://tailwindcss.com/docs/functions-and-directives#tailwind) for detailed usage of the `@tailwind` directive.
36-
:::
27+
#### 2. Configure PostCSS
3728

38-
4. Import the `index.css` file, for example, add the following code in the root component `src/App.jsx`:
29+
Create or update the `postcss.config.cjs` file.
3930

40-
```js
41-
import './index.css';
31+
```js title="postcss.config.cjs"
32+
module.exports = {
33+
plugins: {
34+
tailwindcss: {},
35+
},
36+
};
4237
```
4338

44-
5. Now you can use the Utility Classes provided by Tailwind CSS in your components:
39+
#### 3. Tailwind CSS Configuration Migration
4540

46-
```tsx
47-
const Hello = () => (
48-
<div className="h-12 w-48">
49-
<p className="text-xl font-medium text-black">hello world</p>
50-
</div>
51-
);
52-
```
41+
- **Single Configuration Case**:
5342

54-
## Configuring Tailwind CSS
43+
- If configured only in `tailwind.config.{ts,js}`, no additional action is needed
44+
- If configured only in `modern.config.ts`, migrate the Tailwind-related configuration to `tailwind.config.{ts,js}`
5545

56-
In Modern.js, you have two ways to configure Tailwind CSS:
46+
- **Dual Configuration Case**: If configurations exist in both `tailwind.config.{ts,js}` and `modern.config.ts`, merge the configurations and migrate the merged result to `tailwind.config.{ts,js}`
5747

58-
1. Using the `tailwind.config.{ts,js}` file, which follows the official usage of Tailwind CSS. Please refer to ["Tailwind CSS - Configuration"](https://tailwindcss.com/docs/configuration) for more details.
48+
- **Special Directory Handling**: If your project contains storybook or config/html directories, supplement `./storybook/**/*` or `./config/html/**/*.{html,ejs,hbs}` in the `content` field of `tailwind.config.{ts,js}`
5949

60-
```ts title="tailwind.config.ts"
61-
import type { Config } from 'tailwindcss';
50+
#### 4. CSS Style Import
6251

63-
export default {
64-
content: ['./src/**/*.{js,jsx,ts,tsx}'],
65-
} as Config;
52+
- Change to use the @tailwind directive
53+
54+
```css
55+
/* Old way */
56+
@import 'tailwindcss/base.css';
57+
@import 'tailwindcss/components.css';
58+
@import 'tailwindcss/utilities.css';
59+
60+
/* New way */
61+
@tailwind base;
62+
@tailwind components;
63+
@tailwind utilities;
6664
```
6765

68-
:::tip
69-
Please upgrade Modern.js to version >= MAJOR_VERSION.33.0 to support automatic reading of `tailwind.config.{ts,js}` files.
70-
:::
66+
#### 5. Twin.macro Integration
67+
68+
If your project uses twin.macro, perform the following steps; otherwise, ignore:
7169

72-
2. Using the [tools.tailwindcss](/configure/app/tools/tailwindcss.html) configuration option. This is the old way of configuring Tailwind CSS, and we recommend using the `tailwind.config.{ts,js}` file for configuration.
70+
- Manually install dependencies: `pnpm add twin.macro styled-components -D`
71+
- Add the `babel-plugin-macros.config.cjs` configuration file:
7372

74-
```ts title="modern.config.ts"
75-
export default {
76-
tools: {
77-
tailwindcss: {
78-
content: ['./src/**/*.{js,jsx,ts,tsx}'],
79-
},
73+
```js title="babel-plugin-macros.config.cjs"
74+
module.exports = {
75+
twin: {
76+
preset: 'styled-components',
77+
config: './tailwind.config.js',
8078
},
8179
};
8280
```
8381

84-
If you are using both the `tailwind.config.{ts,js}` file and `tools.tailwindcss` option, the configuration defined in `tools.tailwindcss` will take precedence and override the content defined in `tailwind.config.{ts,js}`.
82+
#### Tailwind CSS V2 Migration
83+
84+
If your project still uses Tailwind CSS v2, we recommend upgrading to v3 to support JIT and other features. For differences between Tailwind CSS v2 and v3, please refer to the following articles:
85+
86+
- [Tailwind CSS v3.0](https://tailwindcss.com/blog/tailwindcss-v3)
87+
- [Upgrade Guide](https://v3.tailwindcss.com/docs/upgrade-guide)
88+
89+
The migration for Tailwind CSS v2 also follows the above steps, but note that the original plugin automatically adapts to the differences between Tailwind v2 (purge configuration) and v3 (content configuration). After removal, you need to use the `purge` option to specify the CSS classes to retain.
8590

8691
## Tailwind CSS Autocomplete
8792

@@ -92,15 +97,6 @@ You can follow the steps below to enable the autocomplete feature:
9297
1. Install the [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) extension in VS Code.
9398
2. If the root directory of your project does not have a `tailwind.config.{ts,js}` file, you need to create one and write the Tailwind CSS configuration for your current project. Otherwise, the IDE plugin will not work correctly.
9499

95-
## Tailwind CSS Version
96-
97-
Modern.js supports both Tailwind CSS v2 and v3 versions, and the framework will recognize the version of the `tailwindcss` dependency in the project `package.json` file and enable the corresponding configuration. By default, we will install Tailwind CSS v3 version for you.
98-
99-
If your project is still using Tailwind CSS v2, we recommend that you upgrade to v3 to support JIT and other capabilities. For the differences between Tailwind CSS v2 and v3 versions, please refer to the following articles:
100-
101-
- [Tailwind CSS v3.0](https://tailwindcss.com/blog/tailwindcss-v3)
102-
- [Upgrade Guide](https://tailwindcss.com/docs/upgrade-guide)
103-
104100
## Browser Compatibility
105101

106102
Tailwind CSS v2 and v3 do not support the IE 11 browser, please refer to:

packages/document/main-doc/docs/zh/guides/basic-features/css/tailwindcss.mdx

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,93 @@
22

33
[Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。
44

5-
在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),你只需要按照以下步骤操作:
5+
## 在 Modern.js 中使用 Tailwind CSS
66

7-
1. 在项目根目录下执行 `pnpm run new`,按照如下进行选择
7+
在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),你只需要按照 Rsbuild 操作步骤进行配置,Rsbuild 支持 Tailwind CSS v3 和 v4 版本
88

9-
```text
10-
? 请选择你想要的操作 启用可选功能
11-
? 请选择功能名称 启用 「Tailwind CSS」 支持
12-
```
9+
- [Tailwind CSS v3](https://rsbuild.rs/zh/guide/styling/tailwindcss-v3#tailwind-css-v3)
10+
- [Tailwind CSS v4](https://rsbuild.rs/zh/guide/styling/tailwindcss)
1311

14-
成功开启后,你会看到 `package.json` 中新增了 `tailwindcss``@modern-js/plugin-tailwindcss` 依赖。
12+
## Tailwind 插件迁移
1513

16-
2.`modern.config.ts` 中注册 Tailwind 插件:
14+
### 迁移背景
1715

18-
```ts title="modern.config.ts"
19-
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
16+
随着 Modern.js 不断演进,为了提供更统一的构建体验和更强的配置灵活性,我们对 Tailwind CSS 的支持方式进行了调整。Modern.js V3 推荐通过 Rsbuild 原生方式接入 Tailwind CSS,不再依赖 `@modern-js/plugin-tailwindcss` 插件,从而充分利用 Rsbuild 提供的更灵活的配置能力和更优的构建体验。
2017

21-
export default defineConfig({
22-
plugins: [..., tailwindcssPlugin()],
23-
});
24-
```
18+
### 迁移步骤
2519

26-
3. 创建 `index.css` 文件,添加以下代码
20+
以 Tailwind CSS V3 版本为例,提供迁移步骤如下
2721

28-
```css title="index.css"
29-
@tailwind base;
30-
@tailwind components;
31-
@tailwind utilities;
32-
```
22+
#### 1. 移除旧插件
23+
24+
- 移除 `@modern-js/plugin-tailwindcss` 依赖
25+
- 移除 `modern.config.ts` 文件中 `@modern-js/plugin-tailwindcss` 插件的导入和使用
3326

34-
:::info
35-
根据需求不同,你可以选择性地导入 Tailwind CSS 提供的 CSS 样式。请参考 [`@tailwind` 文档](https://tailwindcss.com/docs/functions-and-directives#tailwind) 来了解 `@tailwind` 指令的详细用法。
36-
:::
27+
#### 2. 配置 PostCSS
3728

38-
4. 引用 `index.css` 文件,比如在入口的根组件 `src/App.jsx` 添加如下代码:
29+
创建或更新 `postcss.config.cjs` 文件。
3930

40-
```js
41-
import './index.css';
31+
```js title="postcss.config.cjs"
32+
module.exports = {
33+
plugins: {
34+
tailwindcss: {},
35+
},
36+
};
4237
```
4338

44-
5. 然后即可在各个组件中使用 Tailwind CSS 提供的 Utility Class 了:
39+
#### 3. Tailwind CSS 配置迁移
4540

46-
```tsx
47-
const Hello = () => (
48-
<div className="h-12 w-48">
49-
<p className="text-xl font-medium text-black">hello world</p>
50-
</div>
51-
);
52-
```
41+
- **单一配置情况**
5342

54-
### 配置 Tailwind CSS
43+
- 若仅在 `tailwind.config.{ts,js}` 中配置,则无需额外处理
44+
- 若仅在 `modern.config.ts` 中配置,需将 Tailwind 相关配置移植到 `tailwind.config.{ts,js}`
5545

56-
在 Modern.js 中,你有两种方式来配置 Tailwind CSS:
46+
- **双重配置情况**:若在 `tailwind.config.{ts,js}``modern.config.ts` 中都有配置,需要合并两者的配置内容,并将合并后的配置移植到 `tailwind.config.{ts,js}`
5747

58-
1. 使用 `tailwind.config.{ts,js}` 文件,该用法与 Tailwind CSS 的官方用法一致,请参考 ["Tailwind CSS - Configuration"](https://tailwindcss.com/docs/configuration) 来了解更多用法。
48+
- **特殊目录处理**:若项目中存在 storybook 或 config/html 目录,需在 `tailwind.config.{ts,js}` `content` 中补充 `./storybook/**/*``./config/html/**/*.{html,ejs,hbs}`
5949

60-
```ts title="tailwind.config.ts"
61-
import type { Config } from 'tailwindcss';
50+
#### 4. CSS 样式引入
6251

63-
export default {
64-
content: ['./src/**/*.{js,jsx,ts,tsx}'],
65-
} as Config;
52+
- 变更为 @tailwind 指令方式
53+
54+
```css
55+
/* 旧方式 */
56+
@import 'tailwindcss/base.css';
57+
@import 'tailwindcss/components.css';
58+
@import 'tailwindcss/utilities.css';
59+
60+
/* 新方式 */
61+
@tailwind base;
62+
@tailwind components;
63+
@tailwind utilities;
6664
```
6765

68-
:::tip
69-
请升级 Modern.js 到 >= MAJOR_VERSION.33.0 版本,以支持自动读取 `tailwind.config.{ts,js}` 文件。
70-
:::
66+
#### 5. Twin.macro 集成
67+
68+
若项目中有使用 twin.macro 需执行如下操作,未使用则忽略:
7169

72-
2. 使用 [tools.tailwindcss](/configure/app/tools/tailwindcss.html) 配置项,这是旧版本的用法,我们更推荐使用 `tailwind.config.{ts,js}` 文件进行配置。
70+
- 手动安装依赖:`pnpm add twin.macro styled-components -D`
71+
- 新增 `babel-plugin-macros.config.cjs` 配置文件:
7372

74-
```ts title="modern.config.ts"
75-
export default {
76-
tools: {
77-
tailwindcss: {
78-
content: ['./src/**/*.{js,jsx,ts,tsx}'],
79-
},
73+
```js title="babel-plugin-macros.config.cjs"
74+
module.exports = {
75+
twin: {
76+
preset: 'styled-components',
77+
config: './tailwind.config.js',
8078
},
8179
};
8280
```
8381

84-
如果你同时使用了 `tailwind.config.{ts,js}` 文件和 `tools.tailwindcss` 选项,那么 `tools.tailwindcss` 定义的配置会优先生效,并覆盖 `tailwind.config.{ts,js}` 中定义的内容。
82+
#### Tailwind CSS V2 版本迁移
83+
84+
如果你的项目仍使用 Tailwind CSS v2,我们推荐你升级到 v3 以支持 JIT 等能力。关于 Tailwind CSS v2 与 v3 版本之间的差异,请参考以下文章:
85+
86+
- [Tailwind CSS v3.0](https://tailwindcss.com/blog/tailwindcss-v3)
87+
- [Upgrade Guide](https://v3.tailwindcss.com/docs/upgrade-guide)
88+
89+
Tailwind CSS v2 迁移同样参考上述步骤,但需要注意的是,原插件自动适配 Tailwind v2(purge 配置)与 v3(content 配置)的差异。移除后,需使用 `purge` 配置项来指定需要保留的 CSS 类名。
8590

86-
### Tailwind CSS 自动补全
91+
## Tailwind CSS 自动补全
8792

8893
Tailwind CSS 官方提供了 [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) 插件,用于在 VS Code 中自动补全 Tailwind CSS 的 class names、CSS functions 和 directives。
8994

@@ -92,15 +97,6 @@ Tailwind CSS 官方提供了 [Tailwind CSS IntelliSense](https://github.com/tail
9297
1. 在 VS Code 中安装 [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) 插件。
9398
2. 如果项目的根目录没有 `tailwind.config.{ts,js}` 文件,那么你需要创建该文件,并写入当前项目的 Tailwind CSS 配置,否则 IDE 插件将无法正确生效。
9499

95-
## Tailwind CSS 版本
96-
97-
Modern.js 同时支持 Tailwind CSS v2 和 v3 版本,框架会识别项目 `package.json` 中的 `tailwindcss` 依赖版本,并启用相应的配置。默认情况下,我们会为你安装 Tailwind CSS v3 版本。
98-
99-
如果你的项目仍在使用 Tailwind CSS v2,我们推荐你升级到 v3 以支持 JIT 等能力。关于 Tailwind CSS v2 与 v3 版本之间的差异,请参考以下文章:
100-
101-
- [Tailwind CSS v3.0](https://tailwindcss.com/blog/tailwindcss-v3)
102-
- [Upgrade Guide](https://tailwindcss.com/docs/upgrade-guide)
103-
104100
## 浏览器兼容性
105101

106102
Tailwind CSS v2 和 v3 均不支持 IE 11 浏览器,相关背景请参考:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
2+
import { applyBaseConfig } from '../../../../utils/applyBaseConfig';
3+
4+
export default applyBaseConfig({
5+
plugins: [],
6+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"private": true,
3+
"name": "integration-rsbuild-tailwindcss-v2",
4+
"version": "2.66.0",
5+
"scripts": {
6+
"dev": "modern dev",
7+
"build": "modern build",
8+
"serve": "modern serve",
9+
"new": "modern new"
10+
},
11+
"dependencies": {
12+
"@modern-js/app-tools": "workspace:*",
13+
"@modern-js/runtime": "workspace:*",
14+
"react": "^19.2.0",
15+
"react-dom": "^19.2.0",
16+
"tailwindcss": "^2"
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
plugins: {
3+
tailwindcss: {},
4+
},
5+
};

0 commit comments

Comments
 (0)