Installations
npm install less-loader
Developer
webpack-contrib
Developer Guide
Module System
CommonJS
Min. Node Version
>= 18.12.0
Typescript Support
No
Node Version
18.19.0
NPM Version
10.2.3
Statistics
959 Stars
455 Commits
194 Forks
20 Watching
1 Branches
61 Contributors
Updated on 08 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
932,655,694
Last day
-3.7%
943,676
Compared to previous day
Last week
2.5%
4,912,724
Compared to previous week
Last month
9.3%
20,557,684
Compared to previous month
Last year
8.8%
222,297,553
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
Dev Dependencies
25
less-loader
A Less loader for webpack. Compiles Less to CSS.
Getting Started
To begin, you'll need to install less
and less-loader
:
1npm install less less-loader --save-dev
or
1yarn add -D less less-loader
or
1pnpm add -D less less-loader
Then add the loader to your webpack
config. For example:
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 // compiles Less to CSS 8 "style-loader", 9 "css-loader", 10 "less-loader", 11 ], 12 }, 13 ], 14 }, 15};
And run webpack
via your preferred method.
Options
lessOptions
Type:
1type lessOptions = import('less').options | ((loaderContext: LoaderContext) => import('less').options})
Default: { relativeUrls: true }
You can pass any Less specific options to the less-loader
through the lessOptions
property in the loader options. See the Less documentation 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
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 { 8 loader: "style-loader", 9 }, 10 { 11 loader: "css-loader", 12 }, 13 { 14 loader: "less-loader", 15 options: { 16 lessOptions: { 17 strictMath: true, 18 }, 19 }, 20 }, 21 ], 22 }, 23 ], 24 }, 25};
function
Allows setting the options passed through to Less based off of the loader context.
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 lessOptions: (loaderContext) => { 13 // More information about available properties https://webpack.js.org/api/loaders/ 14 const { resourcePath, rootContext } = loaderContext; 15 const relativePath = path.relative(rootContext, resourcePath); 16 17 if (relativePath === "styles/foo.less") { 18 return { 19 paths: ["absolute/path/c", "absolute/path/d"], 20 }; 21 } 22 23 return { 24 paths: ["absolute/path/a", "absolute/path/b"], 25 }; 26 }, 27 }, 28 }, 29 ], 30 }, 31 ], 32 }, 33};
additionalData
Type:
1type additionalData = 2 | string 3 | ((content: string, loaderContext: LoaderContext) => string);
Default: undefined
Prepends/Appends Less
code to the actual entry file.
In this case, the less-loader
will not override the source but just prepend the entry's content.
This is especially useful when some of your Less variables depend on the environment:
Since you're injecting code, this will break the source mappings in your entry file. Often there's a simpler solution than this, like multiple Less entry files.
string
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 additionalData: `@env: ${process.env.NODE_ENV};`, 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
function
Sync
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 additionalData: (content, loaderContext) => { 13 // More information about available properties https://webpack.js.org/api/loaders/ 14 const { resourcePath, rootContext } = loaderContext; 15 const relativePath = path.relative(rootContext, resourcePath); 16 17 if (relativePath === "styles/foo.less") { 18 return "@value: 100px;" + content; 19 } 20 21 return "@value: 200px;" + content; 22 }, 23 }, 24 }, 25 ], 26 }, 27 ], 28 }, 29};
Async
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 additionalData: async (content, loaderContext) => { 13 // More information about available properties https://webpack.js.org/api/loaders/ 14 const { resourcePath, rootContext } = loaderContext; 15 const relativePath = path.relative(rootContext, resourcePath); 16 17 if (relativePath === "styles/foo.less") { 18 return "@value: 100px;" + content; 19 } 20 21 return "@value: 200px;" + content; 22 }, 23 }, 24 }, 25 ], 26 }, 27 ], 28 }, 29};
sourceMap
Type:
1type sourceMap = boolean;
Default: depends on the compiler.devtool
value
By default generation of source maps depends on the devtool
option. All values enable source map generation except eval
and false
value.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 { 9 loader: "css-loader", 10 options: { 11 sourceMap: true, 12 }, 13 }, 14 { 15 loader: "less-loader", 16 options: { 17 sourceMap: true, 18 }, 19 }, 20 ], 21 }, 22 ], 23 }, 24};
webpackImporter
Type:
1type webpackImporter = boolean;
Default: true
Enables/Disables the default webpack
importer.
This can improve performance in some cases. Use it with caution because aliases and @import
from node_modules
will not work.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 webpackImporter: false, 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
implementation
Type:
1type implementation = object | string;
less-loader compatible with Less 3 and 4 versions
The special implementation
option determines which implementation of Less to use. Overrides the locally installed peerDependency
version of less
.
This option is only really useful for downstream tooling authors to ease the Less 3-to-4 transition.
object
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 implementation: require("less"), 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
string
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 implementation: require.resolve("less"), 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
lessLogAsWarnOrErr
Type:
1type lessLogAsWarnOrErr = boolean;
Default: false
Less
warnings and errors will be webpack warnings and errors, not just logs.
warning.less
1div { 2 &:extend(.body1); 3}
If lessLogAsWarnOrErr
is set to false
it will be just a log and webpack will compile successfully, but if you set this option to true
webpack will compile fail with a warning.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "less-loader", 11 options: { 12 lessLogAsWarnOrErr: true, 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
Examples
Normal usage
Chain the less-loader
with the css-loader
and the style-loader
to immediately apply all styles to the DOM.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 { 8 loader: "style-loader", // creates style nodes from JS strings 9 }, 10 { 11 loader: "css-loader", // translates CSS into CommonJS 12 }, 13 { 14 loader: "less-loader", // compiles Less to CSS 15 }, 16 ], 17 }, 18 ], 19 }, 20};
Unfortunately, Less doesn't map all options 1-by-1 to camelCase. When in doubt, check their executable and search for the dash-case option.
Source maps
To enable sourcemaps for CSS, you'll need to pass the sourceMap
property in the loader's options. If this is not passed, the loader will respect the setting for webpack source maps, set in devtool
.
webpack.config.js
1module.exports = { 2 devtool: "source-map", // any "source-map"-like devtool is possible 3 module: { 4 rules: [ 5 { 6 test: /\.less$/i, 7 use: [ 8 "style-loader", 9 { 10 loader: "css-loader", 11 options: { 12 sourceMap: true, 13 }, 14 }, 15 { 16 loader: "less-loader", 17 options: { 18 sourceMap: true, 19 }, 20 }, 21 ], 22 }, 23 ], 24 }, 25};
If you want to edit the original Less files inside Chrome, there's a good blog post. The blog post is about Sass but it also works for Less.
In production
Usually, it's recommended to extract the style sheets into a dedicated file in production using the MiniCssExtractPlugin. This way your styles are not dependent on JavaScript.
Imports
First we try to use built-in less
resolve logic, then webpack
resolve logic.
Webpack Resolver
webpack
provides an advanced mechanism to resolve files.
less-loader
applies a Less plugin that passes all queries to the webpack resolver if less
could not resolve @import
.
Thus you can import your Less modules from node_modules
.
1@import "bootstrap/less/bootstrap";
Using ~
is deprecated and can be removed from your code (we recommend it), but we still support it for historical reasons.
Why you can removed it? The loader will first try to resolve @import
as relative, if it cannot be resolved, the loader will try to resolve @import
inside node_modules
.
Default resolver options can be modified by resolve.byDependency
:
webpack.config.js
1module.exports = { 2 devtool: "source-map", // any "source-map"-like devtool is possible 3 module: { 4 rules: [ 5 { 6 test: /\.less$/i, 7 use: ["style-loader", "css-loader", "less-loader"], 8 }, 9 ], 10 }, 11 resolve: { 12 byDependency: { 13 // More options can be found here https://webpack.js.org/configuration/resolve/ 14 less: { 15 mainFiles: ["custom"], 16 }, 17 }, 18 }, 19};
Less Resolver
If you specify the paths
option, modules will be searched in the given paths
. This is less
default behavior. paths
should be an array with absolute paths:
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.less$/i, 6 use: [ 7 { 8 loader: "style-loader", 9 }, 10 { 11 loader: "css-loader", 12 }, 13 { 14 loader: "less-loader", 15 options: { 16 lessOptions: { 17 paths: [path.resolve(__dirname, "node_modules")], 18 }, 19 }, 20 }, 21 ], 22 }, 23 ], 24 }, 25};
Plugins
In order to use plugins, simply set the plugins
option like this:
webpack.config.js
1const CleanCSSPlugin = require('less-plugin-clean-css'); 2 3module.exports = { 4 ... 5 { 6 loader: 'less-loader', 7 options: { 8 lessOptions: { 9 plugins: [ 10 new CleanCSSPlugin({ advanced: true }), 11 ], 12 }, 13 }, 14 }, 15 ... 16};
[!NOTE]
Access to the loader context inside the custom plugin can be done using the
pluginManager.webpackLoaderContext
property.
1module.exports = { 2 install: function (less, pluginManager, functions) { 3 functions.add("pi", function () { 4 // Loader context is available in `pluginManager.webpackLoaderContext` 5 6 return Math.PI; 7 }); 8 }, 9};
Extracting style sheets
Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or hot module replacement in development. In production, on the other hand, it's not a good idea to apply your style sheets depending on JS execution. Rendering may be delayed or even a FOUC might be visible. Thus it's often still better to have them as separate files in your final production build.
There are two possibilities to extract a style sheet from the bundle:
extract-loader
(simpler, but specialized on the css-loader's output)MiniCssExtractPlugin
(more complex, but works in all use-cases)
CSS modules gotcha
There is a known problem with Less and CSS modules regarding relative file paths in url(...)
statements. See this issue for an explanation.
Contributing
Please take a moment to read our contributing guidelines if you haven't yet done so.
License
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
GitHub workflow tokens follow principle of least privilege
Details
- Info: topLevel 'contents' permission set to 'read': .github/workflows/dependency-review.yml:5
- Info: topLevel 'contents' permission set to 'read': .github/workflows/nodejs.yml:14
- Info: no jobLevel write permissions found
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
Found 16/28 approved changesets -- score normalized to 5
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/dependency-review.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/less-loader/dependency-review.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/dependency-review.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/less-loader/dependency-review.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:35: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/less-loader/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:40: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/less-loader/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:78: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/less-loader/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:81: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/less-loader/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:97: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/less-loader/nodejs.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/nodejs.yml:92
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 2 out of 3 npmCommand dependencies pinned
Reason
3 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 3
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 26 are checked with a SAST tool
Score
5.6
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to less-loader
storybook-less-loader
storybook less loader
customize-cra-less-loader
Add less loader to any create-react-app using customize-cra
rollup-plugin-postcss-webpack-alias-less-loader
A custom less loader for rollup-plugin-postcss that supports webpack style aliases.
@zougt/some-loader-utils
implementation for less-loader or sass-loader. Compiles Less or sass to CSS.