From ce03da9abde88af9b6a4e6312f52f757a65c3ab4 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Mon, 16 Mar 2020 15:57:16 +0000 Subject: [PATCH] docs: add example for `lessOptions` using a function (#326) --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index b76cd46b..0c575242 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,10 @@ 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: +#### `Object` + +Use an object to pass options through to Less. + **webpack.config.js** ```js @@ -84,6 +88,46 @@ module.exports = { }; ``` +#### `Function` + +Allows setting the options passed through to Less based off of the loader context. + +```js +module.exports = { + module: { + rules: [ + { + test: /\.less$/, + use: [ + 'style-loader', + 'css-loader', + { + loader: 'less-loader', + options: { + lessOptions: (loaderContext) => { + // More information about available properties https://webpack.js.org/api/loaders/ + const { resourcePath, rootContext } = loaderContext; + const relativePath = path.relative(rootContext, resourcePath); + + if (relativePath === 'styles/foo.scss') { + return { + paths: ['absolute/path/c', 'absolute/path/d'], + }; + } + + return { + paths: ['absolute/path/a', 'absolute/path/b'], + }; + }, + }, + }, + ], + }, + ], + }, +}; +``` + ### `sourceMap` Type: `Boolean`