Skip to content

Commit 11e89ff

Browse files
authored
Merge pull request #31 from IvanovRoman/master
Got rid of (loader-utils).getOptions in favor loaderContext.getOptions (loaderContext is this inside loader function)
2 parents 3a26b1e + 4b563a3 commit 11e89ff

File tree

5 files changed

+540
-69
lines changed

5 files changed

+540
-69
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# dts-css-modules-loader
22
A small Webpack loader to generate typings for your CSS-Modules. Created as a replacement for the frozend [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).
33

4+
### ⚠ BREAKING CHANGES
5+
6+
* Since version 2.x only supports `webpack` version is `5`
7+
48
## Installation
59
```bash
610
npm i -D dts-css-modules-loader

index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
// @ts-check
22
const fs = require('fs');
33
const fp = require('path');
4-
const loaderUtils = require('loader-utils');
4+
const schema = require("./options.json");
55

6-
/** @type {import('webpack').loader.Loader} */
6+
/**
7+
* @template T
8+
* @typedef {Object} LoaderOptions<T>
9+
* @type {{
10+
* banner?: string,
11+
* namedExport?: boolean,
12+
* customTypings?: (classes: string[]) => string,
13+
* dropEmptyFile?: boolean
14+
* }}
15+
* @property {string} [severityError] Allows to choose how errors are displayed.
16+
*/
17+
18+
/**
19+
* @template T
20+
* @this {import("webpack").LoaderContext<LoaderOptions<T>>}
21+
* @param {Buffer} content
22+
*/
723
module.exports = function (content) {
824
this.cacheable && this.cacheable();
925

@@ -15,7 +31,7 @@ module.exports = function (content) {
1531
* dropEmptyFile?: boolean
1632
* }}
1733
*/
18-
const options = loaderUtils.getOptions(this) || {};
34+
const options = this.getOptions(schema) || {};
1935
const callback = this.async();
2036

2137
const classes = getClasses(content);
@@ -85,7 +101,7 @@ function getClasses(content) {
85101
}
86102

87103
if (~from) {
88-
content = content.substr(from);
104+
content = content.slice(from);
89105

90106
/** @type {RegExpExecArray} */
91107
let match;

options.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"defintions": {
3+
"banner": {
4+
"description": "When the option is switched on classes exported as variables.",
5+
"type": "string"
6+
},
7+
"namedExport": {
8+
"description": "Adds a 'banner' prefix to each generated file",
9+
"type": "boolean"
10+
},
11+
"customTypings": {
12+
"description": "Function that accepts classes (an array of string) and returns the content of declaration file",
13+
"instanceof": "Function"
14+
},
15+
"dropEmptyFile": {
16+
"description": "If there are no classes, the typings file will not be generated, and the existing will be deleted",
17+
"type": "boolean"
18+
}
19+
}
20+
}

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
22
"name": "dts-css-modules-loader",
3-
"version": "1.2.5",
3+
"version": "2.0.0",
44
"description": "webpack loader to generate typings for css modules",
5-
"dependencies": {
6-
"loader-utils": "^2.0.0"
7-
},
85
"peerDependencies": {
96
"css-loader": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
107
},
@@ -18,6 +15,6 @@
1815
"license": "MIT",
1916
"homepage": "https://github.com/Megaputer/dts-css-modules-loader",
2017
"devDependencies": {
21-
"@types/webpack": "^4.41.26"
18+
"@types/webpack": "^5.28.0"
2219
}
2320
}

0 commit comments

Comments
 (0)