Skip to content

Got rid of (loader-utils).getOptions in favor loaderContext.getOptions (loaderContext is this inside loader function) #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# dts-css-modules-loader
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).

### ⚠ BREAKING CHANGES

* Since version 2.x only supports `webpack` version is `5`

## Installation
```bash
npm i -D dts-css-modules-loader
Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
// @ts-check
const fs = require('fs');
const fp = require('path');
const loaderUtils = require('loader-utils');
const schema = require("./options.json");

/** @type {import('webpack').loader.Loader} */
/**
* @template T
* @typedef {Object} LoaderOptions<T>
* @type {{
* banner?: string,
* namedExport?: boolean,
* customTypings?: (classes: string[]) => string,
* dropEmptyFile?: boolean
* }}
* @property {string} [severityError] Allows to choose how errors are displayed.
*/

/**
* @template T
* @this {import("webpack").LoaderContext<LoaderOptions<T>>}
* @param {Buffer} content
*/
module.exports = function (content) {
this.cacheable && this.cacheable();

Expand All @@ -15,7 +31,7 @@ module.exports = function (content) {
* dropEmptyFile?: boolean
* }}
*/
const options = loaderUtils.getOptions(this) || {};
const options = this.getOptions(schema) || {};
const callback = this.async();

const classes = getClasses(content);
Expand Down Expand Up @@ -85,7 +101,7 @@ function getClasses(content) {
}

if (~from) {
content = content.substr(from);
content = content.slice(from);

/** @type {RegExpExecArray} */
let match;
Expand Down
20 changes: 20 additions & 0 deletions options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"defintions": {
"banner": {
"description": "When the option is switched on classes exported as variables.",
"type": "string"
},
"namedExport": {
"description": "Adds a 'banner' prefix to each generated file",
"type": "boolean"
},
"customTypings": {
"description": "Function that accepts classes (an array of string) and returns the content of declaration file",
"instanceof": "Function"
},
"dropEmptyFile": {
"description": "If there are no classes, the typings file will not be generated, and the existing will be deleted",
"type": "boolean"
}
}
}
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"name": "dts-css-modules-loader",
"version": "1.2.5",
"version": "2.0.0",
"description": "webpack loader to generate typings for css modules",
"dependencies": {
"loader-utils": "^2.0.0"
},
"peerDependencies": {
"css-loader": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
},
Expand All @@ -18,6 +15,6 @@
"license": "MIT",
"homepage": "https://github.com/Megaputer/dts-css-modules-loader",
"devDependencies": {
"@types/webpack": "^4.41.26"
"@types/webpack": "^5.28.0"
}
}
Loading