Skip to content

Commit 3c4e6e0

Browse files
feat: added the lessLogAsWarnOrErr option (#536)
1 parent dd398aa commit 3c4e6e0

9 files changed

+117
-10
lines changed

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ And run `webpack` via your preferred method.
6666
- **[`sourceMap`](#sourcemap)**
6767
- **[`webpackImporter`](#webpackimporter)**
6868
- **[`implementation`](#implementation)**
69+
- **[`lessLogAsWarnOrErr`](#lesslogaswarnorerr)**
6970

7071
### `lessOptions`
7172

@@ -411,6 +412,52 @@ module.exports = {
411412
};
412413
```
413414

415+
### `lessLogAsWarnOrErr`
416+
417+
Type:
418+
419+
```ts
420+
type lessLogAsWarnOrErr = boolean;
421+
```
422+
423+
Default: `false`
424+
425+
`Less` warnings and errors will be webpack warnings and errors, not just logs.
426+
427+
**warning.less**
428+
429+
```less
430+
div {
431+
&:extend(.body1);
432+
}
433+
```
434+
435+
If `lessLogAsWarnOrErr` is set to `false` it will be just a log and webpack will compile successfully, but if you set this option to `true` webpack will compile fail with a warning.
436+
437+
**webpack.config.js**
438+
439+
```js
440+
module.exports = {
441+
module: {
442+
rules: [
443+
{
444+
test: /\.less$/i,
445+
use: [
446+
"style-loader",
447+
"css-loader",
448+
{
449+
loader: "less-loader",
450+
options: {
451+
lessLogAsWarnOrErr: true,
452+
},
453+
},
454+
],
455+
},
456+
],
457+
},
458+
};
459+
```
460+
414461
## Examples
415462

416463
### Normal usage

src/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,23 @@ async function lessLoader(source) {
5252
}
5353

5454
const logger = this.getLogger("less-loader");
55+
const loaderContext = this;
5556
const loggerListener = {
5657
error(message) {
57-
logger.error(message);
58+
// TODO enable by default in the next major release
59+
if (options.lessLogAsWarnOrErr) {
60+
loaderContext.emitError(new Error(message));
61+
} else {
62+
logger.error(message);
63+
}
5864
},
5965
warn(message) {
60-
logger.warn(message);
66+
// TODO enable by default in the next major release
67+
if (options.lessLogAsWarnOrErr) {
68+
loaderContext.emitWarning(new Error(message));
69+
} else {
70+
logger.warn(message);
71+
}
6172
},
6273
info(message) {
6374
logger.log(message);

src/options.json

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
"type": "object"
4949
}
5050
]
51+
},
52+
"lessLogAsWarnOrErr": {
53+
"description": "Less warnings and errors will be webpack warnings or errors.",
54+
"link": "https://github.com/webpack-contrib/less-loader#lesslogaswarnorerr",
55+
"type": "boolean"
5156
}
5257
},
5358
"additionalProperties": false

test/__snapshots__/loader.test.js.snap

+11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ exports[`loader should emit an error: errors 1`] = `
8484

8585
exports[`loader should emit an error: warnings 1`] = `[]`;
8686

87+
exports[`loader should emit less warning as webpack warning: css 1`] = `""`;
88+
89+
exports[`loader should emit less warning as webpack warning: errors 1`] = `[]`;
90+
91+
exports[`loader should emit less warning as webpack warning: warnings 1`] = `
92+
[
93+
"ModuleWarning: Module Warning (from \`replaced original path\`):
94+
extend ' .body1' has no matches",
95+
]
96+
`;
97+
8798
exports[`loader should get absolute path relative rootContext: css 1`] = `
8899
".box {
89100
color: #fe33ac;

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ exports[`validate options should throw an error on the "implementation" option w
114114
object {}"
115115
`;
116116

117+
exports[`validate options should throw an error on the "lessLogAsWarnOrErr" option with "string" value 1`] = `
118+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
119+
- options.lessLogAsWarnOrErr should be a boolean.
120+
-> Less warnings and errors will be webpack warnings or errors.
121+
-> Read more at https://github.com/webpack-contrib/less-loader#lesslogaswarnorerr"
122+
`;
123+
117124
exports[`validate options should throw an error on the "lessOptions" option with "[]" value 1`] = `
118125
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
119126
- options.lessOptions should be one of these:
@@ -184,49 +191,49 @@ exports[`validate options should throw an error on the "sourceMap" option with "
184191
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
185192
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
186193
- options has an unknown property 'unknown'. These properties are valid:
187-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
194+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
188195
`;
189196
190197
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
191198
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
192199
- options has an unknown property 'unknown'. These properties are valid:
193-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
200+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
194201
`;
195202
196203
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
197204
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
198205
- options has an unknown property 'unknown'. These properties are valid:
199-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
206+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
200207
`;
201208
202209
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
203210
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
204211
- options has an unknown property 'unknown'. These properties are valid:
205-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
212+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
206213
`;
207214
208215
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
209216
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
210217
- options has an unknown property 'unknown'. These properties are valid:
211-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
218+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
212219
`;
213220
214221
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
215222
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
216223
- options has an unknown property 'unknown'. These properties are valid:
217-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
224+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
218225
`;
219226
220227
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
221228
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
222229
- options has an unknown property 'unknown'. These properties are valid:
223-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
230+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
224231
`;
225232
226233
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
227234
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
228235
- options has an unknown property 'unknown'. These properties are valid:
229-
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
236+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
230237
`;
231238
232239
exports[`validate options should throw an error on the "webpackImporter" option with "string" value 1`] = `

test/fixtures/err.less

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
div {
2+
.m(@x) when (default()) {}
3+
.m(@x) when not(default()) {}
4+
5+
.m(1); // Error
6+
}

test/fixtures/warn.less

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
&:extend(.body1);
3+
}

test/loader.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -1047,4 +1047,17 @@ describe("loader", () => {
10471047
expect(getWarnings(stats)).toMatchSnapshot("warnings");
10481048
expect(getErrors(stats)).toMatchSnapshot("errors");
10491049
});
1050+
1051+
it("should emit less warning as webpack warning", async () => {
1052+
const testId = "./warn.less";
1053+
const compiler = getCompiler(testId, {
1054+
lessLogAsWarnOrErr: true,
1055+
});
1056+
const stats = await compile(compiler);
1057+
const codeFromBundle = getCodeFromBundle(stats, compiler);
1058+
1059+
expect(codeFromBundle.css).toMatchSnapshot("css");
1060+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1061+
expect(getErrors(stats)).toMatchSnapshot("errors");
1062+
});
10501063
});

test/validate-options.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ describe("validate options", () => {
3030
success: [require("less"), "less"],
3131
failure: [true, false, () => {}, []],
3232
},
33+
lessLogAsWarnOrErr: {
34+
success: [true, false],
35+
failure: ["string"],
36+
},
3337
unknown: {
3438
success: [],
3539
failure: [1, true, false, "test", /test/, [], {}, { foo: "bar" }],

0 commit comments

Comments
 (0)