Projects created through CRA need to modify the build configuration to support less.
You can run NPM run eject to change the configuration. However, eject is irreversible and will not follow the react-Script version of the official upgrade project.
If you don’t want to override the configuration with eject, you can do so in one of the following ways:
- Officially supported through CRA
--scripts-version
Parameter to create the project using your own overridereact-scripts
The package.The official introduction- use
react-app-rewired + customize-cra
Combined coverage configuration- use
craco
Cover the configuration
This paper will complete the configuration of LESS through CrACO
Configuration craco
- The installation
@craco/craco
npm i @craco/craco -D
Copy the code
- Modify the
package.json
In thescript
The label
"scripts": {
"start": "craco start"."build": "craco build"."test": "craco test",}Copy the code
- Created in the project root directory
craco.config.js
/* craco.config.js */
module.exports = function () {... };Copy the code
The basic Configuration of Craco is complete. The craco.config.js Configuration file structure can be queried in the CrACO official document: Configuration Overview
Enable the sass
Sass-loader support has been added to CRA scaffolding. To enable Sass, simply install Node-sass.
- The installation
node-sass
npm install node-sass --save
Copy the code
- will
src/App.css
renamesrc/App.scss
- If you need to enable
CSS Modules
, change the CSS file extension to[name].module.scss
或[name].module.sass
- CRA scaffolding use
[name].module.css
File naming convention supportCSS ModulesAnd regular CSS.- CSS Modules allow automatic creation
[filename]_[classname]__[hash]
The unique classname of the format determines the scope of the CSS
Enable the less
CRA scaffolding does not support less by default, so extension is required
- Installing related Loaders
/* Install craco-less, support some webpack loader */
npn i craco-less -S
/* Install less and less-loader */
npm install --save-dev less-loader less
Copy the code
- in
craco.config.js
Added related configurations to:
const CracoLessPlugin = require('craco-less');
const { loaderByName } = require('@craco/craco');
module.exports = function (webpackEnv) {
const lessModuleRegex = /\.module\.less$/;
return {
plugins: [{plugin: CracoLessPlugin,
options: {
// less loader option
lessLoaderOptions: {
lessOptions: {
/* If you need to customize themes in your project using TDesign or AntDesign component libraries, you can add the corresponding less variable */ in modifyVars
modifyVars: {
@primary-color: '#2378ff'
},
javascriptEnabled: true,}},modifyLessRule(lessRule) {
lessRule.exclude = lessModuleRegex;
return lessRule;
},
modifyLessModuleRule(lessModuleRule) {
// configure the file suffix
lessModuleRule.test = lessModuleRegex;
// configure the generated local ident name
const cssLoader = lessModuleRule.use.find(loaderByName('css-loader'));
cssLoader.options.modules = {
/*
注意这里的命名规则:
- CRA脚手架创建的项目是可以直接使用css modules的,css文件的命名规则默认是[local]_[hash:base64:5]
- 这里使用css modules的命名规则
*/
localIdentName: '[local]_[hash:base64:5]'};returnlessModuleRule; },},},],}; };Copy the code
3. Change the CSS file extension to [name].less or [name].module.less (enable CSS Modules)