UNPKG

less-loader

Version:

A Less loader for webpack. Compiles Less to CSS.

781 lines (630 loc) • 20.6 kB
<div align="center"> <a href="https://github.com/webpack/webpack"> <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg"> </a> </div> [![npm][npm]][npm-url] [![node][node]][node-url] [![tests][tests]][tests-url] [![cover][cover]][cover-url] [![discussion][discussion]][discussion-url] [![size][size]][size-url] # less-loader A Less loader for webpack that compiles Less files into CSS. ## Getting Started To begin, you'll need to install `less` and `less-loader`: ```console npm install less less-loader --save-dev ``` or ```console yarn add -D less less-loader ``` or ```console pnpm add -D less less-loader ``` Then add the loader to your `webpack` configuration. For example: **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, // Uses the built-in CSS support of webpack, i.e. `.module.less` files // are treated as CSS modules, other files are treated as plain CSS type: "css/auto", // Compiles Less to CSS use: ["less-loader"], }, ], }, experiments: { // Enables the built-in CSS support of webpack css: true, }, }; ``` > [!NOTE] > > The built-in CSS support of webpack is the recommended way to handle the generated CSS, and needs webpack 5.87.0 or later. Since webpack 5.109.0 it is on by default (`experiments.css` defaults to `"auto"`), but `"auto"` switches it off when another `module.rules` entry handles `.css` files, so the examples below keep `experiments.css: true`. If such a rule still uses `css-loader`, give it `type: "javascript/auto"` so built-in CSS leaves those files to it. > Its output is a `.css` file the page has to link: let webpack build the page — an [HTML entry or `output.html: true`](https://webpack.js.org/guides/native-html/) (webpack 5.108.0 or later; before 5.109.0 also set `experiments.html: true`) adds the `<link>` for you — set [`exportType: "style"`](https://webpack.js.org/guides/native-css/#output-modes-exporttype) (webpack 5.106.0 or later) to inject the styles from JavaScript instead, or link the emitted file yourself. > Alternatively you can still chain the loader with the deprecated [`css-loader`](https://github.com/webpack/css-loader) and [`style-loader`](https://github.com/webpack/style-loader) (or the [`mini-css-extract-plugin`](https://github.com/webpack/mini-css-extract-plugin)), see [Using `css-loader` and `style-loader`](#using-css-loader-and-style-loader) — and the [Native CSS migration guide](https://webpack.js.org/guides/native-css/#migration-guide) to move off them. Finally, run `webpack` using the method you normally use (e.g., via CLI or an npm script). ## Options - **[`lessOptions`](#lessoptions)** - **[`additionalData`](#additionalData)** - **[`sourceMap`](#sourcemap)** - **[`webpackImporter`](#webpackimporter)** - **[`implementation`](#implementation)** - **[`lessLogAsWarnOrErr`](#lesslogaswarnorerr)** ### `lessOptions` Type: <!-- eslint-skip --> ```ts type 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](https://webpack.js.org/configuration/module/#ruleoptions--rulequery). 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 directly to Less. **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { lessOptions: { strictMath: true, }, }, }, ], }, ], }, experiments: { css: true, }, }; ``` #### `function` Allows setting the Less options dynamically based on the loader context. ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { 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.less") { return { paths: ["absolute/path/c", "absolute/path/d"], }; } return { paths: ["absolute/path/a", "absolute/path/b"], }; }, }, }, ], }, ], }, experiments: { css: true, }, }; ``` ### `additionalData` Type: ```ts type additionalData = | string | ((content: string, loaderContext: LoaderContext) => string); ``` Default: `undefined` Prepends or 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` ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { additionalData: `@env: ${process.env.NODE_ENV};`, }, }, ], }, ], }, experiments: { css: true, }, }; ``` #### `function` ##### `Sync` ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { additionalData: (content, 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.less") { return `@value: 100px;${content}`; } return `@value: 200px;${content}`; }, }, }, ], }, ], }, experiments: { css: true, }, }; ``` ##### `Async` ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { additionalData: async (content, 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.less") { return `@value: 100px;${content}`; } return `@value: 200px;${content}`; }, }, }, ], }, ], }, experiments: { css: true, }, }; ``` ### `sourceMap` Type: ```ts type sourceMap = boolean; ``` Default: depends on the `compiler.devtool` value By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option. All values enable source map generation except `eval` and `false` value. **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { sourceMap: true, }, }, ], }, ], }, experiments: { css: true, }, }; ``` ### `webpackImporter` Type: ```ts type webpackImporter = boolean | "only"; ``` Default: `true` Enables or disables the default `webpack` importer. This can improve performance in some cases. Use it with caution because aliases and `@import` from [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules) will not work. **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { webpackImporter: false, }, }, ], }, ], }, experiments: { css: true, }, }; ``` ### `implementation` Type: ```ts type implementation = object | string; ``` > less-loader compatible with both 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` Example using a Less instance: **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { implementation: require("less"), }, }, ], }, ], }, experiments: { css: true, }, }; ``` #### `string` Example using a resolved Less module path: **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { implementation: require.resolve("less"), }, }, ], }, ], }, experiments: { css: true, }, }; ``` ### `lessLogAsWarnOrErr` Type: ```ts type lessLogAsWarnOrErr = boolean; ``` Default: `true` `Less` warnings and errors will be treated as webpack warnings and errors, instead of being logged silently. **warning.less** ```less div { &:extend(.body1); } ``` If `lessLogAsWarnOrErr` is set to `false` it will be just a log and webpack will compile successfully, but if you leave the default value (or set this option to `true`) webpack will compile fail with a warning(or error), and can break the build if configured accordingly. **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { lessLogAsWarnOrErr: true, }, }, ], }, ], }, experiments: { css: true, }, }; ``` ## Examples ### Normal usage Set the module `type` to `css/auto` and enable `experiments.css` to let webpack handle the generated CSS with its built-in CSS support, without any extra loaders or plugins. **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", // Handles the generated CSS using the built-in CSS support of webpack use: ["less-loader"], // Compiles Less to CSS }, ], }, experiments: { css: true, }, }; ``` The `css/auto` module type treats `*.module.less` files as [CSS modules](#css-modules) and any other file as plain CSS. Use `type: "css"` to always treat the file as plain CSS, or `type: "css/module"` to always treat it as a CSS module. Unfortunately, Less doesn't map all options 1-by-1 to camelCase. When in doubt, [check their executable](https://github.com/less/less.js/blob/3.x/bin/lessc) and search for the dash-case option. ### Using `css-loader` and `style-loader` The built-in CSS support of webpack is not mandatory: on setups that still need them, you can chain the `less-loader` with the deprecated [`css-loader`](https://github.com/webpack/css-loader) and [`style-loader`](https://github.com/webpack/style-loader) to immediately apply all styles to the DOM. New projects should use the built-in CSS support instead — see the [Native CSS migration guide](https://webpack.js.org/guides/native-css/#migration-guide). **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, use: [ { loader: "style-loader", // Creates style nodes from JS strings }, { loader: "css-loader", // Translates CSS into CommonJS }, { loader: "less-loader", // Compiles Less to CSS }, ], }, ], }, }; ``` Note that in this case the `type` and `experiments.css` options should not be set for this rule, and options like `sourceMap` have to be enabled for the `css-loader` too. ### 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** ```js module.exports = { devtool: "source-map", // any "source-map"-like devtool is possible module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { sourceMap: true, }, }, ], }, ], }, experiments: { css: true, }, }; ``` If you want to edit the original Less files inside Chrome, [there's a good blog post](https://medium.com/@toolmantim/getting-started-with-css-sourcemaps-and-in-browser-sass-editing-b4daab987fb0). The blog post is about Sass but it also works for Less. ### In production The built-in CSS support of webpack always extracts style sheets into dedicated files, so your styles are not dependent on JavaScript, which improves performance and cacheability. The name of the generated files can be configured using the [`output.cssFilename`](https://webpack.js.org/configuration/output/#outputcssfilename) and [`output.cssChunkFilename`](https://webpack.js.org/configuration/output/#outputcsschunkfilename) options. When you chain the loader with `css-loader` and `style-loader` instead, extract the style sheets into a dedicated file in production using the deprecated [MiniCssExtractPlugin](https://github.com/webpack/mini-css-extract-plugin). ### 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](https://webpack.js.org/configuration/resolve/). `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`. ```css @import "bootstrap/less/bootstrap"; ``` Using `~` prefix (e.g., @import "~bootstrap/less/bootstrap";) 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`](https://webpack.js.org/configuration/resolve/#resolvemodules). Default resolver options can be modified by [`resolve.byDependency`](https://webpack.js.org/configuration/resolve/#resolvebydependency): **webpack.config.js** ```js module.exports = { devtool: "source-map", // any "source-map"-like devtool is possible module: { rules: [ { test: /\.less$/i, type: "css/auto", use: ["less-loader"], }, ], }, experiments: { css: true, }, resolve: { byDependency: { // More options can be found here https://webpack.js.org/configuration/resolve/ less: { mainFiles: ["custom"], }, }, }, }; ``` #### 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** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: [ { loader: "less-loader", options: { lessOptions: { paths: [path.resolve(__dirname, "node_modules")], }, }, }, ], }, ], }, experiments: { css: true, }, }; ``` ### Plugins In order to use [Less plugins](http://lesscss.org/usage/#plugins), simply set the `plugins` option like this: **webpack.config.js** <!-- eslint-skip --> ```js const CleanCSSPlugin = require('less-plugin-clean-css'); module.exports = { ... { loader: 'less-loader', options: { lessOptions: { plugins: [ new CleanCSSPlugin({ advanced: true }), ], }, }, }, ... }; ``` > [!NOTE] > > Access to the [loader context](https://webpack.js.org/api/loaders/#the-loader-context) inside a custom plugin can be done using the `pluginManager.webpackLoaderContext` property. ```js module.exports = { install(less, pluginManager, functions) { functions.add( "pi", () => // Loader context is available in `pluginManager.webpackLoaderContext` Math.PI, ); }, }; ``` ### Extracting style sheets Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or [Hot Module Replacement(HMR)](https://webpack.js.org/concepts/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](https://en.wikipedia.org/wiki/Flash_of_unstyled_content) might be visible. Thus it's often still better to have them as separate files in your final production build. The built-in CSS support of webpack does this out of the box: every entry point and chunk gets its own style sheet, no extra plugin required. When you chain the loader with the `css-loader` instead, use the deprecated [`MiniCssExtractPlugin`](https://github.com/webpack/mini-css-extract-plugin) to extract a style sheet from the bundle. ### CSS modules With the built-in CSS support of webpack, `*.module.less` files are treated as [CSS modules](https://github.com/css-modules/css-modules) when the module `type` is `css/auto`, and all files are treated as CSS modules when the module `type` is `css/module`: **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.less$/i, type: "css/auto", use: ["less-loader"], }, ], }, experiments: { css: true, }, }; ``` **index.js** ```js import * as styles from "./style.module.less"; document.body.className = styles.box; ``` There is a known problem when using Less with CSS modules regarding relative file paths in `url(...)` statements. [See this issue for an explanation](https://github.com/webpack/less-loader/issues/109#issuecomment-253797335). ## Contributing We welcome all contributions! If you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests. [CONTRIBUTING](https://github.com/webpack/less-loader?tab=contributing-ov-file#contributing) ## License [MIT](./LICENSE) [npm]: https://img.shields.io/npm/v/less-loader.svg [npm-url]: https://npmjs.com/package/less-loader [node]: https://img.shields.io/node/v/less-loader.svg [node-url]: https://nodejs.org [tests]: https://github.com/webpack/less-loader/workflows/less-loader/badge.svg [tests-url]: https://github.com/webpack/less-loader/actions [cover]: https://codecov.io/gh/webpack/less-loader/branch/main/graph/badge.svg [cover-url]: https://codecov.io/gh/webpack/less-loader [discussion]: https://img.shields.io/github/discussions/webpack/webpack [discussion-url]: https://github.com/webpack/webpack/discussions [size]: https://packagephobia.now.sh/badge?p=less-loader [size-url]: https://packagephobia.now.sh/result?p=less-loader