I am trying to use .less files with a minimalist React app (created with create-react-app). I've added less
and less-loader
to my package.json as well as a module rule in my webpack.config.js file. The class reference is not being added to the HTML element however (should have class="customColor").
<p>Hello world in a custom color.</p>
I'm wondering what I'm doing wrong.
App.js
import React from 'react';
import './App.css';
import styles from './custom.less';
function App() {
return (
<div className="App">
<header className="App-header">
<p className={styles.customColor}>
Hello world in a custom color.
</p>
</header>
</div>
);
}
export default App;
custom.less
@custom-color: red;
.customColor {
color: @custom-color;
}
package.json
{
"name": "sample",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-scripts": "3.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"devDependencies": {
"less": "^3.10.3",
"less-loader": "^5.0.0"
}
}
webpack.config.js
module.exports = {
rules: [{
test: /\.less$/,
use: [{
loader: 'style-loader',
}, {
loader: 'css-loader', // translates CSS into CommonJS
}, {
loader: 'less-loader', // compiles Less to CSS
}],
}],
}
webpack.config.js
file – Winkelmannpm run eject
but it didn't fix the issue. I think I was missing amodule
attribute in webpack.config.js, so I added it. Still not working though ... my webpack.config.js file looks like this now:module.exports = { module: { rules: [ ....
– Munford