vite-plugin-css-module-types 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +127 -0
- package/dist/index.js +583 -0
- package/index.ts +3 -0
- package/package.json +55 -0
- package/src/lib/camel-case.ts +4 -0
- package/src/lib/class-index.ts +169 -0
- package/src/lib/css-comments.ts +55 -0
- package/src/lib/declaration-map.ts +40 -0
- package/src/lib/generate-dts.ts +157 -0
- package/src/lib/index.ts +16 -0
- package/src/lib/path-filter.ts +37 -0
- package/src/lib/sourcemap.ts +73 -0
- package/src/lib/transform.ts +30 -0
- package/src/lib/types.ts +65 -0
- package/src/plugin.ts +287 -0
- package/src/tests/camel-case.spec.ts +36 -0
- package/src/tests/class-index.spec.ts +439 -0
- package/src/tests/css-comments.spec.ts +116 -0
- package/src/tests/declaration-map.spec.ts +102 -0
- package/src/tests/generate-dts.spec.ts +499 -0
- package/src/tests/integration.spec.ts +175 -0
- package/src/tests/path-filter.spec.ts +130 -0
- package/src/tests/transform.spec.ts +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# vite-plugin-css-module-types
|
|
2
|
+
|
|
3
|
+
Vite **dev-server** plugin that generates `.d.ts` declarations for CSS Modules. With `css.devSourcemap` enabled, includes JSDoc links to original CSS source lines and declaration maps for "Go to Definition".
|
|
4
|
+
|
|
5
|
+
Inspired by [vite-plugin-css-module-dts](https://github.com/lovetingyuan/vite-plugin-css-module-dts) and [typed-css-modules](https://github.com/Quramy/typed-css-modules).
|
|
6
|
+
|
|
7
|
+
## Quick start
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// vite.config.ts
|
|
11
|
+
import viteCssModuleTypesPlugin from 'vite-plugin-css-module-types';
|
|
12
|
+
|
|
13
|
+
export default defineConfig({
|
|
14
|
+
css: { devSourcemap: true },
|
|
15
|
+
plugins: [
|
|
16
|
+
viteCssModuleTypesPlugin({
|
|
17
|
+
dtsOutputDir: 'tmp/vite-plugin-css-module-types', // add to .gitignore
|
|
18
|
+
include: ['apps/web/src'],
|
|
19
|
+
exclude: ['apps/web/src/legacy'],
|
|
20
|
+
}),
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Add to your `.gitignore`:
|
|
26
|
+
|
|
27
|
+
```# vite-plugin-css-module-types
|
|
28
|
+
tmp/vite-plugin-css-module-types/
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Add to your `tsconfig.json`:
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"compilerOptions": {
|
|
36
|
+
"rootDirs": [
|
|
37
|
+
"./src",
|
|
38
|
+
"./tmp/vite-plugin-css-module-types/src"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"include": [
|
|
42
|
+
"**/*.ts",
|
|
43
|
+
"**/*.tsx",
|
|
44
|
+
"tmp/vite-plugin-css-module-types/src"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
If you use `/src` in your `rootDirs` (as in the example above), it's best to also add the corresponding path from the output directory (e.g., `./tmp/vite-plugin-css-module-types/src`) to avoid issues with path mapping.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
> Runs only in `serve` mode. Processes `*.module.css` files.
|
|
53
|
+
|
|
54
|
+
## Options
|
|
55
|
+
|
|
56
|
+
| Option | Type | Default | Description |
|
|
57
|
+
| -------------------------- | -------------------- | ----------- | -------------------------------------------------------------------- |
|
|
58
|
+
| `dtsOutputDir` | `string` | — | **Required.** Output directory for `.d.ts` files, relative to root. |
|
|
59
|
+
| `include` | `string \| string[]` | `undefined` | Folder(s) to include (prefix-matched). |
|
|
60
|
+
| `exclude` | `string \| string[]` | `undefined` | Folder(s) to exclude. **Wins** when both match. |
|
|
61
|
+
| `namedExports` | `boolean` | `false` | Emit `export declare const` per class. |
|
|
62
|
+
| `allowArbitraryExtensions` | `boolean` | `false` | Use TS 5.0+ `.d.module.css.ts` naming instead of `.module.css.d.ts`. |
|
|
63
|
+
| `declarationMap` | `boolean` | `true` | Generate `.d.ts.map` for "Go to Definition". Needs `devSourcemap`. |
|
|
64
|
+
|
|
65
|
+
Path matching is **prefix-based** (not glob). Separators are normalized, `./` and trailing `/` are stripped.
|
|
66
|
+
|
|
67
|
+
## Output examples
|
|
68
|
+
|
|
69
|
+
### Default mode
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
declare const styles: {
|
|
73
|
+
/** [↗ button.module.css:3](file:///abs/path/button.module.css#L3) */
|
|
74
|
+
readonly 'btn-primary': '_btn-primary_x1y2z';
|
|
75
|
+
};
|
|
76
|
+
export default styles;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### With `namedExports: true`
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
/** [↗ button.module.css:3](file:///abs/path/button.module.css#L3) */
|
|
83
|
+
export declare const btnPrimary: string;
|
|
84
|
+
|
|
85
|
+
declare const styles: {
|
|
86
|
+
readonly 'btn-primary': '_btn-primary_x1y2z';
|
|
87
|
+
readonly btnPrimary: '_btn-primary_x1y2z';
|
|
88
|
+
};
|
|
89
|
+
export default styles;
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### With CSS docblock comments
|
|
93
|
+
|
|
94
|
+
```css
|
|
95
|
+
/** Main call-to-action button. */
|
|
96
|
+
.btn-primary { color: blue; }
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
````ts
|
|
100
|
+
declare const styles: {
|
|
101
|
+
/**
|
|
102
|
+
* ```txt
|
|
103
|
+
* Main call-to-action button.
|
|
104
|
+
* ```
|
|
105
|
+
* ---
|
|
106
|
+
* ```css
|
|
107
|
+
* .btn-primary { color: blue; }
|
|
108
|
+
* ```
|
|
109
|
+
* ---
|
|
110
|
+
* [↗ button.module.css:2](file:///abs/path/button.module.css#L2)
|
|
111
|
+
*/
|
|
112
|
+
readonly 'btn-primary': '_btn-primary_x1y2z';
|
|
113
|
+
};
|
|
114
|
+
export default styles;
|
|
115
|
+
````
|
|
116
|
+
|
|
117
|
+
### Empty module
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
export {};
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Dependencies
|
|
124
|
+
|
|
125
|
+
- [source-map](https://www.npmjs.com/package/source-map) — inline CSS sourcemap parsing
|
|
126
|
+
- [@jridgewell/sourcemap-codec](https://www.npmjs.com/package/@jridgewell/sourcemap-codec) — VLQ encoding for declaration maps
|
|
127
|
+
- Vite ^8 (peer), TypeScript ^5 (peer)
|