当前位置:   article > 正文

可能是最详细的React组件库搭建总结

react组件库搭建

可能是最详细的React组件库搭建总结

概览

本文包含以下内容:

  • prepare: 组件库前期开发准备工作。eslint/commit lint/typescript等等;
  • dev: 使用docz进行开发调试以及文档编写;
  • build:umd/cjs/esm、types、polyfill 以及按需加载;
  • test: 组件测试;
  • release: 组件库发布流程;
  • deploy: 使用now部署文档站点,待补充;
  • other: 使用plop.js快速创建组件模板。

准备工作

初始化项目

新建一个happy-ui文件夹,并初始化。

  1. mkdir happy-uicd happy-ui
  2. npm init --y
  3. mkdir components && cd components && touch index.ts # 新建源码文件夹以及入口文件

代码规范

此处直接使用@umijs/fabric的配置。

  1. yarn add @umijs/fabric --dev
  2. yarn add prettier --dev# 因为@umijs/fabric没有将prettier作为依赖 所以我们需要手动安装

.eslintrc.js

  1. module.exports = {
  2. extends: [require.resolve('@umijs/fabric/dist/eslint')],
  3. };

.prettierrc.js

  1. const fabric = require('@umijs/fabric');
  2. module.exports = {
  3. ...fabric.prettier,
  4. };

.stylelintrc.js

  1. module.exports = {
  2. extends: [require.resolve('@umijs/fabric/dist/stylelint')],
  3. };

想自行配置的同学可以参考以下文章:

  • Linting Your React+Typescript Project with ESLint and Prettier!
  • 使用 ESLint+Prettier 规范 React+Typescript 项目

Commit Lint

进行pre-commit代码规范检测。

yarn add husky lint-staged --dev

package.json

  1. "lint-staged": {
  2. "components/**/*.ts?(x)": [
  3. "prettier --write",
  4. "eslint --fix",
  5. "git add"
  6. ],
  7. "components/**/*.less": [
  8. "stylelint --syntax less --fix",
  9. "git add"
  10. ]
  11. },
  12. "husky": {
  13. "hooks": {
  14. "pre-commit": "lint-staged"
  15. }
  16. }

进行 Commit Message 检测。

yarn add @commitlint/cli @commitlint/config-conventional commitizen cz-conventional-changelog --dev

新增.commitlintrc.js写入以下内容

module.exports = { extends: ['@commitlint/config-conventional'] };

package.json 写入以下内容:

  1. // ...
  2. "scripts": {
  3. "commit": "git-cz",
  4. }
  5. // ...
  6. "husky": {
  7. "hooks": {
  8. "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
  9. "pre-commit": "lint-staged"
  10. }
  11. },
  12. "config": {
  13. "commitizen": {
  14. "path": "cz-conventional-changelog"
  15. }
  16. }

后续使用 yarn commit 替代 git commit生成规范的 Commit Message,当然为了效率你可以选择手写,但是要符合规范。

TypeScript

yarn add typescript --dev

新建tsconfig.json并写入以下内容

  1. {
  2. "compilerOptions": {
  3. "baseUrl": "./",
  4. "target": "esnext",
  5. "module": "commonjs",
  6. "jsx": "react",
  7. "declaration": true,
  8. "declarationDir": "lib",
  9. "strict": true,
  10. "moduleResolution": "node",
  11. "allowSyntheticDefaultImports": true,
  12. "esModuleInterop": true,
  13. "resolveJsonModule": true
  14. },
  15. "include": ["components", "global.d.ts"],
  16. "exclude": ["node_modules"]
  17. }

测试

components文件夹下新建alert文件夹,目录结构如下:

  1. alert
  2. ├── alert.tsx # 源文件
  3. ├── index.ts # 入口文件
  4. ├── interface.ts # 类型声明文件
  5. └── style
  6. ├── index.less # 样式文件
  7. └── index.ts # 样式文件里为什么存在一个index.ts - 按需加载样式 管理样式依赖 后面章节会提到

安装React相关依赖:

  1. yarn add react react-dom @types/react @types/react-dom --dev# 开发时依赖,宿主环境一定存在
  2. yarn add prop-types
  3. # 运行时依赖,宿主环境可能不存在 安装本组件库时一起安装

此处依旧安装了prop-types这个库,因为无法保证宿主环境也使用typescript,从而能够进行静态检查,故使用prop-types保证javascript用户也能得到友好的运行时报错信息。

components/alert/interface.ts

  1. export type Kind = 'info' | 'positive' | 'negative' | 'warning';
  2. export type KindMap = Record<Kind, string>;
  3. export interface AlertProps {
  4. /**
  5. * Set this to change alert kind
  6. * @default info
  7. */
  8. kind?: 'info' | 'positive' | 'negative' | 'warning';
  9. }

components/alert/alter.tsx

  1. import React from 'react';
  2. import t from 'prop-types';
  3. import { AlertProps, KindMap } from './interface';
  4. const prefixCls = 'happy-alert';
  5. const kinds: KindMap = {
  6. info: '#5352ED',
  7. positive: '#2ED573',
  8. negative: '#FF4757',
  9. warning: '#FFA502',
  10. };
  11. const Alert: React.FC<AlertProps> = ({ children, kind = 'info', ...rest }) => (
  12. <div
  13. className={prefixCls}
  14. style={{
  15. background: kinds[kind],
  16. }}
  17. {...rest}
  18. >
  19. {children}
  20. </div>
  21. );
  22. Alert.propTypes = {
  23. kind: t.oneOf(['info', 'positive', 'negative', 'warning']),
  24. };
  25. export default Alert;

components/alert/index.ts

  1. import Alert from './alert';
  2. export default Alert;
  3. export * from './interface';

components/alert/style/index.less

  1. @popupPrefix: happy-alert;
  2. .@{popupPrefix} {
  3. padding: 20px;
  4. background: white;
  5. border-radius: 3px;
  6. color: white;
  7. }

components/alert/style/index.ts

import './index.less';

components/index.ts

export { default as Alert } from './alert';

此处组件参考的docz项目typescript以及less示例。

git 一把梭,可以看到控制台已经进行钩子检测了。

  1. git add .
  2. yarn commit# 或 git commit -m'feat: chapter-1 准备工作'
  3. git push

准备工作完成。代码可以在仓库的chapter-1分支获取,若存在与本文内容不符的地方,以master分支以及文章为准。

开发与调试

本节解决开发组件时的预览以及调试问题,顺路解决文档编写。

此处选择docz来辅助预览调试。

docz基于MDX(Markdown + JSX),可以在 Markdown 中引入 React 组件,使得一边编写文档,一边预览调试成为了可能。而且得益于 React 组件生态,我们可以像编写应用一般编写文档,不仅仅是枯燥的文字。docz 也内置了一些组件,比如<Playground>

安装 docz 以及自定义配置

  1. yarn add docz --dev
  2. yarn add rimraf --dev # 清空目录的一个辅助库

增加 npm scriptspackage.json

  1. "scripts": {
  2. "dev": "docz dev", // 启动本地开发环境
  3. "start": "npm run dev", // dev命令别名
  4. "build:doc": "rimraf doc-site && docz build", // 后续会配置打包出来的文件目录名为doc-site,故每次build前删除
  5. "preview:doc": "docz serve" // 预览文档站点
  6. },

注意:本节所有操作都是针对站点应用。打包指代文档站点打包,而非组件库。

新建doczrc.js配置文件,并写入以下内容:

doczrc.js

  1. export default {
  2. files: './components/**/*.{md,markdown,mdx}', // 识别的文件后缀
  3. dest: 'doc-site', // 打包出来的文件目录名
  4. title: 'happy-ui', // 站点标题
  5. typescript: true, // 组件源文件是通过typescript开发,需要打开此选项
  6. };

由于使用了less作为样式预处理器,故需要安装 less 插件。

yarn add less gatsby-plugin-less --dev

新建gatsby-config.js,并写入以下内容:

gatsby-config.js

  1. module.exports = {
  2. plugins: ['gatsby-theme-docz', 'gatsby-plugin-less'],
  3. };

编写文档

新建components/alert/index.mdx,并写入以下内容:

  1. ---
  2. name: Alert 警告提示
  3. route: /Alert
  4. menu: 组件
  5. ---
  6. import { Playground } from 'docz'; import Alert from './alert'; // 引入组件 import './style'; // 引入组件样式
  7. # Alert 警告提示
  8. 警告提示,展现需要关注的信息。
  9. ## 代码演示
  10. ### 基本用法
  11. <Playground>
  12. <Alert kind="warning">这是一条警告提示</Alert>
  13. </Playground>
  14. ## API
  15. | 属性 | 说明 | 类型 | 默认值 |
  16. | ---- | -------- | -------------------------------------------- | ------ |
  17. | kind | 警告类型 | 'info'/'positive'/'negative'/'warning'非必填 | 'info' |

执行脚本命令:

yarn start # or yarn dev

可以在localhost:3000看到如下页面 :

文档站点文档站点

现在可以在index.mdx中愉快地进行文档编写和调试了!

倘若本文到了这里就结束(其实也可以结束了(_^▽^_)),那我只是官方文档的翻译复读机罢了,有兴趣的同学可以继续向下看。

优化文档编写

如果代码演示部分的demo较多(比如基本用法、高级用法以及各种用法等等),在组件复杂的情况下(毕竟<Alert/>着实太简单了),会导致文档很长难以维护,你到底是在写文档呢还是在写代码呢?

那就抽离吧。

components/alert/文件夹下新建demo文件夹,存放我们在编写文档时需要引用的 demo

components/alert/demo/1-demo-basic.tsx

  1. import React from 'react';
  2. import Alert from '../alert';
  3. import '../style';
  4. export default () => <Alert kind="warning"></Alert>;

components/alert/index.mdx

  1. - import Alert from './alert'; // 引入组件
  2. - import './style'; // 引入组件样式
  3. + import BasicDemo from './demo/1-demo-basic';
  4. ...
  5. <Playground>
  6. - <Alert kind="warning">这是一条警告提示</Alert>
  7. + <BasicDemo />
  8. </Playground>

这样我们就将 demo 与文档进行了分隔。预览如下:

文档重构文档重构

等等,下面显示的是<BasicDemo />,而非demo源码。

<Playground />组件暂时无法支持上述形式的展示:自定义下方展示的代码,而非<Playground />内部的代码。相关讨论如下:

  • Allow to hide the LiveError overlay #907
  • Allow to override the playground's editor's code #906

其实第一条 PR 已经解决了问题,但是被关闭了,无奈。

不过既然都能引入 React 组件了,在MDX的环境下自定义一个Playground组件又有何难呢,无非就是渲染组件(MDX 自带)和展示源码,简单开放的东西大家都是喜闻乐见的,就叫HappyBox吧。

优化代码展示

编写 ``组件

安装依赖:

yarn add react-use react-tooltip react-feather react-simple-code-editor prismjs react-copy-to-clipboard raw-loader styled-components --dev
  • react-use - 2020 年了,当然要用hooks
  • react-simple-code-editor - 代码展示区域
  • prismjs - 代码高亮
  • raw-loader - 将源码转成字符串
  • react-copy-to-clipboard - 让用户爸爸们能够 copy demo 代码
  • react-tooltip/react-feather 辅助组件
  • styled-components 方便在文档示例中让用户看到样式,也用作文档组件的样式处理

这些依赖都是服务于文档站点应用,和组件库自身毫无关联。

最终效果如下:

最终效果最终效果

根目录下新建doc-comps文件夹,存放文档中使用的一些工具组件,比如<HappyBox />

doc-comps

  1. ├── happy-box
  2. │ ├── style.ts
  3. │ └── index.tsx
  4. └── index.ts

components/doc-comps/happy-box/index.tsx

  1. import React from 'react';
  2. import Editor from 'react-simple-code-editor';
  3. import CopyToClipboard from 'react-copy-to-clipboard';
  4. import { useToggle } from 'react-use';
  5. import ReactTooltip from 'react-tooltip';
  6. import IconCopy from 'react-feather/dist/icons/clipboard';
  7. import IconCode from 'react-feather/dist/icons/code';
  8. import { highlight, languages } from 'prismjs/components/prism-core';
  9. import { StyledContainer, StyledIconWrapper } from './style';
  10. import 'prismjs/components/prism-clike';
  11. import 'prismjs/components/prism-javascript';
  12. import 'prismjs/components/prism-markup';
  13. require('prismjs/components/prism-jsx');
  14. interface Props {
  15. code: string;
  16. title?: React.ReactNode;
  17. desc?: React.ReactNode;
  18. }
  19. export const HappyBox: React.FC<Props> = ({ code, title, desc, children }) => {
  20. const [isEditVisible, toggleEditVisible] = useToggle(false);
  21. return (
  22. <StyledContainer>
  23. <section className="code-box-demo"> {children}</section>
  24. <section className="code-box-meta">
  25. <div className="text-divider">
  26. <span>{title || '示例'}</span>
  27. </div>
  28. <div className="code-box-description">
  29. <p>{desc || '暂无描述'}</p>
  30. </div>
  31. <div className="divider" />
  32. <div className="code-box-action">
  33. <CopyToClipboard text={code} onCopy={() => alert('复制成功')}>
  34. <IconCopy data-place="top" data-tip="复制代码" />
  35. </CopyToClipboard>
  36. <StyledIconWrapper onClick={toggleEditVisible}>
  37. <IconCode data-place="top" data-tip={isEditVisible ? '收起代码' : '显示代码'} />
  38. </StyledIconWrapper>
  39. </div>
  40. </section>
  41. {renderEditor()}
  42. <ReactTooltip />
  43. </StyledContainer>
  44. );
  45. function renderEditor() {
  46. if (!isEditVisible) return null;
  47. return (
  48. <div className="container_editor_area">
  49. <Editor
  50. readOnly
  51. value={code}
  52. onValueChange={() => {}}
  53. highlight={code => highlight(code, languages.jsx)}
  54. padding={10}
  55. className="container__editor"
  56. style={{
  57. fontFamily: '"Fira code", "Fira Mono", monospace',
  58. fontSize: 14,
  59. }}
  60. />
  61. </div>
  62. );
  63. }
  64. };
  65. export default HappyBox;

相关配置变更

  • 增加 alias别名,样例源码展示相对路径不够友好,让用户直接拷贝才够省心

新建gatsby-node.js,写入以下内容以开启alias

  1. const path = require('path');
  2. exports.onCreateWebpackConfig = args => {
  3. args.actions.setWebpackConfig({
  4. resolve: {
  5. modules: [path.resolve(__dirname, '../src'), 'node_modules'],
  6. alias: {
  7. 'happy-ui/lib': path.resolve(__dirname, '../components/'),
  8. 'happy-ui/esm': path.resolve(__dirname, '../components/'),
  9. 'happy-ui': path.resolve(__dirname, '../components/'),
  10. },
  11. },
  12. });
  13. };

tsconfig.json 打包时需要忽略demo,避免组件库打包生成types时包含其中,同时增加paths属性用于 vscode 自动提示:

tsconfig.json

  1. {
  2. "compilerOptions": {
  3. "baseUrl": "./",
  4. + "paths": {
  5. + "happy-ui": ["components/index.ts"],
  6. + "happy-ui/esm/*": ["components/*"],
  7. + "happy-ui/lib/*": ["components/*"]
  8. + },
  9. "target": "esnext",
  10. "module": "commonjs",
  11. "jsx": "react",
  12. "declaration": true,
  13. "declarationDir": "lib",
  14. "strict": true,
  15. "moduleResolution": "node",
  16. "allowSyntheticDefaultImports": true,
  17. "esModuleInterop": true,
  18. "resolveJsonModule": true
  19. },
  20. "include": ["components", "global.d.ts"],
  21. - "exclude": ["node_modules"]
  22. + "exclude": ["node_modules", "**/demo/**"]
  23. }

新的问题出现了,vscode 的 alias 提示依赖 tsconfig.json,忽略 demo 文件夹后,demo 内的文件模块类型找不到声明(paths 失效),所以不能将 demo 在 tsconfig.json 中移除:

  1. {
  2. - "exclude": ["node_modules", "**/demo/**"]
  3. + "exclude": ["node_modules"]
  4. }

新建一个 tsconfig.build.json 文件:

tsconfig.build.json

  1. {
  2. "extends": "./tsconfig.json",
  3. "exclude": ["**/demo/**", "node_modules"]
  4. }

后续使用 tsc 生成类型声明文件指定tsconfig.build.json即可。

改造相关文件

components/alert/demo/1-demo-basic.tsx

  1. - import Alert from '../alert';
  2. + import Alert from 'happy-ui/lib/alert';
  3. - import '../style';
  4. + import 'happy-ui/lib/alert/style';

components/alert/index.mdx

  1. - import { Playground } from 'docz';
  2. + import { HappyBox } from '../../doc-comps';
  3. + import BasicDemoCode from '!raw-loader!./demo/1-demo-basic.tsx';
  4. ...
  5. - <Playground>
  6. - <BasicDemo />
  7. - </Playground>
  8. + <HappyBox code={BasicDemoCode} title="基本用法" desc="使用kind控制Alert类型">
  9. + <BasicDemo />
  10. + </HappyBox>

yarn start卡住时尝试删除根目录.docz文件夹,而后重新执行命令。

现在可以愉快地开发组件了。代码可以在仓库的chapter-2分支获取,若存在与本文内容不符的地方,以master分支以及文章为准。

组件库打包

宿主环境各不相同,需要将源码进行相关处理后发布至 npm。

明确以下目标:

  1. 导出类型声明文件
  2. 导出 umd/Commonjs module/ES module 等 3 种形式供使用者引入
  3. 支持样式文件 css 引入,而非只有less
  4. 支持按需加载

导出类型声明文件

既然是使用typescript编写的组件库,那么使用者应当享受到类型系统的好处。

我们可以生成类型声明文件,并在package.json中定义入口,如下:

package.json

  1. {
  2. "typings": "lib/index.d.ts", // 定义类型入口文件
  3. "scripts": {
  4. "build:types": "tsc -p tsconfig.build.json && cpr lib esm" // 执行tsc命令生成类型声明文件
  5. }
  6. }

值得注意的是:此处使用cprlib的声明文件拷贝了一份,重命名为esm。用于后面存放 ES module 形式的组件。这样做主要是为了用户手动按需引入组件时依旧可以有自动提示。

最开始使用的将声明文件单独打包成一个types文件夹,这样只通过'happy-ui'引入才可以有自动提示,但是'happy-ui/esm/xxx'和'happy-ui/lib/xxx'就无法提示。

tsconfig.build.json

  1. {
  2. "extends": "./tsconfig.json",
  3. "compilerOptions": { "emitDeclarationOnly": true }, // 只生成声明文件
  4. "exclude": ["**/__tests__/**", "**/demo/**", "node_modules", "lib", "esm"] // 排除示例、测试以及打包好的文件夹
  5. }

执行yarn build:types,可以发现根目录下已经生成了lib文件夹(tsconfig.json中定义的declarationDir字段),目录结构与components文件夹保持一致,如下:

types

  1. ├── alert
  2. │ ├── alert.d.ts
  3. │ ├── index.d.ts
  4. │ ├── interface.d.ts
  5. │ └── style
  6. │ └── index.d.ts
  7. └── index.d.ts

这样使用者引入npm 包时,便能得到自动提示,也能够复用相关组件的类型定义。

接下来将ts(x)等文件处理成js文件。

需要注意的是,我们需要输出Commonjs module以及ES module两种模块类型的文件(暂不考虑umd),以下使用cjs指代Commonjs moduleesm指代ES module。 对此有疑问的同学推荐阅读:import、require、export、module.exports 混合详解

导出 Commonjs 模块

其实完全可以使用babeltsc命令行工具进行代码编译处理(实际上很多工具库就是这样做的),但考虑到还要**处理样式及其按需加载**,我们借助 gulp 来串起这个流程。

babel 配置

首先安装babel及其相关依赖

  1. yarn add @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript @babel/plugin-proposal-class-properties @babel/plugin-transform-runtime --dev
  2. yarn add @babel/runtime-corejs3

新建.babelrc.js文件,写入以下内容:

.babelrc.js

  1. module.exports = {
  2. presets: ['@babel/env', '@babel/typescript', '@babel/react'],
  3. plugins: [
  4. '@babel/proposal-class-properties',
  5. [
  6. '@babel/plugin-transform-runtime',
  7. {
  8. corejs: 3,
  9. helpers: true,
  10. },
  11. ],
  12. ],
  13. };

关于@babel/plugin-transform-runtime@babel/runtime-corejs3

  • helpers选项设置为true,可抽离代码编译过程重复生成的 helper 函数(classCallCheck,extends等),减小生成的代码体积;
  • corejs设置为3,可引入不污染全局的按需polyfill,常用于类库编写(我更推荐:不引入polyfill,转而告知使用者需要引入何种polyfill,避免重复引入或产生冲突,后面会详细提到)。

更多参见官方文档-@babel/plugin-transform-runtime

配置目标环境

为了避免转译浏览器原生支持的语法,新建.browserslistrc文件,根据适配需求,写入支持浏览器范围,作用于@babel/preset-env

.browserslistrc

  1. >0.2%
  2. not dead
  3. not op_mini all

很遗憾的是,@babel/runtime-corejs3无法在按需引入的基础上根据目标浏览器支持程度再次减少polyfill的引入,参见@babel/runtime for target environment 。

这意味着@babel/runtime-corejs3 甚至会在针对现代引擎的情况下注入所有可能的 polyfill:不必要地增加了最终捆绑包的大小。

对于组件库(代码量可能很大),个人建议将polyfill的选择权交还给使用者,在宿主环境进行polyfill。若使用者具有兼容性要求,自然会使用@babel/preset-env + core-js + .browserslistrc进行全局polyfill,这套组合拳引入了最低目标浏览器不支持API的全部 polyfill

业务开发中,将@babel/preset-envuseBuiltIns选项值设置为 usage,同时把node_modulesbabel-loaderexclude掉的同学可能想要这个特性:"useBuiltIns: usage" for node_modules without transpiling #9419,在未支持该issue提到的内容之前,还是乖乖地将useBuiltIns设置为entry,或者不要把node_modulesbabel-loaderexclude

所以组件库不用画蛇添足,引入多余的polyfill,写好文档说明,比什么都重要(就像zent和antd这样)。

现在@babel/runtime-corejs3更换为@babel/runtime,只进行helper函数抽离。

  1. yarn remove @babel/runtime-corejs3
  2. yarn add @babel/runtime

.babelrc.js

  1. module.exports = {
  2. presets: ['@babel/env', '@babel/typescript', '@babel/react'],
  3. plugins: ['@babel/plugin-transform-runtime', '@babel/proposal-class-properties'],
  4. };

@babel/transform-runtimehelper选项默认为true

gulp 配置

再来安装gulp相关依赖

yarn add gulp gulp-babel --dev

新建gulpfile.js,写入以下内容:

gulpfile.js

  1. const gulp = require('gulp');
  2. const babel = require('gulp-babel');
  3. const paths = {
  4. dest: {
  5. lib: 'lib', // commonjs 文件存放的目录名 - 本块关注
  6. esm: 'esm', // ES module 文件存放的目录名 - 暂时不关心
  7. dist: 'dist', // umd文件存放的目录名 - 暂时不关心
  8. },
  9. styles: 'components/**/*.less', // 样式文件路径 - 暂时不关心
  10. scripts: ['components/**/*.{ts,tsx}', '!components/**/demo/*.{ts,tsx}'], // 脚本文件路径
  11. };
  12. function compileCJS() {
  13. const { dest, scripts } = paths;
  14. return gulp
  15. .src(scripts)
  16. .pipe(babel()) // 使用gulp-babel处理
  17. .pipe(gulp.dest(dest.lib));
  18. }
  19. // 并行任务 后续加入样式处理 可以并行处理
  20. const build = gulp.parallel(compileCJS);
  21. exports.build = build;
  22. exports.default = build;

修改package.json

package.json

  1. {
  2. - "main": "index.js",
  3. + "main": "lib/index.js",
  4. "scripts": {
  5. ...
  6. + "clean": "rimraf lib esm dist",
  7. + "build": "npm run clean && npm run build:types && gulp",
  8. ...
  9. },
  10. }

执行yarn build,得到如下内容:

lib

  1. ├── alert
  2. │ ├── alert.js
  3. │ ├── index.js
  4. │ ├── interface.js
  5. │ └── style
  6. │ └── index.js
  7. └── index.js

观察编译后的源码,可以发现:诸多helper方法已被抽离至@babel/runtime中,模块导入导出形式也是commonjs规范。

lib/alert/alert.js

lib/alert/alert.jslib/alert/alert.js

导出 ES module

生成ES module可以更好地进行tree shaking,基于上一步的babel配置,更新以下内容:

  1. 配置@babel/preset-envmodules选项为false,关闭模块转换;
  2. 配置@babel/plugin-transform-runtimeuseESModules选项为true,使用ES module形式引入helper函数。

.babelrc.js

  1. module.exports = {
  2. presets: [
  3. [
  4. '@babel/env',
  5. {
  6. modules: false, // 关闭模块转换
  7. },
  8. ],
  9. '@babel/typescript',
  10. '@babel/react',
  11. ],
  12. plugins: [
  13. '@babel/proposal-class-properties',
  14. [
  15. '@babel/plugin-transform-runtime',
  16. {
  17. useESModules: true, // 使用esm形式的helper
  18. },
  19. ],
  20. ],
  21. };

目标达成,我们再使用环境变量区分esmcjs(执行任务时设置对应的环境变量即可),最终babel配置如下:

.babelrc.js

  1. module.exports = {
  2. presets: ['@babel/env', '@babel/typescript', '@babel/react'],
  3. plugins: ['@babel/plugin-transform-runtime', '@babel/proposal-class-properties'],
  4. env: {
  5. esm: {
  6. presets: [
  7. [
  8. '@babel/env',
  9. {
  10. modules: false,
  11. },
  12. ],
  13. ],
  14. plugins: [
  15. [
  16. '@babel/plugin-transform-runtime',
  17. {
  18. useESModules: true,
  19. },
  20. ],
  21. ],
  22. },
  23. },
  24. };

接下来修改gulp相关配置,抽离compileScripts任务,增加compileESM任务。

gulpfile.js

  1. // ...
  2. /**
  3. * 编译脚本文件
  4. * @param {string} babelEnv babel环境变量
  5. * @param {string} destDir 目标目录
  6. */
  7. function compileScripts(babelEnv, destDir) {
  8. const { scripts } = paths;
  9. // 设置环境变量
  10. process.env.BABEL_ENV = babelEnv;
  11. return gulp
  12. .src(scripts)
  13. .pipe(babel()) // 使用gulp-babel处理
  14. .pipe(gulp.dest(destDir));
  15. }
  16. /**
  17. * 编译cjs
  18. */
  19. function compileCJS() {
  20. const { dest } = paths;
  21. return compileScripts('cjs', dest.lib);
  22. }
  23. /**
  24. * 编译esm
  25. */
  26. function compileESM() {
  27. const { dest } = paths;
  28. return compileScripts('esm', dest.esm);
  29. }
  30. // 串行执行编译脚本任务(cjs,esm) 避免环境变量影响
  31. const buildScripts = gulp.series(compileCJS, compileESM);
  32. // 整体并行执行任务
  33. const build = gulp.parallel(buildScripts);
  34. // ...

执行yarn build,可以发现生成了lib/esm三个文件夹,观察esm目录,结构同lib一致,js 文件都是以ES module模块形式导入导出。

esm/alert/alert.js

esm/alert/alert.jsesm/alert/alert.js

别忘了给package.json增加相关入口。

package.json

  1. {
  2. + "module": "esm/index.js"
  3. }

处理样式文件

拷贝 less 文件

我们会将less文件包含在npm包中,用户可以通过happy-ui/lib/alert/style/index.js的形式按需引入less文件,此处可以直接将 less 文件拷贝至目标文件夹。

gulpfile.js中新建copyLess任务。

gulpfile.js

  1. // ...
  2. /**
  3. * 拷贝less文件
  4. */
  5. function copyLess() {
  6. return gulp
  7. .src(paths.styles)
  8. .pipe(gulp.dest(paths.dest.lib))
  9. .pipe(gulp.dest(paths.dest.esm));
  10. }
  11. const build = gulp.parallel(buildScripts, copyLess);
  12. // ...

观察lib目录,可以发现 less 文件已被拷贝至alert/style目录下。

lib

  1. ├── alert
  2. │ ├── alert.js
  3. │ ├── index.js
  4. │ ├── interface.js
  5. │ └── style
  6. │ ├── index.js
  7. │ └── index.less # less文件
  8. └── index.js

可能有些同学已经发现问题:若使用者没有使用less预处理器,使用的是sass方案甚至原生css方案,那现有方案就搞不定了。经分析,有以下 3 种预选方案:

  1. 告知用户增加less-loader
  2. 打包出一份完整的 css 文件,进行**全量**引入;
  3. 单独提供一份style/css.js文件,引入的是组件 css样式文件依赖,而非 less 依赖,组件库底层抹平差异;
  4. 使用css in js方案。

方案 1 会导致业务方使用成本增加。

方案 2 无法进行按需引入。

方案 4 需要详细聊聊。

css in js除了赋予样式编写更多的可能性之外,在编写第三方组件库时更是利器。

如果我们写一个react-use这种hooks工具库,不涉及到样式,只需要在package.json中设置sideEffectsfalse,业务方使用 webpack 进行打包时,只会打包被使用到的 hooks(优先使用 ES module)。

入口文件index.js中导出的但未被使用的其他 hooks 会被tree shaking,第一次使用这个库的时候我很好奇,为什么没有按需引入的使用方式,结果打包分析时我傻了,原来人家天生支持按需引入。

可能常用的antd以及lodash都要配一配,导致产生了惯性思维。

回到正题。如果将样式使用javascript来编写,在某种维度上讲,组件库和工具库一致了,配好sideEffects,自动按需引入,美滋滋。

而且每个组件都与自己的样式绑定,不需要业务方或组件开发者去**维护样式依赖**,什么是样式依赖,后面会讲到。

缺点:

  1. 样式无法单独缓存;
  2. styled-components 自身体积较大;
  3. 复写组件样式需要使用属性选择器或者使用styled-components,麻烦了点。

需要看取舍了,偷偷说一句styled-components做主题定制也极其方便。

方案 3 是antd使用的这种方案。

在搭建组件库的过程中,有一个问题困扰了我很久:为什么需要alert/style/index.js引入less文件或alert/style/css.js引入css文件?

答案是**管理样式依赖**。

因为我们的组件是没有引入样式文件的,需要用户去手动引入。

假设存在以下场景:引入<Button /><Button />依赖了<Icon />,使用者需要手动去引入调用的组件的样式(<Button />)及其依赖的组件样式(<Icon />),遇到复杂组件极其麻烦,所以组件库开发者可以提供一份这样的js文件,使用者手动引入这个js文件,就能引入对应组件及其依赖组件的样式。

那么问题又来了,为什么组件不能自己去import './index.less'呢?

可以,不过业务方要配置less-loader,什么,业务方不想配,要你import './index.css'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/807486
推荐阅读
相关标签