Skip to content

Commit 674a849

Browse files
author
zhaozhiwen
committed
docs: 文档更新
1 parent 8a3a380 commit 674a849

File tree

5 files changed

+1473
-0
lines changed

5 files changed

+1473
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# React 组件库搭建指南
2+
3+
> build your own React UI library! 🙈
4+
5+
本系列文章主要是开发组件库[dora-ui](https://github.com/worldzhao/dora-ui)过程中的一些经验总结,大致包含以下内容:
6+
7+
- prepare: 组件库前期开发准备工作。`eslint`/`commit lint`/`typescript`等等;
8+
- dev: 使用[docz](https://github.com/doczjs/docz)进行开发调试以及文档编写;
9+
- build: ~~`umd`~~/`cjs`/`esm`、types、polyfill 以及按需加载;
10+
- test: 单元测试;
11+
- deploy: 使用[now](https://zeit.co/home)部署文档站点;
12+
- publish: 发布组件库至`npm`
13+
14+
## 已更新章节:
15+
16+
- [React 组件库搭建指南-准备工作](https://juejin.im/post/5df9a39f6fb9a0165b4cdb26)
17+
- [React 组件库搭建指南-开发调试](https://juejin.im/post/5dfb09b1e51d4558096d5f94)
18+
- [React 组件库搭建指南-打包输出](https://juejin.im/post/5e030b926fb9a0162c487c7b)
19+
- [React 组件库搭建指南-单元测试](https://juejin.im/post/5e23e4035188252c6c478665)
20+
21+
文档所有代码在本仓库中均有对应分支,`markdown`源文件在`articles`文件夹下。
22+
23+
## 待更新章节:
24+
25+
- React 组件库搭建指南-文档站点部署与 npm 包发布
26+
27+
## 其他
28+
29+
如果文章内容有错误的地方,烦请指正,互相交流,共同学习。
30+
31+
如果本仓库帮助到了你,请给我一颗 ✨,谢谢。

articles/chapter-1.md

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
2+
凡事预则立不预则废。
3+
4+
<!--more -->
5+
6+
- [React 组件库搭建指南-准备工作](https://juejin.im/post/5df9a39f6fb9a0165b4cdb26)
7+
- [React 组件库搭建指南-开发调试](https://juejin.im/post/5dfb09b1e51d4558096d5f94)
8+
- [React 组件库搭建指南-打包输出](https://juejin.im/post/5e030b926fb9a0162c487c7b)
9+
- [React 组件库搭建指南-单元测试](https://juejin.im/post/5e23e4035188252c6c478665)
10+
11+
## 概览
12+
13+
本系列文章主要是开发组件库[dora-ui](https://github.com/worldzhao/dora-ui)过程中的一些经验总结,大致包含以下内容:
14+
15+
- prepare: 组件库前期开发准备工作。`eslint`/`commit lint`/`typescript`等等;
16+
- dev: 使用[docz](https://github.com/doczjs/docz)进行开发调试以及文档编写;
17+
- build: ~~`umd`~~/`cjs`/`esm`、types、polyfill 以及按需加载;
18+
- deploy: 使用[now](https://zeit.co/home)部署文档站点;
19+
- publish: 发布组件库至`npm`
20+
- others: 组件测试
21+
22+
注意:`.gitignore``.eslintignore`以及`.editorconfig`等更多文件可在[chapter-1](https://github.com/worldzhao/react-ui-library-tutorial/tree/chapter-1/before-start)章节代码中获取。
23+
24+
## 初始化项目
25+
26+
新建一个`happy-ui`文件夹,并初始化。
27+
28+
```bash
29+
mkdir happy-ui
30+
31+
cd happy-ui
32+
33+
npm init --y
34+
35+
mkdir components && cd components && touch index.ts # 新建源码文件夹以及入口文件
36+
37+
```
38+
39+
## 代码规范
40+
41+
此处直接使用[@umijs/fabric](https://github.com/umijs/fabric)的配置。
42+
43+
```bash
44+
yarn add @umijs/fabric --dev
45+
46+
yarn add prettier --dev # 因为@umijs/fabric没有将prettier作为依赖 所以我们需要手动安装
47+
```
48+
49+
**.eslintrc.js**
50+
51+
```js
52+
module.exports = {
53+
extends: [require.resolve('@umijs/fabric/dist/eslint')],
54+
};
55+
```
56+
57+
**.prettierrc.js**
58+
59+
```js
60+
const fabric = require('@umijs/fabric');
61+
62+
module.exports = {
63+
...fabric.prettier,
64+
};
65+
```
66+
67+
**.stylelintrc.js**
68+
69+
```js
70+
module.exports = {
71+
extends: [require.resolve('@umijs/fabric/dist/stylelint')],
72+
};
73+
```
74+
75+
想自行配置的同学可以参考以下文章:
76+
77+
- [Linting Your React+Typescript Project with ESLint and Prettier!](https://medium.com/@dors718/linting-your-react-typescript-project-with-eslint-and-prettier-2423170c3d42)
78+
- [使用 ESLint+Prettier 规范 React+Typescript 项目 ](https://zhuanlan.zhihu.com/p/62401626)
79+
80+
## Commit Lint
81+
82+
进行`pre-commit`代码规范检测。
83+
84+
```bash
85+
yarn add husky lint-staged --dev
86+
```
87+
88+
**package.json**
89+
90+
```json
91+
"lint-staged": {
92+
"components/**/*.ts?(x)": [
93+
"prettier --write",
94+
"eslint --fix",
95+
"git add"
96+
],
97+
"components/**/*.less": [
98+
"stylelint --syntax less --fix",
99+
"git add"
100+
]
101+
},
102+
"husky": {
103+
"hooks": {
104+
"pre-commit": "lint-staged"
105+
}
106+
}
107+
```
108+
109+
进行 Commit Message 检测。
110+
111+
```bash
112+
yarn add @commitlint/cli @commitlint/config-conventional commitizen cz-conventional-changelog --dev
113+
```
114+
115+
新增`.commitlintrc.js`写入以下内容
116+
117+
```js
118+
module.exports = { extends: ['@commitlint/config-conventional'] };
119+
```
120+
121+
package.json 写入以下内容:
122+
123+
```json
124+
// ...
125+
"scripts": {
126+
"commit": "git-cz",
127+
}
128+
// ...
129+
"husky": {
130+
"hooks": {
131+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
132+
"pre-commit": "lint-staged"
133+
}
134+
},
135+
"config": {
136+
"commitizen": {
137+
"path": "cz-conventional-changelog"
138+
}
139+
}
140+
```
141+
142+
后续使用 `yarn commit` 替代 `git commit`生成规范的 Commit Message,当然为了效率你可以选择手写,但是要符合规范。
143+
144+
## TypeScript
145+
146+
```bash
147+
yarn add typescript --dev
148+
```
149+
150+
新建`tsconfig.json`并写入以下内容
151+
152+
```json
153+
{
154+
"compilerOptions": {
155+
"allowJs": false,
156+
"target": "esnext",
157+
"module": "commonjs",
158+
"jsx": "react",
159+
"declaration": true,
160+
"outDir": "types",
161+
"strict": true,
162+
"moduleResolution": "node",
163+
"allowSyntheticDefaultImports": true,
164+
"esModuleInterop": true
165+
},
166+
"include": ["components"]
167+
}
168+
```
169+
170+
## 测试
171+
172+
`components`文件夹下新建`alert`文件夹,目录结构如下:
173+
174+
```
175+
alert
176+
├── alert.tsx # 源文件
177+
├── index.ts # 入口文件
178+
├── interface.ts # 类型声明文件
179+
└── style
180+
├── index.less # 样式文件
181+
└── index.ts # 样式文件里为什么存在一个index.ts - 按需加载样式 管理样式依赖 后面章节会提到
182+
```
183+
184+
安装`React`相关依赖:
185+
186+
```bash
187+
yarn add react react-dom @types/react @types/react-dom --dev # 开发时依赖,宿主环境一定存在
188+
189+
yarn add prop-types # 运行时依赖,宿主环境可能不存在 安装本组件库时一起安装
190+
```
191+
192+
> 此处依旧安装了`prop-types`这个库,因为无法保证宿主环境也使用`typescript`,从而能够进行静态检查,故使用`prop-types`保证`javascript`用户也能得到友好的运行时报错信息。
193+
194+
**components/alert/interface.ts**
195+
196+
```js
197+
export type Kind = 'info' | 'positive' | 'negative' | 'warning';
198+
export type KindMap = Record<Kind, string>;
199+
200+
export interface AlertProps {
201+
/**
202+
* Set this to change alert kind
203+
* @default info
204+
*/
205+
kind?: 'info' | 'positive' | 'negative' | 'warning';
206+
}
207+
```
208+
209+
**components/alert/alter.tsx**
210+
211+
```jsx
212+
import React from 'react';
213+
import t from 'prop-types';
214+
215+
import { AlertProps, KindMap } from './interface';
216+
217+
const prefixCls = 'happy-alert';
218+
219+
const kinds: KindMap = {
220+
info: '#5352ED',
221+
positive: '#2ED573',
222+
negative: '#FF4757',
223+
warning: '#FFA502',
224+
};
225+
226+
const Alert: React.FC<AlertProps> = ({ children, kind = 'info', ...rest }) => (
227+
<div
228+
className={prefixCls}
229+
style={{
230+
background: kinds[kind],
231+
}}
232+
{...rest}
233+
>
234+
{children}
235+
</div>
236+
);
237+
238+
Alert.propTypes = {
239+
kind: t.oneOf(['info', 'positive', 'negative', 'warning']),
240+
};
241+
242+
export default Alert;
243+
```
244+
245+
**components/alert/index.ts**
246+
247+
```js
248+
import Alert from './alert';
249+
250+
export default Alert;
251+
252+
export * from './interface';
253+
```
254+
255+
**components/alert/style/index.less**
256+
257+
```less
258+
@popupPrefix: happy-alert;
259+
260+
.@{popupPrefix} {
261+
padding: 20px;
262+
background: white;
263+
border-radius: 3px;
264+
color: white;
265+
}
266+
```
267+
268+
**components/alert/style/index.ts**
269+
270+
```js
271+
import './index.less';
272+
```
273+
274+
**components/index.ts**
275+
276+
```js
277+
export { default as Alert } from './alert';
278+
```
279+
280+
> 此处组件参考的`docz`项目`typescript`以及`less`示例。
281+
282+
git 一把梭,可以看到控制台已经进行钩子检测了。
283+
284+
```bash
285+
git add .
286+
287+
yarn commit # 或 git commit -m'feat: chapter-1 准备工作'
288+
289+
git push
290+
```
291+
292+
准备工作结束,欢迎指点交流。
293+
294+
To be Continued...

0 commit comments

Comments
 (0)