Skip to content

Commit 12dca5b

Browse files
feat: add webpackImporter option (#377)
1 parent 919c3b9 commit 12dca5b

File tree

9 files changed

+212
-35
lines changed

9 files changed

+212
-35
lines changed

README.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ And run `webpack` via your preferred method.
4545

4646
## Options
4747

48-
| Name | Type | Default | Description |
49-
| :-------------------------------------: | :------------------: | :----------------------: | :----------------------------------------------------- |
50-
| **[`lessOptions`](#lessoptions)** | `{Object\|Function}` | `{ relativeUrls: true }` | Options for Less. |
51-
| **[`additionalData`](#additionalData)** | `{String\|Function}` | `undefined` | Prepends/Appends `Less` code to the actual entry file. |
52-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
48+
| Name | Type | Default | Description |
49+
| :---------------------------------------: | :------------------: | :----------------------: | :----------------------------------------------------- |
50+
| **[`lessOptions`](#lessoptions)** | `{Object\|Function}` | `{ relativeUrls: true }` | Options for Less. |
51+
| **[`additionalData`](#additionalData)** | `{String\|Function}` | `undefined` | Prepends/Appends `Less` code to the actual entry file. |
52+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
53+
| **[`webpackImporter`](#webpackimporter)** | `{Boolean}` | `true` | Enables/Disables the default Webpack importer. |
5354

5455
### `lessOptions`
5556

@@ -296,6 +297,39 @@ module.exports = {
296297
};
297298
```
298299

300+
### `webpackImporter`
301+
302+
Type: `Boolean`
303+
Default: `true`
304+
305+
Enables/Disables the default Webpack importer.
306+
307+
This can improve performance in some cases. Use it with caution because aliases and `@import` at-rules starting with `~` will not work.
308+
309+
**webpack.config.js**
310+
311+
```js
312+
module.exports = {
313+
module: {
314+
rules: [
315+
{
316+
test: /\.less$/i,
317+
use: [
318+
'style-loader',
319+
'css-loader',
320+
{
321+
loader: 'less-loader',
322+
options: {
323+
webpackImporter: false,
324+
},
325+
},
326+
],
327+
},
328+
],
329+
},
330+
};
331+
```
332+
299333
## Examples
300334

301335
### Normal usage

src/options.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"sourceMap": {
2828
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/less-loader#sourcemap).",
2929
"type": "boolean"
30+
},
31+
"webpackImporter": {
32+
"description": "Enables/Disables default `webpack` importer (https://github.com/webpack-contrib/less-loader#webpackimporter).",
33+
"type": "boolean"
3034
}
3135
},
3236
"additionalProperties": false

src/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ function getLessOptions(loaderContext, loaderOptions) {
157157
...options,
158158
};
159159

160-
lessOptions.plugins.unshift(createWebpackLessPlugin(loaderContext));
160+
const shouldUseWebpackImporter =
161+
typeof loaderOptions.webpackImporter === 'boolean'
162+
? loaderOptions.webpackImporter
163+
: true;
164+
165+
if (shouldUseWebpackImporter) {
166+
lessOptions.plugins.unshift(createWebpackLessPlugin(loaderContext));
167+
}
161168

162169
const useSourceMap =
163170
typeof loaderOptions.sourceMap === 'boolean'

test/__snapshots__/loader.test.js.snap

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -379,26 +379,10 @@ exports[`loader should resolve all imports from node_modules using webpack's res
379379
"
380380
`;
381381
382-
exports[`loader should resolve all imports from node_modules using webpack's resolver: css 2`] = `
383-
"@import \\"~@scope/css.css\\";
384-
.modules-dir-scope-module,
385-
#it-works {
386-
color: hotpink;
387-
}
388-
#it-works {
389-
margin: 10px;
390-
}
391-
"
392-
`;
393-
394382
exports[`loader should resolve all imports from node_modules using webpack's resolver: errors 1`] = `Array []`;
395383
396-
exports[`loader should resolve all imports from node_modules using webpack's resolver: errors 2`] = `Array []`;
397-
398384
exports[`loader should resolve all imports from node_modules using webpack's resolver: warnings 1`] = `Array []`;
399385
400-
exports[`loader should resolve all imports from node_modules using webpack's resolver: warnings 2`] = `Array []`;
401-
402386
exports[`loader should resolve all imports from the given paths using Less resolver: css 1`] = `
403387
".modules-dir-some-module {
404388
color: hotpink;

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,57 @@ exports[`validate options should throw an error on the "sourceMap" option with "
120120
- options.sourceMap should be a boolean.
121121
-> Enables/Disables generation of source maps (https://github.com/webpack-contrib/less-loader#sourcemap)."
122122
`;
123+
124+
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
125+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
126+
- options has an unknown property 'unknown'. These properties are valid:
127+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
128+
`;
129+
130+
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
131+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
132+
- options has an unknown property 'unknown'. These properties are valid:
133+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
134+
`;
135+
136+
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
137+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
138+
- options has an unknown property 'unknown'. These properties are valid:
139+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
140+
`;
141+
142+
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
143+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
144+
- options has an unknown property 'unknown'. These properties are valid:
145+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
146+
`;
147+
148+
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
149+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
150+
- options has an unknown property 'unknown'. These properties are valid:
151+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
152+
`;
153+
154+
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
155+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
156+
- options has an unknown property 'unknown'. These properties are valid:
157+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
158+
`;
159+
160+
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
161+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
162+
- options has an unknown property 'unknown'. These properties are valid:
163+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
164+
`;
165+
166+
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
167+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
168+
- options has an unknown property 'unknown'. These properties are valid:
169+
object { lessOptions?, additionalData?, sourceMap?, webpackImporter? }"
170+
`;
171+
172+
exports[`validate options should throw an error on the "webpackImporter" option with "string" value 1`] = `
173+
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
174+
- options.webpackImporter should be a boolean.
175+
-> Enables/Disables default \`webpack\` importer (https://github.com/webpack-contrib/less-loader#webpackimporter)."
176+
`;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`webpackImporter option not specify webpackImporter option: css 1`] = `
4+
"@import \\"~some/css.css\\";
5+
@import \\"~some/css.css\\";
6+
#it-works {
7+
color: hotpink;
8+
}
9+
.modules-dir-some-module,
10+
#it-works {
11+
background: hotpink;
12+
}
13+
#it-works {
14+
margin: 10px;
15+
}
16+
"
17+
`;
18+
19+
exports[`webpackImporter option not specify webpackImporter option: errors 1`] = `Array []`;
20+
21+
exports[`webpackImporter option not specify webpackImporter option: warnings 1`] = `Array []`;
22+
23+
exports[`webpackImporter option webpackImporter option is false: errors 1`] = `
24+
Array [
25+
"ModuleBuildError: Module build failed (from \`replaced original path\`):
26+
",
27+
]
28+
`;
29+
30+
exports[`webpackImporter option webpackImporter option is false: warnings 1`] = `Array []`;
31+
32+
exports[`webpackImporter option webpackImporter option is true: css 1`] = `
33+
"@import \\"~some/css.css\\";
34+
@import \\"~some/css.css\\";
35+
#it-works {
36+
color: hotpink;
37+
}
38+
.modules-dir-some-module,
39+
#it-works {
40+
background: hotpink;
41+
}
42+
#it-works {
43+
margin: 10px;
44+
}
45+
"
46+
`;
47+
48+
exports[`webpackImporter option webpackImporter option is true: errors 1`] = `Array []`;
49+
50+
exports[`webpackImporter option webpackImporter option is true: warnings 1`] = `Array []`;

test/loader.test.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,6 @@ describe('loader', () => {
176176
expect(getErrors(stats)).toMatchSnapshot('errors');
177177
});
178178

179-
it("should resolve all imports from node_modules using webpack's resolver", async () => {
180-
const testId = './import-scope.less';
181-
const compiler = getCompiler(testId);
182-
const stats = await compile(compiler);
183-
const codeFromBundle = getCodeFromBundle(stats, compiler);
184-
const codeFromLess = await getCodeFromLess(testId);
185-
186-
expect(codeFromBundle.css).toBe(codeFromLess.css);
187-
expect(codeFromBundle.css).toMatchSnapshot('css');
188-
expect(getWarnings(stats)).toMatchSnapshot('warnings');
189-
expect(getErrors(stats)).toMatchSnapshot('errors');
190-
});
191-
192179
it('should resolve aliases in diffrent variants', async () => {
193180
const testId = './import-webpack-aliases.less';
194181
const compiler = getCompiler(

test/validate-options.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ describe('validate options', () => {
1919
success: [true, false],
2020
failure: ['string'],
2121
},
22+
webpackImporter: {
23+
success: [true, false],
24+
failure: ['string'],
25+
},
26+
unknown: {
27+
success: [],
28+
failure: [1, true, false, 'test', /test/, [], {}, { foo: 'bar' }],
29+
},
2230
};
2331

2432
function stringifyValue(value) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
compile,
3+
getCodeFromBundle,
4+
getCodeFromLess,
5+
getCompiler,
6+
getErrors,
7+
getWarnings,
8+
} from './helpers';
9+
10+
describe('webpackImporter option', () => {
11+
it('not specify webpackImporter option', async () => {
12+
const testId = './import-webpack.less';
13+
const compiler = getCompiler(testId);
14+
const stats = await compile(compiler);
15+
const codeFromBundle = getCodeFromBundle(stats, compiler);
16+
const codeFromLess = await getCodeFromLess(testId);
17+
18+
expect(codeFromBundle.css).toBe(codeFromLess.css);
19+
expect(codeFromBundle.css).toMatchSnapshot('css');
20+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
21+
expect(getErrors(stats)).toMatchSnapshot('errors');
22+
});
23+
24+
it('webpackImporter option is true', async () => {
25+
const testId = './import-webpack.less';
26+
const compiler = getCompiler(testId, {
27+
webpackImporter: true,
28+
});
29+
const stats = await compile(compiler);
30+
const codeFromBundle = getCodeFromBundle(stats, compiler);
31+
const codeFromLess = await getCodeFromLess(testId);
32+
33+
expect(codeFromBundle.css).toBe(codeFromLess.css);
34+
expect(codeFromBundle.css).toMatchSnapshot('css');
35+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
36+
expect(getErrors(stats)).toMatchSnapshot('errors');
37+
});
38+
39+
it('webpackImporter option is false', async () => {
40+
const testId = './import-webpack.less';
41+
const compiler = getCompiler(testId, {
42+
webpackImporter: false,
43+
});
44+
const stats = await compile(compiler);
45+
46+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
47+
expect(getErrors(stats)).toMatchSnapshot('errors');
48+
});
49+
});

0 commit comments

Comments
 (0)