UNPKG

5.15 kBMarkdownView Raw
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
3Creates TypeScript definition files from [CSS Modules](https://github.com/css-modules/css-modules) .css files.
4
5If 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
17typed-css-modules creates the following .d.ts files from the above css:
18
19```ts
20/* styles.css.d.ts */
21export const primary: string;
22export const myClass: string;
23```
24
25So, you can import CSS modules' class or variable into your TypeScript sources:
26
27```ts
28/* app.ts */
29import * as styles from './styles.css';
30console.log(`<div class="${styles.myClass}"></div>`);
31console.log(`<div style="color: ${styles.primary}"></div>`);
32```
33
34## CLI
35
36```sh
37npm install -g typed-css-modules
38```
39
40And exec `tcm <input directory>` command.
41For example, if you have .css files under `src` directory, exec the following:
42
43```sh
44tcm src
45```
46
47Then, 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
57Use `-o` or `--outDir` option.
58
59For example:
60
61```sh
62tcm -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
75By the default, this tool searches `**/*.css` files under `<input directory>`.
76If you can customize glob pattern, you can use `--pattern` or `-p` option.
77
78```sh
79tcm -p src/**/*.icss
80```
81
82#### watch
83With `-w` or `--watch`, this CLI watches files in the input directory.
84
85#### camelize CSS token
86With `-c` or `--camelCase`, kebab-cased CSS classes(such as `.my-class {...}`) are exported as camelized TypeScript varibale name(`export const myClass: string`).
87
88
89You can pass `--camelCase dashes` to only camelize dashes in the class name. Since version `0.27.1` in the
90webpack `css-loader`. This will keep upperCase class names intact, e.g.:
91
92```css
93.SomeComponent {
94 height: 10px;
95}
96```
97
98becomes
99
100```typescript
101export const SomeComponent: string;
102```
103
104See also [webpack css-loader's camelCase option](https://github.com/webpack/css-loader#camelcase).
105
106## API
107
108```sh
109npm install typed-css-modules
110```
111
112```js
113import DtsCreator from 'typed-css-modules';
114let creator = new DtsCreator();
115creator.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
123DtsCreator instance processes the input CSS and create TypeScript definition contents.
124
125#### `new DtsCreator(option)`
126You 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)`
135Returns `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
141DtsContent instance has `*.d.ts` content, final output path, and function to write file.
142
143#### `writeFile() => Promise(dtsContent)`
144Writes the DtsContent instance's content to a file.
145
146* `dtsContent`: the DtsContent instance itself.
147
148#### `tokens`
149An array of tokens retrieved from input CSS file.
150e.g. `['myClass']`
151
152#### `contents`
153An array of TypeScript definition expressions.
154e.g. `['export const myClass: string;']`.
155
156#### `formatted`
157A string of TypeScript definition expression.
158
159e.g.
160
161```ts
162export const myClass: string;
163```
164
165#### `messageList`
166An array of messages. The messages contains invalid token information.
167e.g. `['my-class is not valid TypeScript variable name.']`.
168
169#### `outputFilePath`
170Final output file path.
171
172## Remarks
173If 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
194There is a minimum example in this repository `example` folder. Clone this repository and run `cd example; npm i; npm start`.
195
196Or 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
199This software is released under the MIT License, see LICENSE.txt.