Skip to content

Commit 690480c

Browse files
author
Alexander Grigoriev
committed
Revert "Remove unnecessary option namedExport"
This reverts commit 2d587d8.
1 parent 2d587d8 commit 690480c

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

README.md

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ yarn add -D dts-css-modules-loader
1616
{
1717
loader: 'dts-css-modules-loader',
1818
options: {
19+
namedExport: true,
1920
banner: "// This file is generated automatically"
2021
}
2122
},
2223
{
2324
loader: 'css-loader',
2425
options: {
25-
modules: true, // enable the CSS Modules
26-
exportOnlyLocals: true // export class names as variables
27-
camelCase: 'only', // generate valid name of variables
26+
modules: true, // this option must be enabled
27+
camelCase: 'only',
2828
localIdentName: '[local]',
29+
exportOnlyLocals: true
2930
}
3031
},
3132
'sass-loader'
@@ -34,38 +35,32 @@ yarn add -D dts-css-modules-loader
3435
```
3536

3637
## Options
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.
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.
4240

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

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

59-
## Usage in Typescript
60-
61-
With `exportOnlyLocals`:
62-
```ts
63-
import * as classes from './_button.scss';
64-
```
58+
### `banner`
59+
Adds a "banner" prefix to each generated file.
6560

66-
Without:
61+
## Usage in Typescript
6762
```ts
68-
import { locals as classes } from './_button.scss';
63+
import * as styles from './_button.scss';
6964
```
7065

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

8378
## License
8479
Licensed under the MIT license.

index.js

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

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

22-
if (exportOnlyLocals) {
22+
if (options.namedExport) {
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 += `}\nexport const locals: ${i};\n`;
32+
typings += `}\ndeclare const styles: ${i};\nexport default styles;\n`;
3333
}
3434
}
3535

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

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

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

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

6360
/** @type {RegExpExecArray} */
@@ -69,10 +66,7 @@ function getClasses(content) {
6966
}
7067
}
7168

72-
return {
73-
classes,
74-
exportOnlyLocals
75-
};
69+
return classes;
7670
}
7771

7872
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.2",
3+
"version": "1.0.1",
44
"description": "webpack loader to generate typings for css modules",
55
"dependencies": {
66
"loader-utils": "^1.2.0"

0 commit comments

Comments
 (0)