1 | # typed-css-modules [![Build Status](https://travis-ci.org/Quramy/typed-css-modules.svg?branch=master)](https://travis-ci.org/Quramy/typed-css-modules) [![npm version](https://badge.fury.io/js/typed-css-modules.svg)](http://badge.fury.io/js/typed-css-modules)
|
2 |
|
3 | Creates TypeScript definition files from [CSS Modules](https://github.com/css-modules/css-modules) .css files.
|
4 |
|
5 | If you have the following css,
|
6 |
|
7 | ```css
|
8 | /* styles.css */
|
9 |
|
10 | @value primary: red;
|
11 |
|
12 | .myClass {
|
13 | color: primary;
|
14 | }
|
15 | ```
|
16 |
|
17 | typed-css-modules creates the following .d.ts files from the above css:
|
18 |
|
19 | ```ts
|
20 | /* styles.css.d.ts */
|
21 | export const primary: string;
|
22 | export const myClass: string;
|
23 | ```
|
24 |
|
25 | So, you can import CSS modules' class or variable into your TypeScript sources:
|
26 |
|
27 | ```ts
|
28 | /* app.ts */
|
29 | import * as styles from './styles.css';
|
30 | console.log(`<div class="${styles.myClass}"></div>`);
|
31 | console.log(`<div style="color: ${styles.primary}"></div>`);
|
32 | ```
|
33 |
|
34 | ## CLI
|
35 |
|
36 | ```sh
|
37 | npm install -g typed-css-modules
|
38 | ```
|
39 |
|
40 | And exec `tcm <input directory>` command.
|
41 | For example, if you have .css files under `src` directory, exec the following:
|
42 |
|
43 | ```sh
|
44 | tcm src
|
45 | ```
|
46 |
|
47 | Then, this creates `*.css.d.ts` files under the directory which has original .css file.
|
48 |
|
49 | ```text
|
50 | (your project root)
|
51 | - src/
|
52 | | myStyle.css
|
53 | | myStyle.css.d.ts [created]
|
54 | ```
|
55 |
|
56 | #### output directory
|
57 | Use `-o` or `--outDir` option.
|
58 |
|
59 | For example:
|
60 |
|
61 | ```sh
|
62 | tcm -o dist src
|
63 | ```
|
64 |
|
65 | ```text
|
66 | (your project root)
|
67 | - src/
|
68 | | myStyle.css
|
69 | - dist/
|
70 | | myStyle.css.d.ts [created]
|
71 | ```
|
72 |
|
73 | #### file name pattern
|
74 |
|
75 | By the default, this tool searches `**/*.css` files under `<input directory>`.
|
76 | If you can customize glob pattern, you can use `--pattern` or `-p` option.
|
77 |
|
78 | ```sh
|
79 | tcm -p src/**/*.icss
|
80 | ```
|
81 |
|
82 | #### watch
|
83 | With `-w` or `--watch`, this CLI watches files in the input directory.
|
84 |
|
85 | #### camelize CSS token
|
86 | With `-c` or `--camelCase`, kebab-cased CSS classes(such as `.my-class {...}`) are exported as camelized TypeScript varibale name(`export const myClass: string`).
|
87 |
|
88 |
|
89 | You can pass `--camelCase dashes` to only camelize dashes in the class name. Since version `0.27.1` in the
|
90 | webpack `css-loader`. This will keep upperCase class names intact, e.g.:
|
91 |
|
92 | ```css
|
93 | .SomeComponent {
|
94 | height: 10px;
|
95 | }
|
96 | ```
|
97 |
|
98 | becomes
|
99 |
|
100 | ```typescript
|
101 | export const SomeComponent: string;
|
102 | ```
|
103 |
|
104 | See also [webpack css-loader's camelCase option](https://github.com/webpack/css-loader#camelcase).
|
105 |
|
106 | ## API
|
107 |
|
108 | ```sh
|
109 | npm install typed-css-modules
|
110 | ```
|
111 |
|
112 | ```js
|
113 | import DtsCreator from 'typed-css-modules';
|
114 | let creator = new DtsCreator();
|
115 | creator.create('src/style.css').then(content => {
|
116 | console.log(content.tokens); // ['myClass']
|
117 | console.log(content.formatted); // 'export const myClass: string;'
|
118 | content.writeFile(); // writes this content to "src/style.css.d.ts"
|
119 | });
|
120 | ```
|
121 |
|
122 | ### class DtsCreator
|
123 | DtsCreator instance processes the input CSS and create TypeScript definition contents.
|
124 |
|
125 | #### `new DtsCreator(option)`
|
126 | You can set the following options:
|
127 |
|
128 | * `option.rootDir`: Project root directory(default: `process.cwd()`).
|
129 | * `option.searchDir`: Directory which includes target `*.css` files(default: `'./'`).
|
130 | * `option.outDir`: Output directory(default: `option.searchDir`).
|
131 | * `option.camelCase`: Camelize CSS class tokens.
|
132 | * `option.EOL`: EOL (end of line) for the generated `d.ts` files. Possible values `'\n'` or `'\r\n'`(default: `os.EOL`).
|
133 |
|
134 | #### `create(filepath, contents) => Promise(dtsContent)`
|
135 | Returns `DtsContent` instance.
|
136 |
|
137 | * `filepath`: path of target .css file.
|
138 | * `contents`(optional): the CSS content of the `filepath`. If set, DtsCreator uses the contents instead of the original contents of the `filepath`.
|
139 |
|
140 | ### class DtsContent
|
141 | DtsContent instance has `*.d.ts` content, final output path, and function to write file.
|
142 |
|
143 | #### `writeFile() => Promise(dtsContent)`
|
144 | Writes the DtsContent instance's content to a file.
|
145 |
|
146 | * `dtsContent`: the DtsContent instance itself.
|
147 |
|
148 | #### `tokens`
|
149 | An array of tokens retrieved from input CSS file.
|
150 | e.g. `['myClass']`
|
151 |
|
152 | #### `contents`
|
153 | An array of TypeScript definition expressions.
|
154 | e.g. `['export const myClass: string;']`.
|
155 |
|
156 | #### `formatted`
|
157 | A string of TypeScript definition expression.
|
158 |
|
159 | e.g.
|
160 |
|
161 | ```ts
|
162 | export const myClass: string;
|
163 | ```
|
164 |
|
165 | #### `messageList`
|
166 | An array of messages. The messages contains invalid token information.
|
167 | e.g. `['my-class is not valid TypeScript variable name.']`.
|
168 |
|
169 | #### `outputFilePath`
|
170 | Final output file path.
|
171 |
|
172 | ## Remarks
|
173 | If your input CSS file has the followng class names, these invalid tokens are not written to output `.d.ts` file.
|
174 |
|
175 | ```css
|
176 | /* TypeScript reserved word */
|
177 | .while {
|
178 | color: red;
|
179 | }
|
180 |
|
181 | /* invalid TypeScript variable */
|
182 | /* If camelCase option is set, this token will be converted to 'myClass' */
|
183 | .my-class{
|
184 | color: red;
|
185 | }
|
186 |
|
187 | /* it's ok */
|
188 | .myClass {
|
189 | color: red;
|
190 | }
|
191 | ```
|
192 |
|
193 | ## Example
|
194 | There is a minimum example in this repository `example` folder. Clone this repository and run `cd example; npm i; npm start`.
|
195 |
|
196 | Or please see [https://github.com/Quramy/typescript-css-modules-demo](https://github.com/Quramy/typescript-css-modules-demo). It's a working demonstration of CSS Modules with React and TypeScript.
|
197 |
|
198 | ## License
|
199 | This software is released under the MIT License, see LICENSE.txt.
|