# reactui **Repository Path**: shenbo33/reactui ## Basic Information - **Project Name**: reactui - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-05 - **Last Updated**: 2021-06-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 如何快速构建React组件库 https://zhuanlan.zhihu.com/p/196758730 # Getting Started with Create React App This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). ## Available Scripts In the project directory, you can run: ### `yarn start` Runs the app in the development mode.\ Open [http://localhost:3000](http://localhost:3000) to view it in the browser. The page will reload if you make edits.\ You will also see any lint errors in the console. ### `yarn test` Launches the test runner in the interactive watch mode.\ See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. ### `yarn build` Builds the app for production to the `build` folder.\ It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes.\ Your app is ready to be deployed! See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. ### `yarn eject` **Note: this is a one-way operation. Once you `eject`, you can’t go back!** If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. ## Learn More You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). To learn React, check out the [React documentation](https://reactjs.org/). ### Code Splitting This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) ### Analyzing the Bundle Size This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) ### Making a Progressive Web App This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) ### Advanced Configuration This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) ### Deployment This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) ### `yarn build` fails to minify This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 首发于 NutUI 写文章 如何快速构建React组件库 如何快速构建React组件库 NutUI NutUI 轻量级移动端 Vue 组件库 21 人赞同了该文章 前言 俗话说:“麻雀虽小,五脏俱全”,搭建一个组件库,知之非难,行之不易,涉及到的技术方方面面,犹如海面风平浪静,实则暗礁险滩,处处惊险~ 目前团队内已经有较为成熟的 Vue 技术栈的 NutUI 组件库[1] 和 React 技术栈的 yep-react 组件库[2]。然而这些组件库大都从零开始搭建,包括 Webpack 的繁杂配置,Markdown 文件转 Vue 文件功能的开发,单元测试功能的开发、按需加载的 Babel 插件开发等等,完成整个组件库项目实属不易,也是一个浩大的工程。如果我们想快速搭建一个组件库,大可不必如此耗费精力,可以借助业内专业的相关库,经过拼装调试,快速实现一个组件库。 本篇文章就来给大家介绍一下使用 create-react-app 脚手架、docz 文档生成器、node-sass、结合 Netlify 部署项目的整个开发组件库的流程,本着包教包会,不会没有退费的原则,来一场手摸手式教学,话不多说,让我们进入正题: 首先看一下组件库的最终效果: 本文将从以下步骤介绍如何搭建一个 React 组件库: 一、构建本地开发环境 开发一个组件库的首要步骤就是调试本地 React 环境,我们直接使用 React 官方脚手架 create-react-app,可以省去从底层配置 Webpack+TypeScript+React 的摧残: 1、使用 create-react-app 初始化脚手架,并且安装 TypeScript npx create-react-app myapp --typescript 注意使用 node 为较高版本 >10.15.0 2、配置 eslint 进行格式化 由于安装最新的 create-react-app 结合 VScode 编辑器即可支持 eslit,但是需要在项目根目录中要添加 .env 这个配置文件,设置 EXTEND_ESLINT=true 这样才会启用 eslint 检测,注意要 重启 vscode 3、组件库系统文件结构 新建 styles 文件夹,包含了基本样式文件,结构如下: |-styles | |-variables.scss // 各种变量以及可配置设置 | |-mixins.scss // 全局 mixins | |-index.scss // 引入全部的 scss 文件,向外抛出样式入口 |-components | |-Button | |-button.scss // 组件的单独样式 | |-button.mdx // 组件的文档 | |-button.tsx // 组件的核心代码 | |-button.test.tsx // 组件的单元测试文件 | |-index.tsx // 组件对外入口 4、安装 node-sass 处理器 安装 node-sass 用来编译 SCSS 样式文件:npm i node-sass -D 这样最基本的 react 开发环境就完成了,可以开心的开发组件了。 二、组件库打包编译 本地调试完组件库之后,需要打包压缩编译代码,供其他用户使用,这里我们用的 TypeScript 编写的代码,所以使用 Typescript 来编译项目: 首先在每个组件中新建 index.tsx 文件: import Button from './button' export default Button 修改 index.tsx 文件,导入导出各个模块 export { default as Button } from './components/Button' 在根目录新建 tsconfig.build.json,对 .tsx 文件进行编译: { "compilerOptions": { "outDir": "dist",// 生成目录 "module": "esnext",// 格式 "target": "es5",// 版本 "declaration": true,// 为每一个 ts 文件生成 .d.ts 文件 "jsx": "react", "moduleResolution":"Node",// 规定寻找引入文件的路径为 node 标准 "allowSyntheticDefaultImports": true, }, "include": [// 要编译哪些文件 "src" ], "exclude": [// 排除不需要编译的文件 "src/**/*.test.tsx", "src/**/*.stories.tsx", "src/setupTests.ts", ] } 对于样式文件,使用 node-sass 编译 SCSS,抽取所有 SCSS 文件生成 CSS 文件: "script":{ "build-css": "node-sass ./src/styles/index.scss ./dist/index.css", } 并且修改 build 命令: "script":{ "clean": "rimraf ./dist",// 跨平台的兼容 "build": "npm run clean && npm run build-ts && npm run build-css", } 这样,执行 npm run build 之后,就可以生成对应的组件 JS 和 CSS 文件,为后面使用者按需加载和部署到 npm 上提供准备。 三、本地调试组件库 本地完成组件库的开发之后,在发布到 npm 前,需要先在本地调试,避免带着问题上传到 npm 上。这时就需要使用 npm link 出马了。 什么是 npm link 在本地开发 npm 模块的时候,我们可以使用 npm link 命令,将 npm 模块链接到对应的运行项目中去,方便地对模块进行调试和测试。 使用方法 假设组件库是 reactui 文件夹,要在本地的 demo 项目中使用组件。则在组件库中(要被 link 的地方)执行 npm link,则生成从本机的 node_modules/reactui 到 组件库的路径 / reactui 中的映射关系。 然后在要使用组件库的文件夹 demo 中执行 npm link reactui 则生成以下对应链条: 在要使用组件的文件夹 demo 中 -[映射到]—> 本机的 node_modules/reactui —[映射到]-> 开发组件库 reactui 的文件夹 /reactui 需要修改组件库的 package.json 文件来设置入口: { "name": "reactui", "main": "dist/index.js", "module": "dist/index.js", "types": "dist/index.d.ts", } 然后在要使用组件的 demo 项目的依赖中添加: "dependencies":{ "reactui":"0.0.1" } 注意,此时并不用安装依赖,之所以写上该依赖,是为了方便在项目中使用的时候可以有代码提示功能。 然后在 demo 项目中使用: import { Button } from 'reactui' 在 index.tsx 中引入 CSS 文件 import 'reactui/build/index.css' 正当以为大功告成的时候,下面这个报错犹如一盆冷水从天而降: 经过各种问题排查,在 react 官方网站[3] 上查到以下说法: Do not call Hooks in class components. Do not call in event handlers. Do not call Hooks inside functions passed to useMemo, useReducer, or useEffect. 说的很明白: 原因 1: React 和 React DOM 的版本不一样的问题 原因 2: 可能打破了 Hooks 的规则 原因 3: 在同一个项目中使用了多个版本的 React 官网很贴心,给出了解决方法: This problem can also come up when you use npm link or an equivalent. In that case, your bundler might “see” two Reacts — one in application folder and one in your library folder. Assuming myapp and mylib are sibling folders, one possible fix is to run npm link ../myapp/node_modules/react from mylib. This should make the library use the application’s React copy. 核心思想在组件库中使用 npm link 方式,引到 demo 项目中的 react; 所以在组件库中执行: npm link ../demo/node_modules/react 具体步骤如下: 在代码库 reactui 中执行 npm link 在代码库 reactui 中执行 npm link ../../demo/node_modules/react 在项目 demo 中执行 npm link reactui 如此可以解决上面 react 冲突问题;于是可以在本地一边快乐的调试组件库,一边快乐的在使用组件的项目中看到最终效果了。 四、组件库发布到 npm 该过程一定要注意使用的是 npm 源!![非常重要] 首先确定自己是否已经登录了 npm: npm adduser // 填入用户名;密码;email npm whoami // 查看当前登录名 修改组件库的 package.json ,注意 files 配置;以及 dependencies 文件的化简: react 依赖原本是要放在 dependencies 中的,但是可能会和用户安装的 react 版本冲突,所以放在了 devDependencies 中,但是这样话用户如果没有安装 react 则无法使用组件库,所以要在 peerDependencies 中定义前置依赖 peerDependencies,告诉用户 react 和 react-dom 是必要的前置依赖: "main": "dist/index.js", "module": "dist/index.js", "types": "dist/index.d.ts", "files": [ // 把哪些文件上传到 npm "dist" ], "dependencies": { // 执行 npm i 的时候会安装这些依赖到 node_modules 中 "axios": "^0.19.1",// 发送请求 "classnames": "^2.2.6",// "react-transition-group": "^4.3.0" }, "peerDependencies": { // 重要!!,提醒使用者,组件库的核心依赖,必须先安装这些依赖才能使用 "react": ">=16.8.0", // 在 16.8 之后 才引入了 hooks "react-dom": ">=16.8.0" } 好了,整个组件库经过上述过程,基本上各个功能已经有了,提及一句:由于组件库使用的是 create-react-app 脚手架,最新的版本已经集成了单元测试功能。还有配置 husky 等规范代码提交,在这里不在做赘述,读者可以自行配置。 五、生成说明文档 目前生成说明文档较好的工具有 storybook[4]、docz[5] 等工具,两者都是很优秀的文档生成工具,但是尺有所短,寸有所长,经过认真调研比较,最终选择了 docz。 |工具名称|区别一|区别二| |--|--|--| |storybook|使用特有的API开发文档说明,可以引入markdown文件|生成文档的界面带有storybook的痕迹较多一些| |docz|完美的结合了react和markdown语法开发文档|生成的文档界面是常规的文档界面| 1、确定选型 1)storybook 的常用编译文档规范相对 docz 而言,略有繁琐 storybook 的编译文档规范如下所示: //省略 import 引入的代码 storiesOf('Buttons', module) .addDecorator(storyFn =>