react3.x配置样式按需引入和自定义主题

react3.x配置样式按需引入和自定义主题

安装依赖包

yarn add react-app-rewired customize-cra babel-plugin-import less less-loader@7.1.0

修改package.json

"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  },

添加配置文件

在根目录下创建config-overrides.js文件

config-overrides.js文件内容

const {override, fixBabelImports, addLessLoader} = require('customize-cra');
module.exports = override(
    fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: true,
    }),
    addLessLoader({
        lessOptions: {
            javascriptEnabled: true,
            modifyVars: {'@primary-color': '#1DA57A'},
        }
    }),
);

启动项目

App.js

import React, {Component} from 'react';
// 引入antd
import { Button, Icon } from 'antd';
import './App.css';

class App extends Component {
    render() {
        return (
            <div>
                App.....
                App.....
                App.....
                <Icon type="smile" theme="twoTone" />
                <Button type="primary">Primary Button</Button>
            </div>
        );
    }
}

export default App;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App/>, document.getElementById('root'));

效果

 

 看到绿色按钮就代表成功了!

注意内容

启动项目,如果遇到报错,可能是less-loader这个包的版本太高了,语法有问题。所以卸载之后,安装作者测试的版本即可正常启动

 

posted @ 2021-09-09 11:40  上官靖宇  阅读(86)  评论(0编辑  收藏  举报