From a6be94a6da291a27026415d509249e0203e977ad Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 16 Mar 2020 15:45:10 +0000 Subject: [PATCH] feat: allow a function to be used for `lessOptions` (#325) --- README.md | 2 +- src/getLessOptions.js | 10 +++++++++- src/options.json | 15 +++++++++++---- test/index.test.js | 10 ++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d470fda0..b76cd46b 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/getLessOptions.js b/src/getLessOptions.js index 46c63ff5..86b91832 100644 --- a/src/getLessOptions.js +++ b/src/getLessOptions.js @@ -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') { diff --git a/src/options.json b/src/options.json index 135e10d9..4964699b 100644 --- a/src/options.json +++ b/src/options.json @@ -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" } }, diff --git a/test/index.test.js b/test/index.test.js index 6502a7a1..7916d85a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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 = [];