Skip to content

Commit 2d587d8

Browse files
author
Alexander Grigoriev
committed
Remove unnecessary option namedExport
1 parent 920e374 commit 2d587d8

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

README.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ yarn add -D dts-css-modules-loader
1616
{
1717
loader: 'dts-css-modules-loader',
1818
options: {
19-
namedExport: true,
2019
banner: "// This file is generated automatically"
2120
}
2221
},
2322
{
2423
loader: 'css-loader',
2524
options: {
26-
modules: true, // this option must be enabled
27-
camelCase: 'only',
25+
modules: true, // enable the CSS Modules
26+
exportOnlyLocals: true // export class names as variables
27+
camelCase: 'only', // generate valid name of variables
2828
localIdentName: '[local]',
29-
exportOnlyLocals: true
3029
}
3130
},
3231
'sass-loader'
@@ -35,32 +34,38 @@ yarn add -D dts-css-modules-loader
3534
```
3635

3736
## Options
38-
### `namedExport`
39-
When the option is switched on classes exported as variables. Be sure you using `camelCase` option of [css-loader](https://github.com/webpack-contrib/css-loader) to avoid invalid name of variables.
37+
### `banner`
38+
Adds a "banner" prefix to each generated file.
39+
40+
## css-loader
41+
As loader uses output of `css-loader`, generated typings depends on it's options.
4042

43+
When [exportOnlyLocals](https://github.com/webpack-contrib/css-loader#exportonlylocals) is on, class names exported as variables:
4144
```ts
42-
// This file is generated automatically.
4345
export const button: string;
4446
export const buttonActive: string;
4547
```
48+
Be sure you using [camelCase](https://github.com/webpack-contrib/css-loader#camelcase) to avoid invalid name of variables.
4649

47-
When option is off:
50+
When option is off, will be generated following typings:
4851
```ts
49-
// This file is generated automatically.
5052
export interface I_buttonScss {
51-
'button': string
52-
'buttonActive': string
53+
'paButton': string;
54+
'paButtonActive': string;
5355
}
54-
declare const styles: I_buttonScss;
55-
export default styles;
56+
export const locals: I_buttonScss;
5657
```
5758

58-
### `banner`
59-
Adds a "banner" prefix to each generated file.
60-
6159
## Usage in Typescript
60+
61+
With `exportOnlyLocals`:
62+
```ts
63+
import * as classes from './_button.scss';
64+
```
65+
66+
Without:
6267
```ts
63-
import * as styles from './_button.scss';
68+
import { locals as classes } from './_button.scss';
6469
```
6570

6671
To avoid errors about the absent module, you need to determine this:
@@ -73,7 +78,7 @@ declare module '*.scss' {
7378
export = classes;
7479
}
7580
```
76-
When you add new classname Typescript compiler may not find the generated variable so you need to compile twice your files.
81+
When you add new class name, Typescript compiler may not find the generated variable so you need to compile twice your files.
7782

7883
## License
7984
Licensed under the MIT license.

index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ module.exports = function(content) {
1717
}
1818

1919
{
20-
const classes = getClasses(content);
20+
const { classes, exportOnlyLocals } = getClasses(content);
2121

22-
if (options.namedExport) {
22+
if (exportOnlyLocals) {
2323
for (let c of classes) {
2424
typings += `export const ${c}: string;\n`;
2525
}
@@ -29,7 +29,7 @@ module.exports = function(content) {
2929
for (let c of classes) {
3030
typings += ` '${c}': string;\n`;
3131
}
32-
typings += `}\ndeclare const styles: ${i};\nexport default styles;\n`;
32+
typings += `}\nexport const locals: ${i};\n`;
3333
}
3434
}
3535

@@ -48,13 +48,16 @@ function getClasses(content) {
4848

4949
/** @type {string[]} */
5050
let classes = [];
51+
let exportOnlyLocals = true;
5152

52-
// when `exportOnlyLocals` is on
53+
// check `exportOnlyLocals` is on
5354
let from = content.indexOf('module.exports = {');
54-
// when `exportOnlyLocals` is off
55-
from = ~from ? from : content.indexOf('exports.locals = {');
55+
if (from === -1) {
56+
exportOnlyLocals = false;
57+
from = content.indexOf('exports.locals = {');
58+
}
5659

57-
if (~from) {
60+
if (from !== -1) {
5861
content = content.substr(from);
5962

6063
/** @type {RegExpExecArray} */
@@ -66,7 +69,10 @@ function getClasses(content) {
6669
}
6770
}
6871

69-
return classes;
72+
return {
73+
classes,
74+
exportOnlyLocals
75+
};
7076
}
7177

7278
const classesRegex = /"([^"\\/;()\n]+)":/g;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dts-css-modules-loader",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "webpack loader to generate typings for css modules",
55
"dependencies": {
66
"loader-utils": "^1.2.0"

0 commit comments

Comments
 (0)