Skip to content
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

feat: added the lessLogAsWarnOrErr option #536

Merged
merged 2 commits into from
Jan 19, 2024
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
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ And run `webpack` via your preferred method.
- **[`sourceMap`](#sourcemap)**
- **[`webpackImporter`](#webpackimporter)**
- **[`implementation`](#implementation)**
- **[`lessLogAsWarnOrErr`](#lesslogaswarnorerr)**

### `lessOptions`

Expand Down Expand Up @@ -411,6 +412,52 @@ module.exports = {
};
```

### `lessLogAsWarnOrErr`

Type:

```ts
type lessLogAsWarnOrErr = boolean;
```

Default: `false`

`Less` warnings and errors will be webpack warnings and errors, not just logs.

**warning.less**

```less
div {
&:extend(.body1);
}
```

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.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.less$/i,
use: [
"style-loader",
"css-loader",
{
loader: "less-loader",
options: {
lessLogAsWarnOrErr: true,
},
},
],
},
],
},
};
```

## Examples

### Normal usage
Expand Down
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@
}

const logger = this.getLogger("less-loader");
const loaderContext = this;
const loggerListener = {
error(message) {
logger.error(message);
// TODO enable by default in the next major release
if (options.lessLogAsWarnOrErr) {
loaderContext.emitError(new Error(message));
} else {
logger.error(message);

Check warning on line 62 in src/index.js

View check run for this annotation

Codecov / codecov/patch

src/index.js#L60-L62

Added lines #L60 - L62 were not covered by tests
}
},
warn(message) {
logger.warn(message);
// TODO enable by default in the next major release
if (options.lessLogAsWarnOrErr) {
loaderContext.emitWarning(new Error(message));
} else {
logger.warn(message);
}
},
info(message) {
logger.log(message);
Expand Down
5 changes: 5 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
"type": "object"
}
]
},
"lessLogAsWarnOrErr": {
"description": "Less warnings and errors will be webpack warnings or errors.",
"link": "https://github.com/webpack-contrib/less-loader#lesslogaswarnorerr",
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
11 changes: 11 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ exports[`loader should emit an error: errors 1`] = `

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

exports[`loader should emit less warning as webpack warning: css 1`] = `""`;

exports[`loader should emit less warning as webpack warning: errors 1`] = `[]`;

exports[`loader should emit less warning as webpack warning: warnings 1`] = `
[
"ModuleWarning: Module Warning (from \`replaced original path\`):
extend ' .body1' has no matches",
]
`;

exports[`loader should get absolute path relative rootContext: css 1`] = `
".box {
color: #fe33ac;
Expand Down
23 changes: 15 additions & 8 deletions test/__snapshots__/validate-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ exports[`validate options should throw an error on the "implementation" option w
object { … }"
`;

exports[`validate options should throw an error on the "lessLogAsWarnOrErr" option with "string" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.lessLogAsWarnOrErr should be a boolean.
-> Less warnings and errors will be webpack warnings or errors.
-> Read more at https://github.com/webpack-contrib/less-loader#lesslogaswarnorerr"
`;

exports[`validate options should throw an error on the "lessOptions" option with "[]" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.lessOptions should be one of these:
Expand Down Expand Up @@ -184,49 +191,49 @@ exports[`validate options should throw an error on the "sourceMap" option with "
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation? }"
object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }"
`;

exports[`validate options should throw an error on the "webpackImporter" option with "string" value 1`] = `
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/err.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
div {
.m(@x) when (default()) {}
.m(@x) when not(default()) {}

.m(1); // Error
}
3 changes: 3 additions & 0 deletions test/fixtures/warn.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div {
&:extend(.body1);
}
13 changes: 13 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,4 +1047,17 @@ describe("loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should emit less warning as webpack warning", async () => {
const testId = "./warn.less";
const compiler = getCompiler(testId, {
lessLogAsWarnOrErr: true,
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});
4 changes: 4 additions & 0 deletions test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe("validate options", () => {
success: [require("less"), "less"],
failure: [true, false, () => {}, []],
},
lessLogAsWarnOrErr: {
success: [true, false],
failure: ["string"],
},
unknown: {
success: [],
failure: [1, true, false, "test", /test/, [], {}, { foo: "bar" }],
Expand Down