Skip to content

Commit 91617cd

Browse files
authored
Merge pull request #110 from Quramy/prettier
Chore: Install prettier
2 parents e103f1d + 50fb1c3 commit 91617cd

File tree

17 files changed

+572
-332
lines changed

17 files changed

+572
-332
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ jobs:
1616
uses: actions/setup-node@v1
1717
with:
1818
node-version: ${{ matrix.node-version }}
19-
- name: npm install, build, and test
19+
- name: npm install, build
2020
run: |
2121
npm ci
22+
- name: Lint
23+
run: |
24+
npm run lint
25+
- name: Test
26+
run: |
2227
npm run test:ci
2328
env:
2429
CI: true

.husky/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/pre-commit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx pretty-quick --staged
5+

.prettierignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.git/
2+
.husky/
3+
node_modules/
4+
lib/
5+
__snapshots__/
6+
coverage/
7+
*.css.d.ts
8+
test/**/*.css

.prettierrc.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
trailingComma: all
2+
tabWidth: 2
3+
semi: true
4+
singleQuote: true
5+
bracketSpacing: true
6+
printWidth: 120
7+
arrowParens: avoid

README.md

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ typed-css-modules creates the following .d.ts files from the above css:
1919
```ts
2020
/* styles.css.d.ts */
2121
declare const styles: {
22-
readonly "primary": string;
23-
readonly "myClass": string;
22+
readonly primary: string;
23+
readonly myClass: string;
2424
};
2525
export = styles;
2626
```
@@ -57,6 +57,7 @@ Then, this creates `*.css.d.ts` files under the directory which has original .cs
5757
```
5858

5959
#### output directory
60+
6061
Use `-o` or `--outDir` option.
6162

6263
For example:
@@ -84,11 +85,12 @@ tcm -p 'src/**/*.icss' .
8485
```
8586

8687
#### watch
88+
8789
With `-w` or `--watch`, this CLI watches files in the input directory.
8890

8991
#### camelize CSS token
90-
With `-c` or `--camelCase`, kebab-cased CSS classes(such as `.my-class {...}`) are exported as camelized TypeScript varibale name(`export const myClass: string`).
9192

93+
With `-c` or `--camelCase`, kebab-cased CSS classes(such as `.my-class {...}`) are exported as camelized TypeScript varibale name(`export const myClass: string`).
9294

9395
You can pass `--camelCase dashes` to only camelize dashes in the class name. Since version `0.27.1` in the
9496
webpack `css-loader`. This will keep upperCase class names intact, e.g.:
@@ -103,22 +105,23 @@ becomes
103105

104106
```typescript
105107
declare const styles: {
106-
readonly "SomeComponent": string;
108+
readonly SomeComponent: string;
107109
};
108110
export = styles;
109111
```
110112

111113
See also [webpack css-loader's camelCase option](https://github.com/webpack/css-loader#camelcase).
112114

113115
#### named exports (enable tree shaking)
116+
114117
With `-e` or `--namedExports`, types are exported as named exports as opposed to default exports.
115118
This enables support for the `namedExports` css-loader feature, required for webpack to tree shake the final CSS (learn more [here](https://webpack.js.org/loaders/css-loader/#namedexport)).
116119

117120
Use this option in combination with https://webpack.js.org/loaders/css-loader/#namedexport and https://webpack.js.org/loaders/style-loader/#namedexport (if you use `style-loader`).
118121

119122
When this option is enabled, the type definition changes to support named exports.
120123

121-
*NOTE: this option enables camelcase by default.*
124+
_NOTE: this option enables camelcase by default._
122125

123126
```css
124127
.SomeComponent {
@@ -130,7 +133,7 @@ When this option is enabled, the type definition changes to support named export
130133

131134
```typescript
132135
declare const styles: {
133-
readonly "SomeComponent": string;
136+
readonly SomeComponent: string;
134137
};
135138
export = styles;
136139
```
@@ -151,56 +154,62 @@ npm install typed-css-modules
151154
import DtsCreator from 'typed-css-modules';
152155
let creator = new DtsCreator();
153156
creator.create('src/style.css').then(content => {
154-
console.log(content.tokens); // ['myClass']
155-
console.log(content.formatted); // 'export const myClass: string;'
156-
content.writeFile(); // writes this content to "src/style.css.d.ts"
157+
console.log(content.tokens); // ['myClass']
158+
console.log(content.formatted); // 'export const myClass: string;'
159+
content.writeFile(); // writes this content to "src/style.css.d.ts"
157160
});
158161
```
159162

160163
### class DtsCreator
164+
161165
DtsCreator instance processes the input CSS and create TypeScript definition contents.
162166

163167
#### `new DtsCreator(option)`
168+
164169
You can set the following options:
165170

166-
* `option.rootDir`: Project root directory(default: `process.cwd()`).
167-
* `option.searchDir`: Directory which includes target `*.css` files(default: `'./'`).
168-
* `option.outDir`: Output directory(default: `option.searchDir`).
169-
* `option.camelCase`: Camelize CSS class tokens.
170-
* `option.namedExports`: Use named exports as opposed to default exports to enable tree shaking. Requires `import * as style from './file.module.css';` (default: `false`)
171-
* `option.EOL`: EOL (end of line) for the generated `d.ts` files. Possible values `'\n'` or `'\r\n'`(default: `os.EOL`).
171+
- `option.rootDir`: Project root directory(default: `process.cwd()`).
172+
- `option.searchDir`: Directory which includes target `*.css` files(default: `'./'`).
173+
- `option.outDir`: Output directory(default: `option.searchDir`).
174+
- `option.camelCase`: Camelize CSS class tokens.
175+
- `option.namedExports`: Use named exports as opposed to default exports to enable tree shaking. Requires `import * as style from './file.module.css';` (default: `false`)
176+
- `option.EOL`: EOL (end of line) for the generated `d.ts` files. Possible values `'\n'` or `'\r\n'`(default: `os.EOL`).
172177

173178
#### `create(filepath, contents) => Promise(dtsContent)`
179+
174180
Returns `DtsContent` instance.
175181

176-
* `filepath`: path of target .css file.
177-
* `contents`(optional): the CSS content of the `filepath`. If set, DtsCreator uses the contents instead of the original contents of the `filepath`.
182+
- `filepath`: path of target .css file.
183+
- `contents`(optional): the CSS content of the `filepath`. If set, DtsCreator uses the contents instead of the original contents of the `filepath`.
178184

179185
### class DtsContent
186+
180187
DtsContent instance has `*.d.ts` content, final output path, and function to write file.
181188

182189
#### `writeFile(postprocessor) => Promise(dtsContent)`
190+
183191
Writes the DtsContent instance's content to a file. Returns the DtsContent instance.
184192

185-
* `postprocessor` (optional): a function that takes the formatted definition string and returns a modified string that will be the final content written to the file.
193+
- `postprocessor` (optional): a function that takes the formatted definition string and returns a modified string that will be the final content written to the file.
186194

187195
You could use this, for example, to pass generated definitions through a formatter like Prettier, or to add a comment to the top of generated files:
188196

189197
```js
190-
dtsContent.writeFile(
191-
definition => `// Generated automatically, do not edit\n${definition}`
192-
)
198+
dtsContent.writeFile(definition => `// Generated automatically, do not edit\n${definition}`);
193199
```
194200

195201
#### `tokens`
202+
196203
An array of tokens retrieved from input CSS file.
197204
e.g. `['myClass']`
198205

199206
#### `contents`
207+
200208
An array of TypeScript definition expressions.
201209
e.g. `['export const myClass: string;']`.
202210

203211
#### `formatted`
212+
204213
A string of TypeScript definition expression.
205214

206215
e.g.
@@ -210,13 +219,16 @@ export const myClass: string;
210219
```
211220

212221
#### `messageList`
222+
213223
An array of messages. The messages contains invalid token information.
214224
e.g. `['my-class is not valid TypeScript variable name.']`.
215225

216226
#### `outputFilePath`
227+
217228
Final output file path.
218229

219230
## Remarks
231+
220232
If your input CSS file has the following class names, these invalid tokens are not written to output `.d.ts` file.
221233

222234
```css
@@ -227,7 +239,7 @@ If your input CSS file has the following class names, these invalid tokens are n
227239

228240
/* invalid TypeScript variable */
229241
/* If camelCase option is set, this token will be converted to 'myClass' */
230-
.my-class{
242+
.my-class {
231243
color: red;
232244
}
233245

@@ -238,9 +250,11 @@ If your input CSS file has the following class names, these invalid tokens are n
238250
```
239251

240252
## Example
253+
241254
There is a minimum example in this repository `example` folder. Clone this repository and run `cd example; npm i; npm start`.
242255

243256
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.
244257

245258
## License
259+
246260
This software is released under the MIT License, see LICENSE.txt.

0 commit comments

Comments
 (0)