Skip to content

Commit 12839c8

Browse files
authored
feat: add support for using only webpackImporter
1 parent 200d836 commit 12839c8

7 files changed

+59
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ module.exports = {
313313
Type:
314314

315315
```ts
316-
type webpackImporter = boolean;
316+
type webpackImporter = boolean | "only";
317317
```
318318

319319
Default: `true`

src/options.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@
3535
"webpackImporter": {
3636
"description": "Enables/Disables default `webpack` importer.",
3737
"link": "https://github.com/webpack-contrib/less-loader#webpackimporter",
38-
"type": "boolean"
38+
"anyOf": [
39+
{
40+
"type": "boolean"
41+
},
42+
{
43+
"type": "string",
44+
"enum": ["only"]
45+
}
46+
]
3947
},
4048
"implementation": {
4149
"description": "The implementation of the `Less` to be used.",

src/utils.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const MODULE_REQUEST_REGEX = /^[^?]*~/;
3030
* @returns {LessPlugin}
3131
*/
3232
function createWebpackLessPlugin(loaderContext, implementation) {
33+
const lessOptions = loaderContext.getOptions();
3334
const resolve = loaderContext.getResolve({
3435
dependencyType: "less",
3536
conditionNames: ["less", "style", "..."],
@@ -105,7 +106,10 @@ function createWebpackLessPlugin(loaderContext, implementation) {
105106
let result;
106107

107108
try {
108-
if (IS_SPECIAL_MODULE_IMPORT.test(filename)) {
109+
if (
110+
IS_SPECIAL_MODULE_IMPORT.test(filename) ||
111+
lessOptions.webpackImporter === "only"
112+
) {
109113
const error = new Error();
110114

111115
error.type = "Next";
@@ -177,7 +181,8 @@ function getLessOptions(loaderContext, loaderOptions, implementation) {
177181

178182
const plugins = lessOptions.plugins.slice();
179183
const shouldUseWebpackImporter =
180-
typeof loaderOptions.webpackImporter === "boolean"
184+
typeof loaderOptions.webpackImporter === "boolean" ||
185+
loaderOptions.webpackImporter === "only"
181186
? loaderOptions.webpackImporter
182187
: true;
183188

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ exports[`validate options should throw an error on the "unknown" option with "tr
238238
239239
exports[`validate options should throw an error on the "webpackImporter" option with "string" value 1`] = `
240240
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
241-
- options.webpackImporter should be a boolean.
241+
- options.webpackImporter should be one of these:
242+
boolean | "only"
242243
-> Enables/Disables default \`webpack\` importer.
243-
-> Read more at https://github.com/webpack-contrib/less-loader#webpackimporter"
244+
-> Read more at https://github.com/webpack-contrib/less-loader#webpackimporter
245+
Details:
246+
* options.webpackImporter should be a boolean.
247+
* options.webpackImporter should be "only"."
244248
`;

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

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ exports[`"webpackImporter" option should work when value is "false": errors 1`]
3636

3737
exports[`"webpackImporter" option should work when value is "false": warnings 1`] = `[]`;
3838

39+
exports[`"webpackImporter" option should work when value is "only": css 1`] = `
40+
"@import "some/css.css";
41+
@import "some/css.css";
42+
#it-works {
43+
color: hotpink;
44+
}
45+
.modules-dir-some-module,
46+
#it-works {
47+
background: hotpink;
48+
}
49+
#it-works {
50+
margin: 10px;
51+
}
52+
"
53+
`;
54+
55+
exports[`"webpackImporter" option should work when value is "only": errors 1`] = `[]`;
56+
57+
exports[`"webpackImporter" option should work when value is "only": warnings 1`] = `[]`;
58+
3959
exports[`"webpackImporter" option should work when value is "true": css 1`] = `
4060
"@import "some/css.css";
4161
@import "some/css.css";

test/validate-options.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("validate options", () => {
2222
failure: ["string"],
2323
},
2424
webpackImporter: {
25-
success: [true, false],
25+
success: [true, false, "only"],
2626
failure: ["string"],
2727
},
2828
implementation: {

test/webpackImporter-options.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ describe('"webpackImporter" option', () => {
3636
expect(getErrors(stats)).toMatchSnapshot("errors");
3737
});
3838

39+
it('should work when value is "only"', async () => {
40+
const testId = "./import-webpack.less";
41+
const compiler = getCompiler(testId, {
42+
webpackImporter: "only",
43+
});
44+
const stats = await compile(compiler);
45+
const codeFromBundle = getCodeFromBundle(stats, compiler);
46+
const codeFromLess = await getCodeFromLess(testId);
47+
48+
expect(codeFromBundle.css).toBe(codeFromLess.css);
49+
expect(codeFromBundle.css).toMatchSnapshot("css");
50+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
51+
expect(getErrors(stats)).toMatchSnapshot("errors");
52+
});
53+
3954
it('should work when value is "false"', async () => {
4055
const testId = "./import.less";
4156
const compiler = getCompiler(testId, {

0 commit comments

Comments
 (0)