Skip to content

Commit a6be94a

Browse files
author
Ryan Clark
authored
feat: allow a function to be used for lessOptions (#325)
1 parent cdb611f commit a6be94a

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The `less-loader` requires [less](https://github.com/less/less.js) as [`peerDepe
4949

5050
### `lessOptions`
5151

52-
Type: `Object`
52+
Type: `Object|Function`
5353

5454
You can pass any Less specific options to the `less-loader` through the `lessOptions` property in the [loader options](https://webpack.js.org/configuration/module/#rule-options-rule-query). See the [Less documentation](http://lesscss.org/usage/#command-line-usage-options) for all available options in dash-case. Since we're passing these options to Less programmatically, you need to pass them in camelCase here:
5555

src/getLessOptions.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ import createWebpackLessPlugin from './createWebpackLessPlugin';
1010
* @returns {Object}
1111
*/
1212
function getLessOptions(loaderContext, loaderOptions) {
13+
const options = clone(
14+
loaderOptions.lessOptions
15+
? typeof loaderOptions.lessOptions === 'function'
16+
? loaderOptions.lessOptions(loaderContext) || {}
17+
: loaderOptions.lessOptions
18+
: {}
19+
);
20+
1321
const lessOptions = {
1422
plugins: [],
1523
relativeUrls: true,
1624
// We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
1725
filename: loaderContext.resourcePath,
18-
...(loaderOptions.lessOptions ? clone(loaderOptions.lessOptions) : {}),
26+
...options,
1927
};
2028

2129
if (typeof lessOptions.paths === 'undefined') {

src/options.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
"type": "object",
33
"properties": {
44
"lessOptions": {
5-
"description": "Options to pass through to `less`. (https://github.com/webpack-contrib/less-loader#examples).",
6-
"type": "object",
7-
"additionalProperties": true
5+
"description": "Options to pass through to `less`. (https://github.com/webpack-contrib/less-loader#lessoptions).",
6+
"anyOf": [
7+
{
8+
"type": "object",
9+
"additionalProperties": true
10+
},
11+
{
12+
"instanceof": "Function"
13+
}
14+
]
815
},
916
"sourceMap": {
10-
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/less-loader#source-maps).",
17+
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/less-loader#sourcemap).",
1118
"type": "boolean"
1219
}
1320
},

test/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ test("should resolve all imports from the given paths using Less' resolver", asy
128128
});
129129
});
130130

131+
test('should allow a function to be passed through for `lessOptions`', async () => {
132+
await compileAndCompare('import-paths', {
133+
lessLoaderOptions: {
134+
lessOptions: () => ({
135+
paths: [__dirname, nodeModulesPath],
136+
}),
137+
},
138+
});
139+
});
140+
131141
test('should add all resolved imports as dependencies, including those from the Less resolver', async () => {
132142
const dependencies = [];
133143

0 commit comments

Comments
 (0)