可以对默认配置进行修改

安装craco

  1. yarn add @craco/craco

安装craco-less

  1. yarn add craco-less

修改package.json得配置,将**react-scriptsg改成craco**

  1. "scripts": {
  2. "start": "craco start",
  3. "build": "craco build",
  4. "test": "craco test",
  5. "eject": "react-scripts eject"
  6. },

修改配置可以读取根目录下的craco.config.js

修改主题颜色

新建craco.config.js

  1. const CracoLessPlugin = require('craco-less');
  2. module.exports = {
  3. plugins: [
  4. {
  5. plugin: CracoLessPlugin,
  6. options: {
  7. lessLoaderOptions: {
  8. lessOptions: {
  9. modifyVars: { '@primary-color': '#1DA57A' },
  10. javascriptEnabled: true,
  11. },
  12. },
  13. },
  14. },
  15. ],
  16. };

使用

  1. import 'antd/dist/antd.less'
  2. import { Button } from 'antd';
  3. function App() {
  4. return (
  5. <div className="App">
  6. <Button type="primary">Primary Button</Button>
  7. <Button>Default Button</Button>
  8. <Button type="dashed">Dashed Button</Button>
  9. <br />
  10. <Button type="text">Text Button</Button>
  11. <Button type="link">Link Button</Button>
  12. </div>
  13. );
  14. }
  15. export default App;

image.png

起别名

  1. const CracoLessPlugin = require('craco-less');
  2. //自带模块
  3. const path=require("path")
  4. const resolve=dir=>path.resolve(__dirname,dir)
  5. module.exports = {
  6. plugins: [
  7. {
  8. plugin: CracoLessPlugin,
  9. options: {
  10. lessLoaderOptions: {
  11. lessOptions: {
  12. modifyVars: { '@primary-color': '#1DA57A' },
  13. javascriptEnabled: true,
  14. },
  15. },
  16. },
  17. },
  18. ],
  19. //起别名
  20. webpack:{
  21. alias:{
  22. //使用@代替根目录
  23. "@":resolve("src"),
  24. //使用component代替src/components
  25. 'components':resolve('src/components')
  26. }
  27. }
  28. };