Skip to content

Commit d02ac2b

Browse files
committed
docs: update README
1 parent 1feb838 commit d02ac2b

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

README.md

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# rsbuild-plugin-example
1+
# @rsbuild/plugin-typed-css-modules
22

3-
rsbuild-plugin-example is a Rsbuild plugin to do something.
3+
An Rsbuild plugin to generate TypeScript declaration files for CSS Modules.
44

55
<p>
6-
<a href="https://npmjs.com/package/rsbuild-plugin-example">
7-
<img src="https://img.shields.io/npm/v/rsbuild-plugin-example?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
6+
<a href="https://npmjs.com/package/@rsbuild/plugin-typed-css-modules">
7+
<img src="https://img.shields.io/npm/v/@rsbuild/plugin-typed-css-modules?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
88
</a>
99
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
1010
</p>
@@ -14,36 +14,89 @@ rsbuild-plugin-example is a Rsbuild plugin to do something.
1414
Install:
1515

1616
```bash
17-
npm add rsbuild-plugin-example -D
17+
npm add @rsbuild/plugin-typed-css-modules -D
1818
```
1919

2020
Add plugin to your `rsbuild.config.ts`:
2121

2222
```ts
2323
// rsbuild.config.ts
24-
import { pluginExample } from "rsbuild-plugin-example";
24+
import { pluginTypedCSSModules } from "@rsbuild/plugin-typed-css-modules";
2525

2626
export default {
27-
plugins: [pluginExample()],
27+
plugins: [pluginTypedCSSModules()],
2828
};
2929
```
3030

31-
## Options
31+
## Example
3232

33-
### foo
33+
By adding the Typed CSS Modules plugin, Rsbuild will generate TypeScript declaration files for all CSS Modules in the project.
3434

35-
Some description.
35+
For example, create two files named `src/index.ts` and `src/index.module.css`:
3636

37-
- Type: `string`
38-
- Default: `undefined`
39-
- Example:
37+
```tsx title="src/index.ts"
38+
import styles from "./index.module.css";
4039

41-
```js
42-
pluginExample({
43-
foo: "bar",
44-
});
40+
console.log(styles.pageHeader);
4541
```
4642

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+
interface CssExports {
53+
"page-header": string;
54+
pageHeader: string;
55+
}
56+
declare const cssExports: CssExports;
57+
export default cssExports;
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+
export const page: string;
81+
export const 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.
99+
47100
## License
48101

49102
[MIT](./LICENSE).

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "rsbuild-plugin-example",
2+
"name": "@rsbuild/plugin-typed-css-modules",
33
"version": "0.0.0",
4-
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-template",
4+
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-typed-css-modules",
55
"license": "MIT",
66
"type": "module",
77
"exports": {
@@ -43,14 +43,14 @@
4343
"typescript": "^5.5.2"
4444
},
4545
"peerDependencies": {
46-
"@rsbuild/core": "0.x || 1.x"
46+
"@rsbuild/core": "1.x"
4747
},
4848
"peerDependenciesMeta": {
4949
"@rsbuild/core": {
5050
"optional": true
5151
}
5252
},
53-
"packageManager": "pnpm@9.2.0",
53+
"packageManager": "pnpm@9.5.0",
5454
"publishConfig": {
5555
"access": "public",
5656
"registry": "https://registry.npmjs.org/"

playground/rsbuild.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from '@rsbuild/core';
2-
import { pluginExample } from '../src';
2+
import { pluginTypedCSSModules } from '../src';
33

44
export default defineConfig({
5-
plugins: [pluginExample()],
5+
plugins: [pluginTypedCSSModules()],
66
});

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { RsbuildPlugin } from '@rsbuild/core';
22

3-
export type PluginExampleOptions = {
3+
export type pluginTypedCSSModulesOptions = {
44
foo?: string;
55
bar?: boolean;
66
};
77

8-
export const pluginExample = (
9-
options: PluginExampleOptions = {},
8+
export const pluginTypedCSSModules = (
9+
options: pluginTypedCSSModulesOptions = {},
1010
): RsbuildPlugin => ({
1111
name: 'plugin-example',
1212

test/basic/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname } from 'node:path';
22
import { fileURLToPath } from 'node:url';
33
import { expect, test } from '@playwright/test';
44
import { createRsbuild } from '@rsbuild/core';
5-
import { pluginExample } from '../../src';
5+
import { pluginTypedCSSModules } from '../../src';
66
import { getRandomPort } from '../helper';
77

88
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -11,7 +11,7 @@ test('should render page as expected', async ({ page }) => {
1111
const rsbuild = await createRsbuild({
1212
cwd: __dirname,
1313
rsbuildConfig: {
14-
plugins: [pluginExample()],
14+
plugins: [pluginTypedCSSModules()],
1515
server: {
1616
port: getRandomPort(),
1717
},
@@ -30,7 +30,7 @@ test('should build succeed', async ({ page }) => {
3030
const rsbuild = await createRsbuild({
3131
cwd: __dirname,
3232
rsbuildConfig: {
33-
plugins: [pluginExample()],
33+
plugins: [pluginTypedCSSModules()],
3434
},
3535
});
3636

0 commit comments

Comments
 (0)