我正在尝试将 .less 文件与极简的 React 应用程序(使用create-react-app 创建)一起使用。我已经将less
和添加less-loader
到我的package.json以及我的webpack.config.js文件中的module规则。然而,类引用没有被添加到 HTML 元素中(应该有class="customColor")。
<p>Hello world in a custom color.</p>
我想知道我做错了什么。
应用程序.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-color: red;
.customColor {
color: @custom-color;
}
包.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
}],
}],
}