2
2
3
3
Basic TypeScript declaration generator for CSS files.
4
4
5
+ ## Install
6
+
7
+ Install the CLI as a dev dependency.
8
+
9
+ ``` shell
10
+ npm install --save-dev css-typed
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Run ` css-typed ` and pass it a glob targeting your CSS files.
16
+
17
+ ``` shell
18
+ npx css-typed ' src/**/*.css'
19
+ ```
20
+
21
+ This will generate ` .d.css.ts ` files next to the original source files.
22
+
23
+ > Note: A CSS module file with the name ` foo.module.css ` will
24
+ > emit ` foo.module.d.css.ts `
25
+
26
+ ## Configuration
27
+
28
+ Configure TypeScript to allow arbitrary extensions (TS 5+).
29
+
30
+ ``` json
31
+ {
32
+ "compilerOptions" : {
33
+ "allowArbitraryExtensions" : true
34
+ }
35
+ }
36
+ ```
37
+
38
+ Add ` *.d.css.ts ` to your ` .gitignore ` if appropriate.
39
+
40
+ ``` shell
41
+ echo ' *.d.css.ts' >> .gitignore
42
+ ```
43
+
44
+ ## Recipes
45
+
46
+ ### Run script
47
+
48
+ To run it as part of your build, you will likely include it as a run script,
49
+ maybe as ` codegen ` or ` pretsc ` .
50
+
51
+ ``` json
52
+ {
53
+ "scripts" : {
54
+ "codegen" : " css-typed 'src/**/*.css'" ,
55
+ "pretsc" : " css-typed 'src/**/*.css'" ,
56
+ "tsc" : " tsc"
57
+ }
58
+ }
59
+ ```
60
+
61
+ ### Watch
62
+
63
+ The CLI does not have built-in watch support. Feel free to [ nodemon] or similar.
64
+
65
+ ``` json
66
+ {
67
+ "scripts" : {
68
+ "codegen" : " css-typed 'src/**/*.css'" ,
69
+ "codegen:watch" : " nodemon -x 'npm run codegen' -w src -e css"
70
+ }
71
+ }
72
+ ```
73
+
74
+ [ nodemon ] : https://www.npmjs.com/package/nodemon
75
+
76
+ ## Details
77
+
78
+ This (very basic) implementation uses [ glob] for file matching and [ css-tree]
79
+ for CSS parsing. It extracts CSS classes (` ClassSelector ` in CSS Tree’s AST) and
80
+ exports them as ` string ` constants (named exports).
81
+
82
+ I chose CSS Tree after a brief search because it had a nice API, good
83
+ documentation, and supported CSS nesting (a requirement for my original use
84
+ case).
85
+
86
+ [ glob ] : https://www.npmjs.com/package/glob
87
+ [ css-tree ] : https://www.npmjs.com/package/css-tree
88
+
5
89
## Motivation
6
90
7
91
[ typescript-plugin-css-modules] provides a great IDE experience, but cannot
@@ -24,10 +108,21 @@ appears [abandoned][174].
24
108
25
109
Therefore, I wrote my own (very basic) implementation.
26
110
27
- <!-- prettier-ignore-start -->
28
111
[ typescript-plugin-css-modules ] : https://www.npmjs.com/package/typescript-plugin-css-modules
29
112
[ typed-css-modules ] : https://www.npmjs.com/package/typed-css-modules
30
113
[ typed-scss-modules ] : https://www.npmjs.com/package/typed-scss-modules
31
114
[ css-modules-loader-core ] : https://www.npmjs.com/package/css-modules-loader-core
32
115
[ 174 ] : https://github.com/css-modules/css-modules-loader-core/issues/174
33
- <!-- prettier-ignore-end -->
116
+
117
+ ## Future
118
+
119
+ This (very basic) implementation suited my immediate needs, but I see some
120
+ improvements we could make. _ All naming subject to bike shedding._
121
+
122
+ - ` ext ` : Traditional (pre TS 5) extension naming with ` *.css.d.ts `
123
+ - ` ignore ` : Ignore support
124
+ - ` format ` : Class name formatting
125
+ - (Related) Gracefully handle invalid names (example: kebab case)
126
+ - ` outDir ` : Publish to a directory instead of next to the sources
127
+ - ` watch ` : First-class watch mode
128
+ - General CLI/UX improvements
0 commit comments