webpack4 configure less-loader

The webpack version of this tutorial: 4.46.0
Insert picture description here
Our webpack.config.js configuration is as follows:

const path = require('path')

//module.exports是commonjs语法
module.exports = {
    
    
  entry: './src/index.js',
  output: {
    
    
    filename: 'built.js',
    path: path.join(__dirname, 'build')
  },
  module: {
    
    
    rules: [
      {
    
    
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      }
    ]
  },
  plugins: [],
  mode: 'development'
}

Introduce a less file, if there is no configuration, an webpack -c .\webpack.config.jserror will be reported when executed :
Insert picture description here

We need to configure less-loader:

{
    
    
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader'
        ]
      }

Then install less-loaderandless

npm i less-loader less -D

Insert picture description here
success!

Guess you like

Origin blog.csdn.net/dyw3390199/article/details/112713311