Skip to content

Commit c652c79

Browse files
feat: added the warnRuleAsWarning option, allow to emit a warning on the @warn rule (#992)
1 parent bb7cef9 commit c652c79

9 files changed

+457
-16
lines changed

README.md

+67-7
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ Thankfully there are a two solutions to this problem:
119119

120120
## Options
121121

122-
| Name | Type | Default | Description |
123-
| :---------------------------------------: | :------------------: | :-------------------------------------: | :---------------------------------------------------------------- |
124-
| **[`implementation`](#implementation)** | `{Object\|String}` | `sass` | Setup Sass implementation to use. |
125-
| **[`sassOptions`](#sassoptions)** | `{Object\|Function}` | defaults values for Sass implementation | Options for Sass. |
126-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
127-
| **[`additionalData`](#additionaldata)** | `{String\|Function}` | `undefined` | Prepends/Appends `Sass`/`SCSS` code before the actual entry file. |
128-
| **[`webpackImporter`](#webpackimporter)** | `{Boolean}` | `true` | Enables/Disables the default Webpack importer. |
122+
| Name | Type | Default | Description |
123+
| :-------------------------------------------: | :------------------: | :-------------------------------------: | :---------------------------------------------------------------- |
124+
| **[`implementation`](#implementation)** | `{Object\|String}` | `sass` | Setup Sass implementation to use. |
125+
| **[`sassOptions`](#sassoptions)** | `{Object\|Function}` | defaults values for Sass implementation | Options for Sass. |
126+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
127+
| **[`additionalData`](#additionaldata)** | `{String\|Function}` | `undefined` | Prepends/Appends `Sass`/`SCSS` code before the actual entry file. |
128+
| **[`webpackImporter`](#webpackimporter)** | `{Boolean}` | `true` | Enables/Disables the default Webpack importer. |
129+
| **[`warnRuleAsWarning`](#warnruleaswarning)** | `{Boolean}` | `false` | Treats the `@warn` rule as a webpack warning. |
129130

130131
### `implementation`
131132

@@ -604,6 +605,65 @@ module.exports = {
604605
};
605606
```
606607

608+
### `warnRuleAsWarning`
609+
610+
Type: `Boolean`
611+
Default: `false`
612+
613+
Treats the `@warn` rule as a webpack warning.
614+
615+
> ℹ️ It will be `true` by default in the next major release.
616+
617+
**style.scss**
618+
619+
```scss
620+
$known-prefixes: webkit, moz, ms, o;
621+
622+
@mixin prefix($property, $value, $prefixes) {
623+
@each $prefix in $prefixes {
624+
@if not index($known-prefixes, $prefix) {
625+
@warn "Unknown prefix #{$prefix}.";
626+
}
627+
628+
-#{$prefix}-#{$property}: $value;
629+
}
630+
#{$property}: $value;
631+
}
632+
633+
.tilt {
634+
// Oops, we typo'd "webkit" as "wekbit"!
635+
@include prefix(transform, rotate(15deg), wekbit ms);
636+
}
637+
```
638+
639+
The presented code will throw webpack warning instead logging.
640+
641+
To ignore unnecessary warnings you can use the [ignoreWarnings](https://webpack.js.org/configuration/other-options/#ignorewarnings) option.
642+
643+
**webpack.config.js**
644+
645+
```js
646+
module.exports = {
647+
module: {
648+
rules: [
649+
{
650+
test: /\.s[ac]ss$/i,
651+
use: [
652+
"style-loader",
653+
"css-loader",
654+
{
655+
loader: "sass-loader",
656+
options: {
657+
warnRuleAsWarning: true,
658+
},
659+
},
660+
],
661+
},
662+
],
663+
},
664+
};
665+
```
666+
607667
## Examples
608668

609669
### Extracts CSS into separate files

src/SassError.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class SassError extends Error {
33
super();
44

55
this.name = "SassError";
6+
// TODO remove me in the next major release
67
this.originalSassError = sassError;
78
this.loc = {
89
line: sassError.line,

src/SassWarning.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class SassWarning extends Error {
2+
constructor(warning, options) {
3+
super(warning);
4+
5+
this.name = "SassWarning";
6+
this.hideStack = true;
7+
8+
if (options.span) {
9+
this.loc = {
10+
line: options.span.start.line,
11+
column: options.span.start.column,
12+
};
13+
}
14+
}
15+
}
16+
17+
export default SassWarning;

src/options.json

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
"description": "Enables/Disables default `webpack` importer.",
4949
"link": "https://github.com/webpack-contrib/sass-loader#webpackimporter",
5050
"type": "boolean"
51+
},
52+
"warnRuleAsWarning": {
53+
"description": "Treats the '@warn' rule as a webpack warning.",
54+
"link": "https://github.com/webpack-contrib/sass-loader#warnruleaswarning",
55+
"type": "boolean"
5156
}
5257
},
5358
"additionalProperties": false

src/utils.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path from "path";
44
import { klona } from "klona/full";
55
import async from "neo-async";
66

7+
import SassWarning from "./SassWarning";
8+
79
function getDefaultSassImplementation() {
810
let sassImplPkg = "sass";
911

@@ -229,6 +231,9 @@ async function getSassOptions(
229231
}
230232

231233
if (!options.logger) {
234+
// TODO set me to `true` by default in the next major release
235+
const needEmitWarning = loaderOptions.warnRuleAsWarning === true;
236+
232237
const logger = loaderContext.getLogger("sass-loader");
233238
const formatSpan = (span) =>
234239
`${span.url || "-"}:${span.start.line}:${span.start.column}: `;
@@ -262,7 +267,13 @@ async function getSassOptions(
262267
builtMessage += `\n\n${loggerOptions.stack}`;
263268
}
264269

265-
logger.warn(builtMessage);
270+
if (needEmitWarning) {
271+
loaderContext.emitWarning(
272+
new SassWarning(builtMessage, loggerOptions)
273+
);
274+
} else {
275+
logger.warn(builtMessage);
276+
}
266277
},
267278
};
268279
}

test/__snapshots__/validate-options.test.js.snap

+15-8
Original file line numberDiff line numberDiff line change
@@ -69,49 +69,56 @@ exports[`validate options should throw an error on the "sourceMap" option with "
6969
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
7070
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
7171
- options has an unknown property 'unknown'. These properties are valid:
72-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
72+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
7373
`;
7474
7575
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
7676
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
7777
- options has an unknown property 'unknown'. These properties are valid:
78-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
78+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
7979
`;
8080
8181
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
8282
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
8383
- options has an unknown property 'unknown'. These properties are valid:
84-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
84+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
8585
`;
8686
8787
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
8888
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
8989
- options has an unknown property 'unknown'. These properties are valid:
90-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
90+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
9191
`;
9292
9393
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
9494
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
9595
- options has an unknown property 'unknown'. These properties are valid:
96-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
96+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
9797
`;
9898
9999
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
100100
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
101101
- options has an unknown property 'unknown'. These properties are valid:
102-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
102+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
103103
`;
104104
105105
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
106106
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
107107
- options has an unknown property 'unknown'. These properties are valid:
108-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
108+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
109109
`;
110110
111111
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
112112
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
113113
- options has an unknown property 'unknown'. These properties are valid:
114-
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter? }"
114+
object { implementation?, sassOptions?, additionalData?, sourceMap?, webpackImporter?, warnRuleAsWarning? }"
115+
`;
116+
117+
exports[`validate options should throw an error on the "warnRuleAsWarning" option with "string" value 1`] = `
118+
"Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
119+
- options.warnRuleAsWarning should be a boolean.
120+
-> Treats the '@warn' rule as a webpack warning.
121+
-> Read more at https://github.com/webpack-contrib/sass-loader#warnruleaswarning"
115122
`;
116123
117124
exports[`validate options should throw an error on the "webpackImporter" option with "string" value 1`] = `

0 commit comments

Comments
 (0)