Skip to content

Commit 1424f6a

Browse files
committed
Support css-loader 4
Closes #16
1 parent 13be893 commit 1424f6a

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# dts-css-modules-loader
2-
Replacement for the [typings-for-css-modules-loader](https://github.com/Jimdo/typings-for-css-modules-loader). This loader does not make any changes in content of styles, just creates `*.d.ts` file during the work. It is assumed that the content will be preprocessed first by [css-loader](https://github.com/webpack-contrib/css-loader). Currently supported versions 2 and 3 of the `css-loader`.
2+
Replacement for the [typings-for-css-modules-loader](https://github.com/Jimdo/typings-for-css-modules-loader). This loader does not make any changes in content of styles, just creates `*.d.ts` file during the work. It is assumed that the content will be preprocessed first by [css-loader](https://github.com/webpack-contrib/css-loader). Currently supported versions 2, 3 and 4 of the `css-loader`.
33

44
## Installation
55
```bash
@@ -13,6 +13,7 @@ yarn add -D dts-css-modules-loader
1313
{
1414
test: /\.scss$/,
1515
use: [
16+
'style-loader',
1617
{
1718
loader: 'dts-css-modules-loader',
1819
options: {
@@ -23,18 +24,17 @@ yarn add -D dts-css-modules-loader
2324
{
2425
loader: 'css-loader',
2526
options: {
26-
modules: { // this option must be enabled
27+
// options for the v4 of css-loader
28+
modules: {
29+
exportLocalsConvention: 'camelCaseOnly',
2730
localIdentName: '[local]'
2831
}
29-
localsConvention: 'camelCaseOnly',
30-
onlyLocals: true
3132
}
3233
},
3334
'sass-loader'
3435
]
3536
}
3637
```
37-
`css-loader` options presented for the third version.
3838

3939
## Options
4040
### `namedExport`
@@ -54,7 +54,7 @@ export interface I_buttonScss {
5454
'buttonActive': string
5555
}
5656
declare const styles: I_buttonScss;
57-
export default styles;
57+
export = styles;
5858
```
5959

6060
### `banner`

index.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module.exports = function(content) {
1818

1919
{
2020
const classes = getClasses(content);
21-
2221
if (options.namedExport) {
2322
for (let c of classes) {
2423
typings += `export const ${c}: string;\n`;
@@ -29,7 +28,7 @@ module.exports = function(content) {
2928
for (let c of classes) {
3029
typings += ` '${c}': string;\n`;
3130
}
32-
typings += `}\ndeclare const styles: ${i};\nexport default styles;\n`;
31+
typings += `}\ndeclare const styles: ${i};\nexport = styles;\n`;
3332
}
3433
}
3534

@@ -48,18 +47,31 @@ function getClasses(content) {
4847

4948
/** @type {string[]} */
5049
let classes = [];
50+
let isCssLoader4NamedExport = false;
5151

52-
// when `onlyLocals` is on
53-
let from = content.indexOf('module.exports = {');
54-
// when `onlyLocals` is off
55-
from = ~from ? from : content.indexOf('exports.locals = {');
52+
// check v4
53+
let from = content.indexOf('___CSS_LOADER_EXPORT___.locals = {');
54+
if (from === -1) {
55+
from = content.indexOf('export const ');
56+
isCssLoader4NamedExport = from !== -1;
57+
}
58+
// check v3
59+
if (from === -1) {
60+
// when `onlyLocals` is on
61+
from = content.indexOf('module.exports = {');
62+
}
63+
if (from === -1) {
64+
// when `onlyLocals` is off
65+
from = content.indexOf('exports.locals = {');
66+
}
5667

5768
if (~from) {
5869
content = content.substr(from);
5970

6071
/** @type {RegExpExecArray} */
6172
let match;
62-
while (match = classesRegex.exec(content)) {
73+
const regex = isCssLoader4NamedExport ? classesOfNamedExportRegex : classesRegex;
74+
while (match = regex.exec(content)) {
6375
if (classes.indexOf(match[1]) === -1) {
6476
classes.push(match[1]);
6577
}
@@ -70,6 +82,7 @@ function getClasses(content) {
7082
}
7183

7284
const classesRegex = /"([^"\\/;()\n]+)":/g;
85+
const classesOfNamedExportRegex = /export const (\w+) =/g;
7386

7487
/**
7588
* @param {string} [path]

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "dts-css-modules-loader",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "webpack loader to generate typings for css modules",
55
"dependencies": {
6-
"loader-utils": "^1.2.0"
6+
"loader-utils": "^2.0.0"
77
},
88
"peerDependencies": {
9-
"css-loader": "^2.0.0 || ^3.0.0"
9+
"css-loader": "^2.0.0 || ^3.0.0 || ^4.0.0"
1010
},
1111
"keywords": [
1212
"css-modules",

0 commit comments

Comments
 (0)