Skip to content

Commit 9a2edd7

Browse files
paniuncleliushuoyan
authored and
liushuoyan
committed
feat: use the webpackImporter var and add unit test
1 parent da0be65 commit 9a2edd7

7 files changed

+58
-46
lines changed

README.md

+1-40
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`
@@ -346,45 +346,6 @@ module.exports = {
346346
};
347347
```
348348

349-
### `onlyWebpackImporter`
350-
351-
Type:
352-
353-
```ts
354-
type onlyWebpackImporter = boolean;
355-
```
356-
357-
Default: `true`
358-
359-
Enables/Disables the only use `webpack` importer.
360-
361-
This can improve performance in some cases. Use it with caution because aliases and `@import` from [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules) will not work.
362-
363-
**webpack.config.js**
364-
365-
```js
366-
module.exports = {
367-
module: {
368-
rules: [
369-
{
370-
test: /\.less$/i,
371-
use: [
372-
"style-loader",
373-
"css-loader",
374-
{
375-
loader: "less-loader",
376-
options: {
377-
webpackImporter: true,
378-
onlyWebpackImporter: true,
379-
},
380-
},
381-
],
382-
},
383-
],
384-
},
385-
};
386-
```
387-
388349
### `implementation`
389350

390351
Type:

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ function createWebpackLessPlugin(loaderContext, implementation) {
106106
let result;
107107

108108
try {
109-
if (IS_SPECIAL_MODULE_IMPORT.test(filename) || lessOptions.onlyWebpackImporter) {
109+
if (
110+
IS_SPECIAL_MODULE_IMPORT.test(filename) ||
111+
lessOptions.webpackImporter === "only"
112+
) {
110113
const error = new Error();
111114

112115
error.type = "Next";
@@ -178,7 +181,8 @@ function getLessOptions(loaderContext, loaderOptions, implementation) {
178181

179182
const plugins = lessOptions.plugins.slice();
180183
const shouldUseWebpackImporter =
181-
typeof loaderOptions.webpackImporter === "boolean"
184+
typeof loaderOptions.webpackImporter === "boolean" ||
185+
loaderOptions.webpackImporter === "only"
182186
? loaderOptions.webpackImporter
183187
: true;
184188

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)