在webpack中使用less-loader

时间:2018-05-12 05:08:55

标签: webpack less less-loader

我正在使用webpack创建一个bundle.js文件。我已经决定我也想开始使用LESS,并且我已经尝试按照说明添加less-loader:

https://github.com/webpack-contrib/less-loader

我跑了:     npm-install less-loader --save-dev

然后我更新了webpack.config.js以包含规则:

module.exports = {
    entry: "./src/entry.js",
    output: {
        path: __dirname,
        filename: "./bundle.js",
        publicPath: "/"
    },
    module: {
    //******** BELOW IS THE RULES U ADDED *************/
        rules: [{
            test: /\.less$/,
            use: [{
              loader: 'style-loader' // creates style nodes from JS strings
            }, {
              loader: 'css-loader' // translates CSS into CommonJS
            }, {
              loader: 'less-loader' // compiles Less to CSS
            }]
        }],
         //******** ABOVE IS THE RULES U ADDED *************/
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            }
        ],
        rules: [
            {
              test: /\.css$/,
              use: [
                { loader: "style-loader" },
                { loader: "css-loader" }
              ]
            },
            {
                test: /\.(html)$/,
                use: {
                  loader: 'html-loader',
                  options: {
                    attrs: [':data-src']                  
                  }
                }
            }
        ]
    },
    devServer: {
        stats: 'errors-only'
    },
    stats: {
        colors: true
    },
    devtool: 'source-map'
};

然后我添加了一个较少的文件,并且需要它:     要求( “./引导/自举-iso.less”);

less文件只是bootstrap的包装器(我也尝试过简单的css)

#entry {
    @import (less) './css/bootstrap.css';
  }

但我继续收到以下错误:

ERROR in ./src/Bootstrap/bootstrap-iso.less
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| #entry {
|     @import (less) './css/bootstrap.css';
|   }
 @ ./src/entry.js 13:0-41

似乎加载器未正确加载?

1 个答案:

答案 0 :(得分:3)

这是我的错误,我不应该有多个规则属性..请参阅工作webpack.config.js片段:

module: {
        rules: [{
            test: /\.(less)$/,
            use: [{
              loader: 'style-loader' // creates style nodes from JS strings
            }, {
              loader: 'css-loader' // translates CSS into CommonJS
            }, {
              loader: 'less-loader' // compiles Less to CSS
            }]},
            {
                test: /\.js$/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.css$/,
                use: [
                  { loader: "style-loader" },
                  { loader: "css-loader" }
                ]
              },
              {
                  test: /\.(html)$/,
                  use: {
                    loader: 'html-loader',
                    options: {
                      attrs: [':data-src']                  
                    }
                  }
              }
        ],
    },