{"name":"less-loader","version":"12.3.1","keywords":["webpack","loader","less","lesscss","less.js","css","preprocessor"],"author":{"name":"Johannes Ewald @jhnns"},"license":"MIT","_id":"less-loader@12.3.1","maintainers":[{"name":"evilebottnawi","email":"sheo13666q@gmail.com"},{"name":"sokra","email":"tobias.koppers@googlemail.com"},{"name":"jhnns","email":"mail@johannesewald.de"},{"name":"15000621931","email":"784487301@qq.com"},{"name":"ev1stensberg","email":"evenstensberg@gmail.com"},{"name":"__hai","email":"haijie0619@gmail.com"}],"homepage":"https://github.com/webpack/less-loader","bugs":{"url":"https://github.com/webpack/less-loader/issues"},"dist":{"shasum":"4b80179507d522fa3082c19a09d08e50b9495ac3","tarball":"http://registry-npm.ywwl.com/less-loader/download/less-loader-12.3.1.tgz","fileCount":7,"integrity":"sha512-JZZmG7gMzoDP3VGeEG8Sh6FW5wygB5jYL7Wp29FFihuRTsIBacqO3LbRPr2yStYD11riVf13selLm/CPFRDBRQ==","signatures":[{"sig":"MEUCIACUlbOt13mxGxJ3uP6kBMNgJR7xOmNQurDil6Fx6d4YAiEA4h0SY4/7aRxwsR+xbtY5dpZsYU2owyqxfLyepMsiRVY=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":34207,"size":10051,"noattachment":false,"key":"/less-loader/-/less-loader-12.3.1.tgz"},"main":"dist/cjs.js","engines":{"node":">= 18.12.0"},"funding":{"url":"https://opencollective.com/webpack","type":"opencollective"},"gitHead":"ef02ce4d7534d9730431fe38ca11b25fce62f5b4","scripts":{"fix":"npm-run-all -l fix:js fix:prettier","lint":"npm-run-all -l -p \"lint:**\"","test":"npm run test:coverage","build":"cross-env NODE_ENV=production babel src -d dist --copy-files","clean":"del-cli dist","start":"npm run build -- -w","fix:js":"npm run lint:js -- --fix","lint:js":"eslint --cache .","prepare":"husky && npm run build","pretest":"npm run lint","release":"standard-version","prebuild":"npm run clean","security":"npm audit --production","test:only":"cross-env NODE_ENV=test jest","commitlint":"commitlint --from=main","test:watch":"npm run test:only -- --watch","fix:prettier":"npm run lint:prettier -- --write","lint:prettier":"prettier --cache --list-different .","lint:spelling":"cspell --cache --no-must-find-files --quiet \"**/*.*\"","test:coverage":"npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage"},"_npmUser":{"name":"evilebottnawi","email":"sheo13666q@gmail.com"},"repository":{"url":"git+https://github.com/webpack/less-loader.git","type":"git"},"_npmVersion":"11.6.2","description":"A Less loader for webpack. Compiles Less to CSS.","directories":{},"_nodeVersion":"24.11.1","_hasShrinkwrap":false,"devDependencies":{"del":"^7.1.0","jest":"^30.0.0","less":"^4.2.0","husky":"^9.1.3","memfs":"^4.9.3","cspell":"^8.10.0","eslint":"^9.32.0","del-cli":"^5.1.0","globals":"^16.3.0","webpack":"^5.92.1","prettier":"^3.3.2","cross-env":"^7.0.3","@babel/cli":"^7.24.7","@eslint/js":"^9.32.0","babel-jest":"^30.0.0","strip-ansi":"^7.1.0","@babel/core":"^7.24.7","lint-staged":"^15.2.7","npm-run-all":"^4.1.5","@commitlint/cli":"^18.6.1","eslint-plugin-n":"^17.21.1","@eslint/markdown":"^7.1.0","less-plugin-glob":"^3.0.0","standard-version":"^9.3.1","@babel/preset-env":"^7.24.7","typescript-eslint":"^8.38.0","eslint-plugin-jest":"^29.0.1","eslint-plugin-import":"^2.32.0","eslint-config-webpack":"^4.5.1","eslint-plugin-unicorn":"^60.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-prettier":"^5.5.3","@stylistic/eslint-plugin":"^5.2.2","@commitlint/config-conventional":"^18.6.2"},"peerDependencies":{"less":"^3.5.0 || ^4.0.0","webpack":"^5.0.0","@rspack/core":"0.x || ^1.0.0 || ^2.0.0-0"},"peerDependenciesMeta":{"webpack":{"optional":true},"@rspack/core":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/less-loader_12.3.1_1770329221316_0.330052861212635","host":"s3://npm-registry-packages-npm-production"},"readme":"<div align=\"center\">\n  <a href=\"https://github.com/webpack/webpack\">\n    <img width=\"200\" height=\"200\" src=\"https://webpack.js.org/assets/icon-square-big.svg\">\n  </a>\n</div>\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![tests][tests]][tests-url]\n[![cover][cover]][cover-url]\n[![discussion][discussion]][discussion-url]\n[![size][size]][size-url]\n\n# less-loader\n\nA Less loader for webpack that compiles Less files into CSS.\n\n## Getting Started\n\nTo begin, you'll need to install `less` and `less-loader`:\n\n```console\nnpm install less less-loader --save-dev\n```\n\nor\n\n```console\nyarn add -D less less-loader\n```\n\nor\n\n```console\npnpm add -D less less-loader\n```\n\nThen add the loader to your `webpack` configuration. For example:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          // compiles Less to CSS\n          \"style-loader\",\n          \"css-loader\",\n          \"less-loader\",\n        ],\n      },\n    ],\n  },\n};\n```\n\nFinally, run `webpack` using the method you normally use (e.g., via CLI or an npm script).\n\n## Options\n\n- **[`lessOptions`](#lessoptions)**\n- **[`additionalData`](#additionalData)**\n- **[`sourceMap`](#sourcemap)**\n- **[`webpackImporter`](#webpackimporter)**\n- **[`implementation`](#implementation)**\n- **[`lessLogAsWarnOrErr`](#lesslogaswarnorerr)**\n\n### `lessOptions`\n\nType:\n\n<!-- eslint-skip -->\n\n```ts\ntype lessOptions = import('less').options | ((loaderContext: LoaderContext) => import('less').options})\n```\n\nDefault: `{ relativeUrls: true }`\n\nYou 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.\n\nSince we're passing these options to Less programmatically, you need to pass them in camelCase here:\n\n#### `object`\n\nUse an object to pass options directly to Less.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          {\n            loader: \"style-loader\",\n          },\n          {\n            loader: \"css-loader\",\n          },\n          {\n            loader: \"less-loader\",\n            options: {\n              lessOptions: {\n                strictMath: true,\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### `function`\n\nAllows setting the Less options dynamically based on the loader context.\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              lessOptions: (loaderContext) => {\n                // More information about available properties https://webpack.js.org/api/loaders/\n                const { resourcePath, rootContext } = loaderContext;\n                const relativePath = path.relative(rootContext, resourcePath);\n\n                if (relativePath === \"styles/foo.less\") {\n                  return {\n                    paths: [\"absolute/path/c\", \"absolute/path/d\"],\n                  };\n                }\n\n                return {\n                  paths: [\"absolute/path/a\", \"absolute/path/b\"],\n                };\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `additionalData`\n\nType:\n\n```ts\ntype additionalData =\n  | string\n  | ((content: string, loaderContext: LoaderContext) => string);\n```\n\nDefault: `undefined`\n\nPrepends or Appends `Less` code to the actual entry file.\nIn this case, the `less-loader` will not override the source but just **prepend** the entry's content.\n\nThis is especially useful when some of your Less variables depend on the environment.\n\n> 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.\n\n#### `string`\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              additionalData: `@env: ${process.env.NODE_ENV};`,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### `function`\n\n##### `Sync`\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              additionalData: (content, loaderContext) => {\n                // More information about available properties https://webpack.js.org/api/loaders/\n                const { resourcePath, rootContext } = loaderContext;\n                const relativePath = path.relative(rootContext, resourcePath);\n\n                if (relativePath === \"styles/foo.less\") {\n                  return `@value: 100px;${content}`;\n                }\n\n                return `@value: 200px;${content}`;\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n##### `Async`\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              additionalData: async (content, loaderContext) => {\n                // More information about available properties https://webpack.js.org/api/loaders/\n                const { resourcePath, rootContext } = loaderContext;\n                const relativePath = path.relative(rootContext, resourcePath);\n\n                if (relativePath === \"styles/foo.less\") {\n                  return `@value: 100px;${content}`;\n                }\n\n                return `@value: 200px;${content}`;\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `sourceMap`\n\nType:\n\n```ts\ntype sourceMap = boolean;\n```\n\nDefault: depends on the `compiler.devtool` value\n\nBy default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option.\nAll values enable source map generation except `eval` and `false` value.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          {\n            loader: \"css-loader\",\n            options: {\n              sourceMap: true,\n            },\n          },\n          {\n            loader: \"less-loader\",\n            options: {\n              sourceMap: true,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `webpackImporter`\n\nType:\n\n```ts\ntype webpackImporter = boolean | \"only\";\n```\n\nDefault: `true`\n\nEnables or disables the default `webpack` importer.\n\nThis 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.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              webpackImporter: false,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `implementation`\n\nType:\n\n```ts\ntype implementation = object | string;\n```\n\n> less-loader compatible with both Less 3 and 4 versions\n\nThe special `implementation` option determines which implementation of Less to use. Overrides the locally installed `peerDependency` version of `less`.\n\n**This option is only really useful for downstream tooling authors to ease the Less 3-to-4 transition.**\n\n#### `object`\n\nExample using a Less instance:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              implementation: require(\"less\"),\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### `string`\n\nExample using a resolved Less module path:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              implementation: require.resolve(\"less\"),\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `lessLogAsWarnOrErr`\n\nType:\n\n```ts\ntype lessLogAsWarnOrErr = boolean;\n```\n\nDefault: `true`\n\n`Less` warnings and errors will be treated as webpack warnings and errors, instead of being logged silently.\n\n**warning.less**\n\n```less\ndiv {\n  &:extend(.body1);\n}\n```\n\nIf `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.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"less-loader\",\n            options: {\n              lessLogAsWarnOrErr: true,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n## Examples\n\n### Normal usage\n\nChain the `less-loader` with [`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.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          {\n            loader: \"style-loader\", // Creates style nodes from JS strings\n          },\n          {\n            loader: \"css-loader\", // Translates CSS into CommonJS\n          },\n          {\n            loader: \"less-loader\", // Compiles Less to CSS\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\nUnfortunately, 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.\n\n### Source maps\n\nTo 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`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  devtool: \"source-map\", // any \"source-map\"-like devtool is possible\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          \"style-loader\",\n          {\n            loader: \"css-loader\",\n            options: {\n              sourceMap: true,\n            },\n          },\n          {\n            loader: \"less-loader\",\n            options: {\n              sourceMap: true,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\nIf 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.\n\n### In production\n\nUsually, it's recommended to extract the style sheets into a dedicated file in production using the [MiniCssExtractPlugin](https://github.com/webpack/mini-css-extract-plugin). This way your styles are not dependent on JavaScript, improving performance and cacheability.\n\n### Imports\n\nFirst we try to use built-in `less` resolve logic, then `webpack` resolve logic.\n\n#### Webpack Resolver\n\n`webpack` provides an [advanced mechanism to resolve files](https://webpack.js.org/configuration/resolve/).\n`less-loader` applies a Less plugin that passes all queries to the webpack resolver if `less` could not resolve `@import`.\nThus you can import your Less modules from `node_modules`.\n\n```css\n@import \"bootstrap/less/bootstrap\";\n```\n\nUsing `~` 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.\nWhy 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).\n\nDefault resolver options can be modified by [`resolve.byDependency`](https://webpack.js.org/configuration/resolve/#resolvebydependency):\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  devtool: \"source-map\", // any \"source-map\"-like devtool is possible\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\"style-loader\", \"css-loader\", \"less-loader\"],\n      },\n    ],\n  },\n  resolve: {\n    byDependency: {\n      // More options can be found here https://webpack.js.org/configuration/resolve/\n      less: {\n        mainFiles: [\"custom\"],\n      },\n    },\n  },\n};\n```\n\n#### Less Resolver\n\nIf 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:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.less$/i,\n        use: [\n          {\n            loader: \"style-loader\",\n          },\n          {\n            loader: \"css-loader\",\n          },\n          {\n            loader: \"less-loader\",\n            options: {\n              lessOptions: {\n                paths: [path.resolve(__dirname, \"node_modules\")],\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### Plugins\n\nIn order to use [Less plugins](http://lesscss.org/usage/#plugins), simply set the `plugins` option like this:\n\n**webpack.config.js**\n\n<!-- eslint-skip -->\n\n```js\nconst CleanCSSPlugin = require('less-plugin-clean-css');\n\nmodule.exports = {\n  ...\n    {\n      loader: 'less-loader',\n      options: {\n        lessOptions: {\n          plugins: [\n            new CleanCSSPlugin({ advanced: true }),\n          ],\n        },\n      },\n    },\n  ...\n};\n```\n\n> [!NOTE]\n>\n> 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.\n\n```js\nmodule.exports = {\n  install(less, pluginManager, functions) {\n    functions.add(\n      \"pi\",\n      () =>\n        // Loader context is available in `pluginManager.webpackLoaderContext`\n\n        Math.PI,\n    );\n  },\n};\n```\n\n### Extracting style sheets\n\nBundling 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.\n\nIn 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.\n\nThere are two possibilities to extract a style sheet from the bundle:\n\n- [`extract-loader`](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)\n- [`MiniCssExtractPlugin`](https://github.com/webpack/mini-css-extract-plugin) (more complex, but works in all use-cases)\n\n### CSS modules gotcha\n\nThere is a known problem when using Less with [CSS modules](https://github.com/css-modules/css-modules) regarding relative file paths in `url(...)` statements.\n[See this issue for an explanation](https://github.com/webpack/less-loader/issues/109#issuecomment-253797335).\n\n## Contributing\n\nWe welcome all contributions!\nIf you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.\n\n[CONTRIBUTING](https://github.com/webpack/less-loader?tab=contributing-ov-file#contributing)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/less-loader.svg\n[npm-url]: https://npmjs.com/package/less-loader\n[node]: https://img.shields.io/node/v/less-loader.svg\n[node-url]: https://nodejs.org\n[tests]: https://github.com/webpack/less-loader/workflows/less-loader/badge.svg\n[tests-url]: https://github.com/webpack/less-loader/actions\n[cover]: https://codecov.io/gh/webpack/less-loader/branch/main/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack/less-loader\n[discussion]: https://img.shields.io/github/discussions/webpack/webpack\n[discussion-url]: https://github.com/webpack/webpack/discussions\n[size]: https://packagephobia.now.sh/badge?p=less-loader\n[size-url]: https://packagephobia.now.sh/result?p=less-loader\n","publish_time":1770329221463,"_cnpm_publish_time":1770329221463,"dist-tags":{"latest":"13.1.0"}}