less-loader

[![npm][npm]][npm-url] [![node][node]][node-url] [![deps][deps]][deps-url] [![tests][tests]][tests-url] [![chat][chat]][chat-url]

处理 less 的 webpack loader。将 Less 编译为 CSS。

要求

此模块需要 Node v6.9.0+ 和 webpack v4.0.0+。

起步

你需要预先安装 less-loader

  1. $ npm install less-loader --save-dev

然后,修改 webpack.config.js

  1. // webpack.config.js
  2. module.exports = {
  3. ...
  4. module: {
  5. rules: [{
  6. test: /\.less$/,
  7. loader: 'less-loader' // 将 Less 编译为 CSS
  8. }]
  9. }
  10. };

然后,通过你偏爱的方式去运行 webpack

The less-loader requires less as peerDependency. Thus you are able to control the versions accurately.

示例

css-loaderstyle-loaderless-loader 链式调用, 可以把所有样式立即应用于 DOM。

  1. // webpack.config.js
  2. module.exports = {
  3. ...
  4. module: {
  5. rules: [{
  6. test: /\.less$/,
  7. use: [{
  8. loader: 'style-loader' // creates style nodes from JS strings
  9. }, {
  10. loader: 'css-loader' // translates CSS into CommonJS
  11. }, {
  12. loader: 'less-loader' // compiles Less to CSS
  13. }]
  14. }]
  15. }
  16. };

You can pass any Less specific options to the less-loader via 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:

  1. // webpack.config.js
  2. module.exports = {
  3. ...
  4. module: {
  5. rules: [{
  6. test: /\.less$/,
  7. use: [{
  8. loader: 'style-loader'
  9. }, {
  10. loader: 'css-loader'
  11. }, {
  12. loader: 'less-loader', options: {
  13. strictMath: true,
  14. noIeCompat: true
  15. }
  16. }]
  17. }]
  18. }
  19. };

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.

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

Starting with less-loader 4, you can now choose between Less’ builtin resolver and webpack’s resolver. By default, webpack’s resolver is used.

webpack resolver

webpack 提供了一种 解析文件的高级机制less-loader 应用一个 Less 插件,并且将所有查询参数传递给 webpack resolver。 所以,你可以从 node_modules 导入你的 less 模块。 只要添加一个 ~ 前缀, 告诉 webpack 去查询 模块

  1. @import '~bootstrap/less/bootstrap';

重要的是只使用 ~ 前缀,因为 ~/ 会解析为主目录。 webpack 需要区分 bootstrap~bootstrap, 因为 CSS 和 Less 文件没有用于导入相对文件的特殊语法。 @import "file"@import "./file"; 写法相同

Non-Less imports

使用 webpack resolver,你可以引入任何文件类型。 你只需要一个导出有效的 Less 代码的 loader。 通常,你还需要设置 issuer 条件, 以确保此规则仅适用于 Less 文件的导入:

  1. // webpack.config.js
  2. module.exports = {
  3. ...
  4. module: {
  5. rules: [{
  6. test: /\.js$/,
  7. issuer: /\.less$/,
  8. use: [{
  9. loader: 'js-to-less-loader'
  10. }]
  11. }]
  12. }
  13. };

Less resolver

If you specify the paths option, the less-loader will not use webpack’s resolver. Modules, that can’t be resolved in the local folder, will be searched in the given paths. This is Less’ default behavior. paths should be an array with absolute paths:

  1. // webpack.config.js
  2. module.exports = {
  3. ...
  4. module: {
  5. rules: [{
  6. test: /\.less$/,
  7. use: [{
  8. loader: 'style-loader'
  9. }, {
  10. loader: 'css-loader'
  11. }, {
  12. loader: 'less-loader', options: {
  13. paths: [
  14. path.resolve(__dirname, 'node_modules')
  15. ]
  16. }
  17. }]
  18. }]
  19. }
  20. };

In this case, all webpack features like importing non-Less files or aliasing won’t work of course.

插件

想要使用 插件, 只需像下面这样简单设置 plugins 选项:

  1. // webpack.config.js
  2. const CleanCSSPlugin = require('less-plugin-clean-css');
  3. module.exports = {
  4. ...
  5. {
  6. loader: 'less-loader', options: {
  7. plugins: [
  8. new CleanCSSPlugin({ advanced: true })
  9. ]
  10. }
  11. }]
  12. ...
  13. };

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:

source map

想要启用 CSS 的 source map, 你需要将 sourceMap 选项传递给 less-loadercss-loader。 所以你的 `webpack.config.js’ 应该是这样:

  1. module.exports = {
  2. ...
  3. module: {
  4. rules: [{
  5. test: /\.less$/,
  6. use: [{
  7. loader: 'style-loader'
  8. }, {
  9. loader: 'css-loader', options: {
  10. sourceMap: true
  11. }
  12. }, {
  13. loader: 'less-loader', options: {
  14. sourceMap: true
  15. }
  16. }]
  17. }]
  18. }
  19. };

Also checkout the sourceMaps example.

如果你要在 Chrome 中编辑原始 Less 文件, 这里有一个很好的博客文章。 此博客文章是关于 Sass 的,但它也适用于 Less。

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.

贡献

如果你从未阅读过我们的贡献指南, 请在上面花点时间。

贡献指南 贡献指南” class=”icon-link” href=”#贡献指南”>

License

MIT MIT” class=”icon-link” href=”#mit”>