Skip to content

Commit 439a11d

Browse files
authored
docs(css-modules): add generate exact type definitions guide (#2388)
1 parent 49e62f6 commit 439a11d

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,55 @@ declare module '*.module.stylus';
144144

145145
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.
146146

147+
## Generate exact type definitions
148+
149+
Although the above method can provide the type of CSS Modules, it cannot accurately prompt which classNames are exported by a certain CSS file.
150+
151+
Rsbuild supports generating accurate type declarations for CSS Modules, you only need to register the [Typed CSS Modules Plugin](/plugins/list/plugin-typed-css-modules), and then execute the build, Rsbuild will generate type declaration files for all CSS Modules.
152+
153+
```ts file=rsbuild.config.ts
154+
import { pluginTypedCSSModules } from '@rsbuild/plugin-typed-css-modules';
155+
156+
export default {
157+
plugins: [pluginTypedCSSModules()],
158+
};
159+
```
160+
161+
### Example
162+
163+
For example, there are two files `src/index.ts` and `src/index.module.scss` under a certain folder:
164+
165+
```tsx title="src/index.ts"
166+
import styles from './index.module.scss';
167+
168+
export default () => {
169+
<div>
170+
<div className={styles.pageHeader}>Page Header</div>
171+
</div>;
172+
};
173+
```
174+
175+
```scss title="src/index.module.scss"
176+
.page-header {
177+
color: black;
178+
}
179+
```
180+
181+
After executing the build, the `src/index.module.scss.d.ts` type declaration file will be automatically generated:
182+
183+
```ts title="src/index.module.scss.d.ts"
184+
// This file is automatically generated.
185+
// Please do not change this file!
186+
interface CssExports {
187+
'page-header': string;
188+
pageHeader: string;
189+
}
190+
export const cssExports: CssExports;
191+
export default cssExports;
192+
```
193+
194+
Then open the `src/index.ts` file again, you will see that the `styles` object already has a exact type.
195+
147196
### Related configuration
148197

149198
In the above example, `src/index.module.scss.d.ts` is generated by compilation, you can choose to commit them to the Git repository, or you can choose to ignore them in the `.gitignore` file:

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,56 @@ declare module '*.module.stylus';
144144

145145
添加类型声明后,如果依然存在类型错误,请尝试重启当前 IDE,或者调整 `env.d.ts` 所在的目录,使 TypeScript 能够正确识别类型定义。
146146

147+
## 生成准确的类型定义
148+
149+
上述方法虽然可以解决 CSS Modules 在 TypeScript 中的类型问题,但是无法准确地提示出某个 CSS 文件导出了哪些类名。
150+
151+
Rsbuild 支持为 CSS Modules 生成准确的类型声明,你只需要开启 [Typed CSS Modules 插件](/plugins/list/plugin-typed-css-modules),再执行构建命令,Rsbuild 就会为项目中所有的 CSS Modules 文件生成相应的类型声明文件。
152+
153+
```ts title="rsbuild.config.ts"
154+
import { pluginTypedCSSModules } from '@rsbuild/plugin-typed-css-modules';
155+
156+
export default {
157+
plugins: [pluginTypedCSSModules()],
158+
};
159+
```
160+
161+
### 示例
162+
163+
例如某个文件夹下面有 `src/index.ts``src/index.module.scss` 两个文件:
164+
165+
```tsx title="src/index.ts"
166+
import styles from './index.module.scss';
167+
168+
export default () => {
169+
<div>
170+
<div className={styles.pageHeader}>Page Header</div>
171+
</div>;
172+
};
173+
```
174+
175+
```scss
176+
// index.module.scss
177+
.page-header {
178+
color: black;
179+
}
180+
```
181+
182+
执行构建命令后,会自动生成 `src/index.module.scss.d.ts` 类型声明文件:
183+
184+
```ts title="src/index.module.scss.d.ts"
185+
// This file is automatically generated.
186+
// Please do not change this file!
187+
interface CssExports {
188+
'page-header': string;
189+
pageHeader: string;
190+
}
191+
export const cssExports: CssExports;
192+
export default cssExports;
193+
```
194+
195+
此时再打开 `src/index.ts` 文件,可以看到 `styles` 对象已经具备了准确的类型。
196+
147197
### 相关配置
148198

149199
在上述例子中,`src/index.module.scss.d.ts` 是编译生成的代码,你可以选择将它们提交到 Git 仓库里,也可以选择在 `.gitignore` 文件中忽略它们:

0 commit comments

Comments
 (0)