Home > Enterprise >  using styles from './page.less' React
using styles from './page.less' React

Time:01-10

What I am trying to do:

import styles from './page.less';

<div className={styles.header}></div>

this is my webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader',
        ],
      },
    ],
  },
  plugins: [new HtmlWebpackPlugin({
    template: './src/index.html',
  })],
};

I have install the less and less loader dependency, but it is still not able to use the styling from page.less, where did I went wrong?

CodePudding user response:

Modify import statement:

import './page.less'

<div className="header"></div>

Suppose you have these styles

// page.less

.header {
  // some styles
}

CodePudding user response:

Hi I found you need to wrap your loaders into a single object notation

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

I think it will work. :)

  •  Tags:  
  • Related