Skip to content

Commit

Permalink
feat: allow a function to be used for lessOptions (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Clark committed Mar 16, 2020
1 parent cdb611f commit a6be94a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -49,7 +49,7 @@ The `less-loader` requires [less](https://github.com/less/less.js) as [`peerDepe

### `lessOptions`

Type: `Object`
Type: `Object|Function`

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:

Expand Down
10 changes: 9 additions & 1 deletion src/getLessOptions.js
Expand Up @@ -10,12 +10,20 @@ import createWebpackLessPlugin from './createWebpackLessPlugin';
* @returns {Object}
*/
function getLessOptions(loaderContext, loaderOptions) {
const options = clone(
loaderOptions.lessOptions
? typeof loaderOptions.lessOptions === 'function'
? loaderOptions.lessOptions(loaderContext) || {}
: loaderOptions.lessOptions
: {}
);

const lessOptions = {
plugins: [],
relativeUrls: true,
// We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
filename: loaderContext.resourcePath,
...(loaderOptions.lessOptions ? clone(loaderOptions.lessOptions) : {}),
...options,
};

if (typeof lessOptions.paths === 'undefined') {
Expand Down
15 changes: 11 additions & 4 deletions src/options.json
Expand Up @@ -2,12 +2,19 @@
"type": "object",
"properties": {
"lessOptions": {
"description": "Options to pass through to `less`. (https://github.com/webpack-contrib/less-loader#examples).",
"type": "object",
"additionalProperties": true
"description": "Options to pass through to `less`. (https://github.com/webpack-contrib/less-loader#lessoptions).",
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"instanceof": "Function"
}
]
},
"sourceMap": {
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/less-loader#source-maps).",
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/less-loader#sourcemap).",
"type": "boolean"
}
},
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Expand Up @@ -128,6 +128,16 @@ test("should resolve all imports from the given paths using Less' resolver", asy
});
});

test('should allow a function to be passed through for `lessOptions`', async () => {
await compileAndCompare('import-paths', {
lessLoaderOptions: {
lessOptions: () => ({
paths: [__dirname, nodeModulesPath],
}),
},
});
});

test('should add all resolved imports as dependencies, including those from the Less resolver', async () => {
const dependencies = [];

Expand Down

0 comments on commit a6be94a

Please sign in to comment.