2016-12-22 14:24:50 +08:00
|
|
|
|
---
|
|
|
|
|
order: 4
|
|
|
|
|
title: 在 create-react-app 中使用
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
[create-react-app](https://github.com/facebookincubator/create-react-app) 是业界最优秀的 React 应用开发工具之一,本文会尝试在 create-react-app 创建的工程中使用 antd 组件,并自定义 webpack 的配置以满足各类工程化需求。
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 安装和初始化
|
|
|
|
|
|
2017-01-09 13:50:49 +08:00
|
|
|
|
我们需要在命令行中安装 create-react-app 工具,你可能还需要安装 [yarn](https://github.com/yarnpkg/yarn/)。
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2017-01-09 13:50:49 +08:00
|
|
|
|
$ npm install -g create-react-app yarn
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
然后新建一个项目。
|
|
|
|
|
|
|
|
|
|
```bash
|
2017-01-09 13:50:49 +08:00
|
|
|
|
$ create-react-app antd-demo
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
工具会自动初始化一个脚手架并安装 React 项目的各种必要依赖,如果在过程中出现网络问题,请尝试配置代理或使用其他 npm registry。
|
|
|
|
|
|
|
|
|
|
然后我们进入项目并启动。
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ cd antd-demo
|
2017-01-09 13:50:49 +08:00
|
|
|
|
$ yarn start
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
此时浏览器会访问 http://localhost:3000/ ,看到 `Welcome to React` 的界面就算成功了。
|
|
|
|
|
|
|
|
|
|
## 引入 antd
|
|
|
|
|
|
|
|
|
|
这是 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
|
|
|
|
|
```
|
|
|
|
|
|
2017-01-09 13:50:49 +08:00
|
|
|
|
现在从 yarn 或 npm 安装并引入 antd。
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2017-05-28 17:03:34 +08:00
|
|
|
|
$ yarn add antd
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
修改 `src/App.js`,引入 antd 的按钮组件。
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
import React, { Component } from 'react';
|
2017-06-06 13:40:19 +08:00
|
|
|
|
import Button from 'antd/lib/button';
|
2016-12-22 14:24:50 +08:00
|
|
|
|
import './App.css';
|
|
|
|
|
|
|
|
|
|
class App extends Component {
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div className="App">
|
|
|
|
|
<Button type="primary">Button</Button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
修改 `src/App.css`,在文件顶部引入 `antd/dist/antd.css`。
|
|
|
|
|
|
|
|
|
|
```css
|
|
|
|
|
@import '~antd/dist/antd.css';
|
|
|
|
|
|
|
|
|
|
.App {
|
|
|
|
|
text-align: center;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
好了,现在你应该能看到页面上已经有了 antd 的蓝色按钮组件,接下来就可以继续选用其他组件开发应用了。其他开发流程你可以参考 create-react-app 的[官方文档](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md)。
|
|
|
|
|
|
|
|
|
|
## 高级配置
|
|
|
|
|
|
2017-06-06 13:40:19 +08:00
|
|
|
|
我们现在已经把组件成功运行起来了,但是在实际开发过程中还有很多问题,例如上面的例子实际上加载了全部的 antd 组件的样式(对前端性能是个隐患)。
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
2017-06-06 13:40:19 +08:00
|
|
|
|
此时我们可能需要对 create-react-app 的默认配置进行自定义。可以使用 `eject` 命令将所有内建的配置暴露出来。
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2017-01-09 13:50:49 +08:00
|
|
|
|
$ yarn run eject
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
2017-06-06 13:40:19 +08:00
|
|
|
|
### 使用 babel-plugin-import
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
2017-02-09 17:53:34 +08:00
|
|
|
|
[babel-plugin-import](https://github.com/ant-design/babel-plugin-import) 是一个用于按需加载组件代码和样式的 babel 插件([原理](/docs/react/getting-started#按需加载)),现在我们尝试安装它并修改 `config/webpack.config.dev.js` 文件。
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2017-05-28 17:03:34 +08:00
|
|
|
|
$ yarn add babel-plugin-import --dev
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```diff
|
|
|
|
|
// Process JS with Babel.
|
|
|
|
|
{
|
|
|
|
|
test: /\.(js|jsx)$/,
|
|
|
|
|
include: paths.appSrc,
|
2017-05-28 17:03:34 +08:00
|
|
|
|
loader: require.resolve('babel-loader'),
|
|
|
|
|
options: {
|
2016-12-22 14:24:50 +08:00
|
|
|
|
+ plugins: [
|
2017-05-19 18:28:19 +08:00
|
|
|
|
+ ['import', { libraryName: 'antd', style: 'css' }],
|
2016-12-22 14:24:50 +08:00
|
|
|
|
+ ],
|
|
|
|
|
// This is a feature of `babel-loader` for webpack (not Babel itself).
|
|
|
|
|
// It enables caching results in ./node_modules/.cache/babel-loader/
|
|
|
|
|
// directory for faster rebuilds.
|
|
|
|
|
cacheDirectory: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-27 21:07:16 +08:00
|
|
|
|
> 注意,由于 create-react-app eject 之后的配置中没有 `.babelrc` 文件,所以需要把配置放到 `webpack.config.js` 或 `package.json` 的 `babel` 属性中。
|
|
|
|
|
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
2017-06-06 13:40:19 +08:00
|
|
|
|
然后移除前面在 `src/App.css` 里全量添加的 `@import '~antd/dist/antd.css';` 样式代码,并且按下面的格式引入模块。
|
|
|
|
|
|
|
|
|
|
```diff
|
|
|
|
|
// scr/App.js
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
- import Button from 'antd/lib/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 代码都会按需加载,你在控制台也不会看到这样的[警告信息](https://zos.alipayobjects.com/rmsportal/vgcHJRVZFmPjAawwVoXK.png)。关于按需加载的原理和其他方式可以阅读[这里](/docs/react/getting-started#Import-on-Demand).
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
|
|
|
|
### 自定义主题
|
|
|
|
|
|
2016-12-26 11:15:36 +08:00
|
|
|
|
按照 [配置主题](/docs/react/customize-theme) 的要求,自定义主题需要用到 less 变量覆盖功能,因此首先我们需要引入 [less-loader](https://github.com/webpack/less-loader) 来加载 less 样式,同时修改 `config/webpack.config.dev.js` 文件。
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2017-05-28 17:03:34 +08:00
|
|
|
|
$ yarn add less less-loader --dev
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```diff
|
|
|
|
|
{
|
|
|
|
|
exclude: [
|
|
|
|
|
/\.html$/,
|
|
|
|
|
/\.(js|jsx)$/,
|
|
|
|
|
/\.css$/,
|
2017-05-28 17:03:34 +08:00
|
|
|
|
+ /\.less$/,
|
2016-12-22 14:24:50 +08:00
|
|
|
|
/\.json$/,
|
2017-05-28 17:03:34 +08:00
|
|
|
|
/\.bmp$/,
|
|
|
|
|
/\.gif$/,
|
|
|
|
|
/\.jpe?g$/,
|
|
|
|
|
/\.png$/,
|
2016-12-22 14:24:50 +08:00
|
|
|
|
],
|
2017-05-28 17:03:34 +08:00
|
|
|
|
loader: require.resolve('file-loader'),
|
|
|
|
|
options: {
|
|
|
|
|
name: 'static/media/[name].[hash:8].[ext]',
|
|
|
|
|
},
|
|
|
|
|
}
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
// Process JS with Babel.
|
|
|
|
|
{
|
|
|
|
|
test: /\.(js|jsx)$/,
|
|
|
|
|
include: paths.appSrc,
|
|
|
|
|
loader: 'babel',
|
2017-05-28 17:03:34 +08:00
|
|
|
|
options: {
|
2016-12-22 14:24:50 +08:00
|
|
|
|
plugins: [
|
2017-05-19 18:28:19 +08:00
|
|
|
|
- ['import', [{ libraryName: 'antd', style: 'css' }]],
|
2017-05-28 17:03:34 +08:00
|
|
|
|
+ ['import', [{ libraryName: 'antd', style: true }]], // import less
|
2016-12-22 14:24:50 +08:00
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
2017-05-28 17:03:34 +08:00
|
|
|
|
+ // Parse less files and modify variables
|
|
|
|
|
+ {
|
|
|
|
|
+ test: /\.less$/,
|
|
|
|
|
+ use: [
|
|
|
|
|
+ require.resolve('style-loader'),
|
|
|
|
|
+ require.resolve('css-loader'),
|
|
|
|
|
+ {
|
|
|
|
|
+ loader: require.resolve('postcss-loader'),
|
|
|
|
|
+ options: {
|
|
|
|
|
+ ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
|
|
|
|
|
+ plugins: () => [
|
|
|
|
|
+ require('postcss-flexbugs-fixes'),
|
|
|
|
|
+ autoprefixer({
|
|
|
|
|
+ browsers: [
|
|
|
|
|
+ '>1%',
|
|
|
|
|
+ 'last 4 versions',
|
|
|
|
|
+ 'Firefox ESR',
|
|
|
|
|
+ 'not ie < 9', // React doesn't support IE8 anyway
|
|
|
|
|
+ ],
|
|
|
|
|
+ flexbox: 'no-2009',
|
|
|
|
|
+ }),
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ loader: require.resolve('less-loader'),
|
|
|
|
|
+ options: {
|
|
|
|
|
+ modifyVars: { "@primary-color": "#1DA57A" },
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
2017-05-19 18:28:19 +08:00
|
|
|
|
],
|
2016-12-22 14:24:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这里利用了 [less-loader](https://github.com/webpack/less-loader#less-options) 的 `modifyVars` 来进行主题配置,
|
|
|
|
|
变量和其他配置方式可以参考 [配置主题](/docs/react/customize-theme) 文档。
|
|
|
|
|
|
2017-01-09 13:50:49 +08:00
|
|
|
|
修改后重启 `yarn start`,如果看到一个绿色的按钮就说明配置成功了。
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
2017-03-13 10:54:25 +08:00
|
|
|
|
> 注意,上述示例只修改了 `webpack.config.dev.js`,如果需要在生产环境生效,你需要同步修改 `webpack.config.prod.js`。
|
|
|
|
|
|
2017-03-10 16:37:39 +08:00
|
|
|
|
## 源码和其他脚手架
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
2016-12-30 14:13:43 +08:00
|
|
|
|
以上是在 create-react-app 中使用 antd 的相关实践,你也可以借鉴此文的做法在自己的 webpack 工作流中使用 antd,更多 webpack 配置可参考 [atool-build](https://github.com/ant-tool/atool-build/blob/master/src/getWebpackCommonConfig.js)。(例如加入 [moment noParse](https://github.com/ant-tool/atool-build/blob/e4bd2959689b6a95cb5c1c854a5db8c98676bdb3/src/getWebpackCommonConfig.js#L90) 避免加载所有语言文件)
|
2016-12-22 14:24:50 +08:00
|
|
|
|
|
2017-03-10 20:34:30 +08:00
|
|
|
|
React 生态圈中还有很多优秀的脚手架,使用它们并引入 antd 时,你可能会遇到一些问题,下面是一些著名脚手架使用 antd 的范例,包括本文的 create-react-app。
|
2017-03-10 16:37:39 +08:00
|
|
|
|
|
|
|
|
|
- [react-boilerplate/react-boilerplate](https://github.com/ant-design/react-boilerplate)
|
|
|
|
|
- [kriasoft/react-starter-kit](https://github.com/ant-design/react-starter-kit)
|
|
|
|
|
- [create-react-app-antd](https://github.com/ant-design/create-react-app-antd)
|