Skip to content
This repository was archived by the owner on Oct 30, 2020. It is now read-only.

Commit 91502ff

Browse files
authored
Merge pull request #17 from Jimdo/allow-to-silence-logger
Allow to silence logger
2 parents eca0946 + 6b78ef6 commit 91502ff

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ export const barBaz: string;
5757

5858
`css-loader` exports mappings to `exports.locals` which is incompatible with the `namedExport`-option unless paired with `extract-text-webpack-plugin` or `style-loader`. They move the exported properties from `exports.locals` to `exports` making them reuired for `namedExport` to work, and `namedExport` required for them to work. *Always combine usage of `extract-text-webpack-plugin` or `style-loader` with the `namedExport`-option.*
5959

60+
### `silent`-option
61+
To silence the loader because you get annoyed by its warnings or for other reasons, you can simply pass the "silent" query to the loader and it will shut up.
62+
e.g.:
63+
64+
```js
65+
{ test: /\.css$/, loader: 'typings-for-css-modules?silent' }
66+
```
67+
6068
## Usage
6169

6270
Keep your `webpack.config` as is just instead of using `css-loader` use `typings-for-css-modules-loader`

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"prepublish": "npm run build",
99
"pretest": "rm -f ./test/example*.css.d.ts",
1010
"test:diff": "(cd test; set -e; for f in example*css.d.ts; do diff $f expected-$f; done;)",
11-
"test:run": "babel-node ./node_modules/webpack/bin/webpack --config ./test/webpack.config.babel.js && npm run test:diff",
12-
"test": "npm run test:run > /dev/null 2>&1 && npm run test:run"
11+
"test:run": "babel-node ./node_modules/webpack/bin/webpack --config ./test/webpack.config.babel.js",
12+
"test:run-twice" : "npm run test:run > /dev/null 2>&1 && npm run test:run",
13+
"test": "npm run test:run-twice && npm run test:diff"
1314
},
1415
"author": "Tim Sebastian <[email protected]>",
1516
"license": "MIT",

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
filenameToTypingsFilename,
1111
} from './cssModuleToInterface';
1212
import * as persist from './persist';
13+
import loggerCreator from './logger';
1314

1415
function delegateToCssLoader(ctx, input, callback) {
1516
ctx.async = () => callback;
@@ -23,9 +24,11 @@ module.exports = function(input) {
2324
const callback = this.async();
2425

2526
const query = loaderUtils.parseQuery(this.query);
27+
const logger = loggerCreator(query.silent);
28+
2629
const moduleMode = query.modules || query.module;
2730
if (!moduleMode) {
28-
console.warn('Typings for CSS-Modules: option `modules` is not active - skipping extraction work...'.red);
31+
logger('warn','Typings for CSS-Modules: option `modules` is not active - skipping extraction work...'.red);
2932
return delegateToCssLoader(this, input, callback);
3033
}
3134

@@ -45,7 +48,7 @@ module.exports = function(input) {
4548
} else {
4649
const [cleanedDefinitions, skippedDefinitions,] = filterNonWordClasses(cssModuleKeys);
4750
if (skippedDefinitions.length > 0 && !query.camelCase) {
48-
console.warn(`Typings for CSS-Modules: option 'namedExport' was set but 'camelCase' for the css-loader not.
51+
logger('warn', `Typings for CSS-Modules: option 'namedExport' was set but 'camelCase' for the css-loader not.
4952
The following classes will not be available as named exports:
5053
${skippedDefinitions.map(sd => ` - "${sd}"`).join('\n').red}
5154
`.yellow);

src/logger.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default (silent) => {
2+
if (silent) {
3+
return () => {};
4+
}
5+
return (level, ...args) => console[level](...args);
6+
};

test/entry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {locals as stylesBase} from './example.css';
22
import {locals as stylesCamelCase} from './example-camelcase.css';
33
import * as stylesNamedExport from './example-namedexport.css';
44
import * as stylesCamelCasedNamedExport from './example-camelcase-namedexport.css';
5+
import './example-no-css-modules.css';
56

67
const foo = stylesBase.foo;
78
const barBaz = stylesBase['bar-baz'];

test/example-no-css-modules.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.foo {
2+
color: white;
3+
}
4+
5+
.bar-baz {
6+
color: green;
7+
}

test/webpack.config.babel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module.exports = {
1010
{ test: /example.css$/, loader: '../src/index.js?modules' },
1111
{ test: /example-camelcase.css$/, loader: '../src/index.js?modules&camelCase' },
1212
{ test: /example-namedexport.css$/, loader: '../src/index.js?modules&namedExport' },
13-
{ test: /example-camelcase-namedexport.css$/, loader: '../src/index.js?modules&camelCase&namedExport' }
13+
{ test: /example-camelcase-namedexport.css$/, loader: '../src/index.js?modules&camelCase&namedExport' },
14+
{ test: /example-no-css-modules.css$/, loader: '../src/index.js' },
1415
]
1516
}
1617
};

0 commit comments

Comments
 (0)