在 create-react-app 中使用

    在开始之前,你可能需要安装 。

    工具会自动初始化一个脚手架并安装 React 项目的各种必要依赖,如果在过程中出现网络问题,请尝试配置代理或使用其他 npm registry。

    然后我们进入项目并启动。

    1. $ yarn start

    此时浏览器会访问 http://localhost:3000/ ,看到 Welcome to React 的界面就算成功了。

    引入 antd

    这是 create-react-app 生成的默认目录结构。

    1. ├── README.md
    2. ├── package.json
    3. ├── public
    4. ├── favicon.ico
    5. └── index.html
    6. ├── src
    7. ├── App.css
    8. ├── App.js
    9. ├── App.test.js
    10. ├── index.css
    11. ├── index.js
    12. └── logo.svg
    13. └── yarn.lock

    现在从 yarn 或 npm 安装并引入 antd。

    1. $ yarn add antd

    修改 src/App.js,引入 antd 的按钮组件。

    1. import React from 'react';
    2. import { Button } from 'antd';
    3. import './App.css';
    4. const App = () => (
    5. <Button type="primary">Button</Button>
    6. </div>
    7. );
    8. export default App;

    修改 ,在文件顶部引入 antd/dist/antd.css

    好了,现在你应该能看到页面上已经有了 antd 的蓝色按钮组件,接下来就可以继续选用其他组件开发应用了。其他开发流程你可以参考 create-react-app 的。

    我们现在已经把 antd 组件成功运行起来了,开始开发你的应用吧!

    此时我们需要对 create-react-app 的默认配置进行自定义,这里我们使用 react-app-rewired (一个对 create-react-app 进行自定义配置的社区解决方案)。

    引入 react-app-rewired 并修改 package.json 里的启动配置。由于新的 版本的关系,你还需要安装 customize-cra

    1. $ yarn add react-app-rewired customize-cra
    1. /* package.json */
    2. "scripts": {
    3. - "start": "react-scripts start",
    4. + "start": "react-app-rewired start",
    5. - "build": "react-scripts build",
    6. + "build": "react-app-rewired build",
    7. - "test": "react-scripts test",
    8. + "test": "react-app-rewired test",
    9. }

    然后在项目根目录创建一个 config-overrides.js 用于修改默认配置。

    1. module.exports = function override(config, env) {
    2. // do stuff with the webpack config...
    3. return config;
    4. };

    是一个用于按需加载组件代码和样式的 babel 插件(原理),现在我们尝试安装它并修改 config-overrides.js 文件。

    1. $ yarn add babel-plugin-import

    然后移除前面在 src/App.css 里全量添加的 @import '~antd/dist/antd.css'; 样式代码,并且按下面的格式引入模块。

    1. // src/App.js
    2. import React, { Component } from 'react';
    3. - import Button from 'antd/es/button';
    4. + import { Button } from 'antd';
    5. import './App.css';
    6. class App extends Component {
    7. render() {
    8. <Button type="primary">Button</Button>
    9. </div>
    10. );
    11. }
    12. }
    13. export default App;

    最后重启 yarn start 访问页面,antd 组件的 js 和 css 代码都会按需加载,你在控制台也不会看到这样的。关于按需加载的原理和其他方式可以阅读这里

    自定义主题

    按照 的要求,自定义主题需要用到 less 变量覆盖功能。我们可以引入 customize-cra 中提供的 less 相关的函数 addLessLoader 来帮助加载 less 样式,同时修改 config-overrides.js 文件如下。

    1. $ yarn add less less-loader
    1. - const { override, fixBabelImports } = require('customize-cra');
    2. + const { override, fixBabelImports, addLessLoader } = require('customize-cra');
    3. module.exports = override(
    4. fixBabelImports('import', {
    5. libraryName: 'antd',
    6. libraryDirectory: 'es',
    7. - style: 'css',
    8. + style: true,
    9. }),
    10. + addLessLoader({
    11. + javascriptEnabled: true,
    12. + modifyVars: { '@primary-color': '#1DA57A' },
    13. + }),
    14. );

    这里利用了 的 modifyVars 来进行主题配置,变量和其他配置方式可以参考 配置主题 文档。

    修改后重启 ,如果看到一个绿色的按钮就说明配置成功了。

    使用 Day.js 替换 momentjs 优化打包大小

    你可以使用 插件用 Day.js 替换 momentjs 来大幅减小打包大小。

    1. $ yarn add antd-dayjs-webpack-plugin

    你也可以使用 create-react-app 提供的 yarn run eject 命令将所有内建的配置暴露出来。不过这种配置方式需要你自行探索,不在本文讨论范围内。

    源码和其他脚手架

    以上是在 create-react-app 中使用 antd 的相关实践,你也可以借鉴此文的做法在自己的 webpack 工作流中使用 antd,更多 webpack 配置可参考 。(例如加入 moment noParse 避免加载所有语言文件)

    React 生态圈中还有很多优秀的脚手架,使用它们并引入 antd 时,你可能会遇到一些问题,下面是一些著名脚手架使用 antd 的范例,包括本文的 create-react-app。