Skip to content

Commit 07a2c91

Browse files
committed
Add new option dropEmptyFile
Closes #21
1 parent 66ca551 commit 07a2c91

File tree

4 files changed

+97
-19
lines changed

4 files changed

+97
-19
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ yarn add -D dts-css-modules-loader
1313
{
1414
test: /\.scss$/,
1515
use: [
16-
'style-loader',
16+
{
17+
loader: 'style-loader',
18+
options: {
19+
esModule: false,
20+
},
21+
},
1722
{
1823
loader: 'dts-css-modules-loader',
1924
options: {
20-
namedExport: true,
21-
banner: "// This file is generated automatically"
25+
namedExport: true
2226
}
2327
},
2428
{
2529
loader: 'css-loader',
2630
options: {
27-
// options for the v4 of css-loader
31+
// options for the v5 of css-loader
2832
modules: {
2933
exportLocalsConvention: 'camelCaseOnly',
3034
localIdentName: '[local]'
@@ -72,7 +76,10 @@ customTypings: classes => {
7276
return content;
7377
}
7478
```
75-
`namedExport` option will be ignored
79+
`namedExport` and `banner` option will be ignored
80+
81+
### `dropEmptyFile`
82+
If there are no classes, the typings file will not be generated, and the existing will be deleted.
7683

7784
## Usage in Typescript
7885
```ts

index.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@ const fp = require('path');
44
const loaderUtils = require('loader-utils');
55

66
/** @type {import('webpack').loader.Loader} */
7-
module.exports = function(content) {
7+
module.exports = function (content) {
88
this.cacheable && this.cacheable();
99

10-
/** @type {{ banner?: string, namedExport?: boolean, customTypings?: (classes: string[]) => string }} */
10+
/**
11+
* @type {{
12+
* banner?: string,
13+
* namedExport?: boolean,
14+
* customTypings?: (classes: string[]) => string,
15+
* dropEmptyFile?: boolean
16+
* }}
17+
*/
1118
const options = loaderUtils.getOptions(this) || {};
1219
const callback = this.async();
1320

14-
let typings = '';
21+
const classes = getClasses(content);
22+
const dtsPath = getDtsPath(this.resourcePath);
1523

16-
if (options.banner) {
17-
typings = `${options.banner}\n`;
18-
}
24+
if (options.dropEmptyFile && classes.length === 0) {
25+
if (fs.existsSync(dtsPath)) {
26+
fs.rmSync(dtsPath);
27+
}
28+
} else {
29+
let typings = options.banner ? `${options.banner}\n` : '';
1930

20-
{
21-
const classes = getClasses(content);
2231
if (options.customTypings) {
2332
typings = options.customTypings(classes);
2433
} else if (options.namedExport) {
@@ -33,9 +42,9 @@ module.exports = function(content) {
3342
}
3443
typings += `}\ndeclare const styles: ${i};\nexport = styles;\n`;
3544
}
36-
}
3745

38-
fs.writeFileSync(getDtsPath(this.resourcePath), typings);
46+
fs.writeFileSync(dtsPath, typings);
47+
}
3948

4049
callback(null, content);
4150
};
@@ -74,7 +83,7 @@ function getClasses(content) {
7483
/** @type {RegExpExecArray} */
7584
let match;
7685
const regex = isCssLoader4NamedExport ? classesOfNamedExportRegex : classesRegex;
77-
while (match = regex.exec(content)) {
86+
while ((match = regex.exec(content))) {
7887
if (classes.indexOf(match[1]) === -1) {
7988
classes.push(match[1]);
8089
}
@@ -98,7 +107,8 @@ function getDtsPath(path) {
98107
* @param {string} [path]
99108
*/
100109
function getInterfaceName(path) {
101-
return fp.basename(path)
110+
return fp
111+
.basename(path)
102112
.replace(/^(\w)/, (_, c) => 'I' + c.toUpperCase())
103113
.replace(/\W+(\w)/g, (_, c) => c.toUpperCase());
104114
}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dts-css-modules-loader",
3-
"version": "1.1.1",
3+
"version": "1.2.1",
44
"description": "webpack loader to generate typings for css modules",
55
"dependencies": {
66
"loader-utils": "^2.0.0"
@@ -16,5 +16,8 @@
1616
"loader"
1717
],
1818
"license": "MIT",
19-
"homepage": "https://github.com/Megaputer/dts-css-modules-loader"
19+
"homepage": "https://github.com/Megaputer/dts-css-modules-loader",
20+
"devDependencies": {
21+
"@types/webpack": "^4.41.26"
22+
}
2023
}

yarn.lock

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,54 @@
22
# yarn lockfile v1
33

44

5+
"@types/anymatch@*":
6+
version "1.3.1"
7+
resolved "https://registry.yarnpkg.com/@types/anymatch/-/anymatch-1.3.1.tgz#336badc1beecb9dacc38bea2cf32adf627a8421a"
8+
integrity sha512-/+CRPXpBDpo2RK9C68N3b2cOvO0Cf5B9aPijHsoDQTHivnGSObdOF2BRQOYjojWTDy6nQvMjmqRXIxH55VjxxA==
9+
10+
"@types/node@*":
11+
version "14.14.35"
12+
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.35.tgz#42c953a4e2b18ab931f72477e7012172f4ffa313"
13+
integrity sha512-Lt+wj8NVPx0zUmUwumiVXapmaLUcAk3yPuHCFVXras9k5VT9TdhJqKqGVUQCD60OTMCl0qxJ57OiTL0Mic3Iag==
14+
15+
"@types/source-list-map@*":
16+
version "0.1.2"
17+
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
18+
integrity sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==
19+
20+
"@types/tapable@*":
21+
version "1.0.6"
22+
resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.6.tgz#a9ca4b70a18b270ccb2bc0aaafefd1d486b7ea74"
23+
integrity sha512-W+bw9ds02rAQaMvaLYxAbJ6cvguW/iJXNT6lTssS1ps6QdrMKttqEAMEG/b5CR8TZl3/L7/lH0ZV5nNR1LXikA==
24+
25+
"@types/uglify-js@*":
26+
version "3.13.0"
27+
resolved "https://registry.yarnpkg.com/@types/uglify-js/-/uglify-js-3.13.0.tgz#1cad8df1fb0b143c5aba08de5712ea9d1ff71124"
28+
integrity sha512-EGkrJD5Uy+Pg0NUR8uA4bJ5WMfljyad0G+784vLCNUkD+QwOJXUbBYExXfVGf7YtyzdQp3L/XMYcliB987kL5Q==
29+
dependencies:
30+
source-map "^0.6.1"
31+
32+
"@types/webpack-sources@*":
33+
version "2.1.0"
34+
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-2.1.0.tgz#8882b0bd62d1e0ce62f183d0d01b72e6e82e8c10"
35+
integrity sha512-LXn/oYIpBeucgP1EIJbKQ2/4ZmpvRl+dlrFdX7+94SKRUV3Evy3FsfMZY318vGhkWUS5MPhtOM3w1/hCOAOXcg==
36+
dependencies:
37+
"@types/node" "*"
38+
"@types/source-list-map" "*"
39+
source-map "^0.7.3"
40+
41+
"@types/webpack@^4.41.26":
42+
version "4.41.26"
43+
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.41.26.tgz#27a30d7d531e16489f9c7607c747be6bc1a459ef"
44+
integrity sha512-7ZyTfxjCRwexh+EJFwRUM+CDB2XvgHl4vfuqf1ZKrgGvcS5BrNvPQqJh3tsZ0P6h6Aa1qClVHaJZszLPzpqHeA==
45+
dependencies:
46+
"@types/anymatch" "*"
47+
"@types/node" "*"
48+
"@types/tapable" "*"
49+
"@types/uglify-js" "*"
50+
"@types/webpack-sources" "*"
51+
source-map "^0.6.0"
52+
553
big.js@^5.2.2:
654
version "5.2.2"
755
resolved "https://registry.yarnpkg.com/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328"
@@ -32,3 +80,13 @@ minimist@^1.2.5:
3280
version "1.2.5"
3381
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
3482
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
83+
84+
source-map@^0.6.0, source-map@^0.6.1:
85+
version "0.6.1"
86+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
87+
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
88+
89+
source-map@^0.7.3:
90+
version "0.7.3"
91+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
92+
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

0 commit comments

Comments
 (0)