create-react-app 是業(yè)界最優(yōu)秀的 React 應用開發(fā)工具之一,本文會嘗試在 create-react-app 創(chuàng)建的工程中使用 antd 組件,并自定義 webpack 的配置以滿足各類工程化需求。
在開始之前,你可能需要安裝 yarn。
$ yarn create react-app antd-demo
# or
$ npx create-react-app antd-demo
工具會自動初始化一個腳手架并安裝 React 項目的各種必要依賴,如果在過程中出現(xiàn)網(wǎng)絡問題,請嘗試配置代理或使用其他 npm registry。
然后我們進入項目并啟動。
$ cd antd-demo
$ yarn start
此時瀏覽器會訪問 http://localhost:3000/ ,看到 Welcome to React
的界面就算成功了。
這是 create-react-app 生成的默認目錄結構。
├── README.md
├── package.json
├── public
│ ├── favicon.ico
│ └── index.html
├── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── index.css
│ ├── index.js
│ └── logo.svg
└── yarn.lock
現(xiàn)在從 yarn 或 npm 安裝并引入 antd。
$ yarn add antd
修改 src/App.js
,引入 antd 的按鈕組件。
import React from 'react';
import { Button } from 'antd';
import './App.css';
const App = () => (
<div className="App">
<Button type="primary">Button</Button>
</div>
);
export default App;
修改 src/App.css
,在文件頂部引入 antd/dist/antd.css
。
@import '~antd/dist/antd.css';
.App {
text-align: center;
}
...
好了,現(xiàn)在你應該能看到頁面上已經(jīng)有了 antd 的藍色按鈕組件,接下來就可以繼續(xù)選用其他組件開發(fā)應用了。其他開發(fā)流程你可以參考 create-react-app 的官方文檔。
我們現(xiàn)在已經(jīng)把 antd 組件成功運行起來了,開始開發(fā)你的應用吧!
這個例子在實際開發(fā)中還有一些優(yōu)化的空間,比如無法進行主題配置,而且上面的例子加載了全部的 antd 組件的樣式(gzipped 后一共大約 60kb)。
此時我們需要對 create-react-app 的默認配置進行自定義,這里我們使用 react-app-rewired (一個對 create-react-app 進行自定義配置的社區(qū)解決方案)。
引入 react-app-rewired 并修改 package.json 里的啟動配置。由于新的 react-app-rewired@2.x 版本的關系,你還需要安裝 customize-cra。
$ yarn add react-app-rewired customize-cra
/* package.json */
"scripts": {
- "start": "react-scripts start",
+ "start": "react-app-rewired start",
- "build": "react-scripts build",
+ "build": "react-app-rewired build",
- "test": "react-scripts test",
+ "test": "react-app-rewired test",
}
然后在項目根目錄創(chuàng)建一個 config-overrides.js
用于修改默認配置。
module.exports = function override(config, env) {
// do stuff with the webpack config...
return config;
};
注意:antd 默認支持基于 ES module 的 tree shaking,js 代碼部分不使用這個插件也會有按需加載的效果。
babel-plugin-import 是一個用于按需加載組件代碼和樣式的 babel 插件(原理),現(xiàn)在我們嘗試安裝它并修改 config-overrides.js
文件。
$ yarn add babel-plugin-import
+ const { override, fixBabelImports } = require('customize-cra');
- module.exports = function override(config, env) {
- // do stuff with the webpack config...
- return config;
- };
+ module.exports = override(
+ fixBabelImports('import', {
+ libraryName: 'antd',
+ libraryDirectory: 'es',
+ style: 'css',
+ }),
+ );
然后移除前面在 src/App.css
里全量添加的 @import '~antd/dist/antd.css';
樣式代碼,并且按下面的格式引入模塊。
// src/App.js
import React, { Component } from 'react';
- import Button from 'antd/es/button';
+ import { Button } from 'antd';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<Button type="primary">Button</Button>
</div>
);
}
}
export default App;
最后重啟 yarn start
訪問頁面,antd 組件的 js 和 css 代碼都會按需加載,你在控制臺也不會看到這樣的警告信息。關于按需加載的原理和其他方式可以閱讀這里。
按照 配置主題 的要求,自定義主題需要用到 less 變量覆蓋功能。我們可以引入 customize-cra
中提供的 less 相關的函數(shù) addLessLoader 來幫助加載 less 樣式,同時修改 config-overrides.js
文件如下。
$ yarn add less less-loader
- const { override, fixBabelImports } = require('customize-cra');
+ const { override, fixBabelImports, addLessLoader } = require('customize-cra');
module.exports = override(
fixBabelImports('import', {
libraryName: 'antd',
libraryDirectory: 'es',
- style: 'css',
+ style: true,
}),
+ addLessLoader({
+ javascriptEnabled: true,
+ modifyVars: { '@primary-color': '#1DA57A' },
+ }),
);
這里利用了 less-loader 的 modifyVars
來進行主題配置,變量和其他配置方式可以參考 配置主題 文檔。
修改后重啟 yarn start
,如果看到一個綠色的按鈕就說明配置成功了。
你也可以使用 craco 和 craco-antd 來實現(xiàn)和 customize-cra 一樣的修改 create-react-app 配置的功能。
你可以使用 antd-dayjs-webpack-plugin 插件用 Day.js 替換 momentjs 來大幅減小打包大小。
$ yarn add antd-dayjs-webpack-plugin
const { override, addWebpackPlugin } = require('customize-cra');
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin');
module.exports = override(addWebpackPlugin(new AntdDayjsWebpackPlugin()));
你也可以使用 create-react-app 提供的 yarn run eject 命令將所有內建的配置暴露出來。不過這種配置方式需要你自行探索,不在本文討論范圍內。
以上是在 create-react-app 中使用 antd 的相關實踐,你也可以借鑒此文的做法在自己的 webpack 工作流中使用 antd,更多 webpack 配置可參考 atool-build。(例如加入 moment noParse 避免加載所有語言文件)
React 生態(tài)圈中還有很多優(yōu)秀的腳手架,使用它們并引入 antd 時,你可能會遇到一些問題,下面是一些著名腳手架使用 antd 的范例,包括本文的 create-react-app。
更多建議: