You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Webpack loader that works as a css-loader drop-in replacement to generate TypeScript typings for CSS modules on the fly
7
+
Webpack loader that generates TypeScript typings for CSS modules from css-loader on the fly
4
8
5
9
## Disclaimer
6
10
@@ -10,105 +14,115 @@ This repository is a fork of the unmaintained https://github.com/Jimdo/typings-f
10
14
11
15
Install via npm `npm install --save-dev @teamsupercell/typings-for-css-modules-loader`
12
16
13
-
## Options
14
-
15
-
Just like any other loader you can specify options e.g. as query-params
16
-
17
-
### css-loader options
18
-
Any option that your installed version of css-loader supports can be used and will be passed to it.
19
-
20
-
### `namedExport`-option
21
-
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.
22
-
e.g.:
23
-
```ts
24
-
exportinterfaceIExampleCss {
25
-
'foo':string;
26
-
'bar-baz':string;
27
-
}
28
-
declareconst styles:IExampleCss;
29
-
30
-
exportdefaultstyles;
31
-
```
32
-
33
-
A cleaner way is to expose all classes as named exports, this can be done if you enable the `namedExport`-option.
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)
40
-
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.
|**[`banner`](#banner)**|`{String}`| To add a 'banner' prefix to each generated `*.d.ts` file |
41
+
|**[`formatter`](#formatter)**|`{String}`| Formats the generated `*.d.ts` file with specified formatter, eg. `prettier`|
42
+
|**[`eol`](#eol)**|`{String}`| Newline character to be used in generated `*.d.ts` files |
61
43
62
-
`css-loader` exports mappings to `exports.locals` which is incompatible with the `namedExport`-option unless paired with `extract-text-webpack-plugin` or `style-loader`. They move the exported properties from `exports.locals` to `exports` making them required for `namedExport` to work, and `namedExport` required for them to work. *Always combine usage of `extract-text-webpack-plugin` or `style-loader` with the `namedExport`-option.*
44
+
### `banner`
63
45
64
-
### `orderAlphabetically`-option
65
-
Orders generated exports or interface properties alphabetically. This is useful when committing the .d.ts files as the default ordering is not always consistent and can change from commit to commit.
66
-
e.g.:
46
+
To add a "banner" prefix to each generated `*.d.ts` file, you can pass a string to this option as shown below. The prefix is quite literally prefixed into the generated file, so please ensure it conforms to the type definition syntax.
"// autogenerated by typings-for-css-modules-loader. \n// Please do not change this file!"
60
+
}
61
+
},
62
+
"css-loader"
63
+
]
64
+
}
65
+
]
66
+
}
67
+
};
70
68
```
71
69
72
-
### `silent`-option
73
-
To silence the loader because you get annoyed by its warnings or for other reasons, you can simply pass the "silent" query to the loader and it will shut up.
To add a "banner" prefix to each generated `*.d.ts` file, you can pass a string to this option as shown below. The prefix is quite literally prefixed into the generated file, so please ensure it conforms to the type definition syntax.
72
+
Possible options: `none` and `prettier` (requires `prettier` package to be installed). Defaults to prettier if `prettier` module can be resolved.
82
73
83
74
```js
84
-
{ test:/\.css$/, loader:'typings-for-css-modules?banner="// This file is automatically generated by typings-for-css-modules.\n// Please do not change this file!"' }
Imagine you have a file `~/my-project/src/component/MyComponent/myComponent.scss` in your project with the following content:
111
-
```css
124
+
125
+
```scss
112
126
.some-class {
113
127
// some styles
114
128
&.someOtherClass {
@@ -121,45 +135,46 @@ Imagine you have a file `~/my-project/src/component/MyComponent/myComponent.scss
121
135
```
122
136
123
137
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:
138
+
124
139
```ts
125
140
exportinterfaceIMyComponentScss {
126
-
'some-class':string;
127
-
'someOtherClass':string;
128
-
'some-class-sayWhat':string;
141
+
"some-class":string;
142
+
someOtherClass:string;
143
+
"some-class-sayWhat":string;
129
144
}
130
-
declareconst styles:IMyComponentScss;
131
145
132
-
exportdefaultstyles;
146
+
exportconst locals:IExampleCss;
147
+
exportdefaultlocals;
133
148
```
134
149
135
-
### using `namedExport` with the `camelCase`-option
136
-
Using the `namedExport` as well as the `camelCase` options the generated file will look as follow:
137
150
```ts
138
-
exportconst someClass:string;
139
-
exportconst someOtherClass:string;
140
-
exportconst someClassSayWhat:string;
151
+
// using default export when used with style-loader or mini-css-extract-plugin
As the loader just acts as an intermediary it can handle all kind of css preprocessors (`sass`, `scss`, `stylus`, `less`, ...).
158
173
The only requirement is that those preprocessors have proper webpack loaders defined - meaning they can already be loaded by webpack anyways.
159
174
160
175
## Requirements
161
176
162
-
The loader uses `css-loader`(https://github.com/webpack/css-loader) under the hood. Thus it is a peer-dependency and the expected loader to create CSS Modules.
177
+
The loader is supposed to be used with `css-loader`(https://github.com/webpack/css-loader). Thus it is a peer-dependency and the expected loader to create CSS Modules.
163
178
164
179
## Known issues
165
180
@@ -168,6 +183,7 @@ The loader uses `css-loader`(https://github.com/webpack/css-loader) under the ho
168
183
As the loader generates typing files, it is wise to tell webpack to ignore them.
169
184
The fix is luckily very simple. Webpack ships with a "WatchIgnorePlugin" out of the box.
170
185
Simply add this to your webpack plugins:
186
+
171
187
```
172
188
plugins: [
173
189
new webpack.WatchIgnorePlugin([
@@ -176,9 +192,18 @@ plugins: [
176
192
...
177
193
]
178
194
```
195
+
179
196
where `css` is the file extension of your style files. If you use `sass` you need to put `sass` here instead. If you use `less`, `stylus` or any other style language use their file ending.
180
197
181
-
### Typescript doesnt find the typings
198
+
### Typescript does not find the typings
182
199
183
200
As the webpack process is independent from your typescript "runtime" it may take a while for typescript to pick up the typings.
184
-
Any hints on how this could be fixed deterministically are welcome!
201
+
202
+
It is possible to write a custom webpack plugin using the `fork-ts-checker-service-before-start` hook from https://github.com/TypeStrong/fork-ts-checker-webpack-plugin#plugin-hooks to delay the start of type checking until all the `*.d.ts` files are generated. Potentially, this plugin can be included in this repository.
0 commit comments