You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By adding the Typed CSS Modules plugin, Rsbuild will generate TypeScript declaration files for all CSS Modules in the project.
34
34
35
-
Some description.
35
+
For example, create two files named `src/index.ts` and `src/index.module.css`:
36
36
37
-
- Type: `string`
38
-
- Default: `undefined`
39
-
- Example:
37
+
```tsx title="src/index.ts"
38
+
importstylesfrom"./index.module.css";
40
39
41
-
```js
42
-
pluginExample({
43
-
foo:"bar",
44
-
});
40
+
console.log(styles.pageHeader);
45
41
```
46
42
43
+
```css title="src/index.module.css"
44
+
.page-header {
45
+
color: black;
46
+
}
47
+
```
48
+
49
+
After building, Rsbuild will generate a `src/index.module.css.d.ts` type declaration file:
50
+
51
+
```ts title="src/index.module.css.d.ts"
52
+
interfaceCssExports {
53
+
"page-header":string;
54
+
pageHeader:string;
55
+
}
56
+
declareconst cssExports:CssExports;
57
+
exportdefaultcssExports;
58
+
```
59
+
60
+
Now when you open the `src/index.ts` file, you can see that the `styles` object already has an accurate type.
61
+
62
+
## Named Export
63
+
64
+
If [output.cssModules.namedExport](/config/output/css-modules#cssmodulesnamedexport) is enabled, the generated type declaration file will only include named exports.
65
+
66
+
For example:
67
+
68
+
```css title="index.module.css"
69
+
.page {
70
+
color: black;
71
+
}
72
+
.header {
73
+
color: white;
74
+
}
75
+
```
76
+
77
+
The generated types would be:
78
+
79
+
```ts title="src/index.module.css.d.ts"
80
+
exportconst page:string;
81
+
exportconst header:string;
82
+
```
83
+
84
+
## Configure Git
85
+
86
+
In the above example, `src/index.module.css.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:
87
+
88
+
```bash
89
+
# Ignore auto generated CSS declarations
90
+
*.module.css.d.ts
91
+
*.module.sass.d.ts
92
+
*.module.scss.d.ts
93
+
*.module.less.d.ts
94
+
*.module.styl.d.ts
95
+
*.module.stylus.d.ts
96
+
```
97
+
98
+
In addition, if the generated code causes ESLint to report errors, you can also add the above configuration to the `.eslintignore` file.
0 commit comments