-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: move typed CSS modules content to separate file (#1611)
- Loading branch information
1 parent
eeedeca
commit 6891193
Showing
4 changed files
with
115 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/document/docs/en/plugins/list/plugin-typed-css-modules.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Typed CSS Modules Plugin | ||
|
||
{/* this plugin is still WIP */} | ||
|
||
## Generate exact type definitions | ||
|
||
Although the above method can provide the type of CSS Modules, it cannot accurately prompt which classNames are exported by a certain CSS file. | ||
|
||
Rsbuild supports generating accurate type declarations for CSS Modules, you only need to enable the [output.enableCssModuleTSDeclaration](/config/output/enablecssmodulesdeclaration) config, and then execute the build, Rsbuild will generate type declaration files for all CSS Modules. | ||
|
||
```ts | ||
export default { | ||
output: { | ||
enableCssModuleTSDeclaration: true, | ||
}, | ||
}; | ||
``` | ||
|
||
### Example | ||
|
||
For example, there are two files `src/index.ts` and `src/index.module.scss` under a certain folder: | ||
|
||
```tsx title="src/index.ts" | ||
import styles from './index.module.scss'; | ||
|
||
export default () => { | ||
<div> | ||
<div className={styles.pageHeader}>Page Header</div> | ||
</div>; | ||
}; | ||
``` | ||
|
||
```scss title="src/index.module.scss" | ||
.page-header { | ||
color: black; | ||
} | ||
``` | ||
|
||
After executing the build, the `src/index.module.scss.d.ts` type declaration file will be automatically generated: | ||
|
||
```ts title="src/index.module.scss.d.ts" | ||
// This file is automatically generated. | ||
// Please do not change this file! | ||
interface CssExports { | ||
'page-header': string; | ||
pageHeader: string; | ||
} | ||
export const cssExports: CssExports; | ||
export default cssExports; | ||
``` | ||
|
||
Then open the `src/index.ts` file again, you will see that the `styles` object already has a exact type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/document/docs/zh/plugins/list/plugin-typed-css-modules.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Typed CSS Modules 插件 | ||
|
||
{/* this plugin is still WIP */} | ||
|
||
## 生成准确的类型定义 | ||
|
||
上述方法虽然可以解决 CSS Modules 在 TypeScript 中的类型问题,但是无法准确地提示出某个 CSS 文件导出了哪些类名。 | ||
|
||
Rsbuild 支持为 CSS Modules 生成准确的类型声明,你只需要开启 [output.enableCssModuleTSDeclaration](/config/output/enablecssmoduletsdeclaration) 配置项,再执行构建命令,Rsbuild 就会为项目中所有的 CSS Modules 文件生成相应的类型声明文件。 | ||
|
||
```ts | ||
export default { | ||
output: { | ||
enableCssModuleTSDeclaration: true, | ||
}, | ||
}; | ||
``` | ||
|
||
### 示例 | ||
|
||
例如某个文件夹下面有 `src/index.ts` 和 `src/index.module.scss` 两个文件: | ||
|
||
```tsx title="src/index.ts" | ||
import styles from './index.module.scss'; | ||
|
||
export default () => { | ||
<div> | ||
<div className={styles.pageHeader}>Page Header</div> | ||
</div>; | ||
}; | ||
``` | ||
|
||
```scss | ||
// index.module.scss | ||
.page-header { | ||
color: black; | ||
} | ||
``` | ||
|
||
执行构建命令后,会自动生成 `src/index.module.scss.d.ts` 类型声明文件: | ||
|
||
```ts title="src/index.module.scss.d.ts" | ||
// This file is automatically generated. | ||
// Please do not change this file! | ||
interface CssExports { | ||
'page-header': string; | ||
pageHeader: string; | ||
} | ||
export const cssExports: CssExports; | ||
export default cssExports; | ||
``` | ||
|
||
此时再打开 `src/index.ts` 文件,可以看到 `styles` 对象已经具备了准确的类型。 |