Skip to content
This repository was archived by the owner on Oct 30, 2020. It is now read-only.

Commit 6e16ce9

Browse files
committed
document option namedExport in readme.md
1 parent 0f27a87 commit 6e16ce9

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,55 @@ Webpack loader that works as a css-loader drop-in replacement to generate TypeSc
66

77
Install via npm `npm install --save-dev typings-for-css-modules-loader`
88

9+
## Options
10+
11+
Just like any other loader you can specify options e.g. as query-params
12+
13+
### css-loader options
14+
Any option that your installed version of css-loader supports can be used and will be passed to it.
15+
16+
### `namedExport`-option
17+
As your fellow css-developer may tend to use characters like dashes(`-`) that are not valid characters for a typescript variable the default behaviour for this loader is to export an interface as the default export that contains the classnames as properties.
18+
e.g.:
19+
```ts
20+
export interface IExampleCss {
21+
'foo': string;
22+
'bar-baz': string;
23+
}
24+
declare const styles: IExampleCss;
25+
26+
export default styles;
27+
```
28+
29+
A cleaner way is to expose all classes as named exports, this can be done if you enable the `namedExport`-option.
30+
e.g.
31+
```js
32+
{ test: /\.css$/, loader: 'typings-for-css-modules?modules&namedExport' }
33+
```
34+
35+
As mentioned above, this requires classnames to only contain valid typescript characters, thus filtering out all classnames that do not match /^\w+$/i. (feel free to improve that regexp)
36+
In order to make sure that even classnames with non-legal characters are used it is highly recommended to use the `camelCase`-option as well, that - once passed to the css-loader - makes sure all classnames are transformed to valid variables.
37+
with:
38+
```js
39+
{ test: /\.css$/, loader: 'typings-for-css-modules?modules&namedExport&camelCase' }
40+
```
41+
using the following css:
42+
```css
43+
.foo {
44+
color: white;
45+
}
46+
47+
.bar-baz {
48+
color: green;
49+
}
50+
```
51+
52+
will generate the following typings file:
53+
```ts
54+
export const foo: string;
55+
export const barBaz: string;
56+
```
57+
958
## Usage
1059

1160
Keep your `webpack.config` as is just instead of using `css-loader` use `typings-for-css-modules-loader`
@@ -30,7 +79,7 @@ webpackConfig.module.loaders: [
3079
## Example
3180

3281
Imagine you have a file `~/my-project/src/component/MyComponent/component.scss` in your project with the following content:
33-
```
82+
```css
3483
.some-class {
3584
// some styles
3685
&.someOtherClass {
@@ -43,7 +92,7 @@ Imagine you have a file `~/my-project/src/component/MyComponent/component.scss`
4392
```
4493

4594
Adding the `typings-for-css-modules-loader` will generate a file `~/my-project/src/component/MyComponent/mycomponent.scss.d.ts` that has the following content:
46-
```
95+
```ts
4796
export interface IMyComponentScss {
4897
'some-class': string;
4998
'someOtherClass': string;
@@ -54,6 +103,14 @@ declare const styles: IMyComponentScss;
54103
export default styles;
55104
```
56105

106+
### using `namedExport`
107+
Using the `namedExport`-option the generated file will look as follow:
108+
```ts
109+
export const someClass: string;
110+
export const someOtherClass: string;
111+
export const someClassSayWhat: string;
112+
```
113+
57114
### Example in Visual Studio Code
58115
![typed-css-modules](https://cloud.githubusercontent.com/assets/749171/16340497/c1cb6888-3a28-11e6-919b-f2f51a282bba.gif)
59116

0 commit comments

Comments
 (0)