从零到一搭建React组件库React那点事儿

搭建工程不打算采用create-react-app脚手架来搭建,因为脚手架封装好了很多东西,而有些东西对于组件库并不适用,用来搭建组件库过于臃肿,因此我不打算借助任何脚手架来搭建工程。

首先,先创建一个工程文件夹pony-react-ui,在该文件夹下执行如下命令:

npminit//生成package.jsontsc--init//生成tsconfig.json然后,按照如下目录结构初始化工程:

pony-react-ui├──src├──assets├──components├──Button├──Button.tsx└──index.ts└──Dialog├──Dialog.tsx└──index.ts├──styles├──_button.scss├──_dialog.scss├──_mixins.scss├──_variables.scss└──pony.scss└──index.ts//打包的入口文件,引入pony.scss,抛出每一个组件├──index.js//主文件入口,package.json中main字段指定的文件├──package.json├──tsconfig.json//指定了用来编译这个项目的根文件和编译选项├──webpack.config.js└──README.md编写一个Button组件Button组件应该满足一下需求:

Button.tsx

export*from'./Button';这样,一个示例组件就基本完成了,有同学肯定会有这么一个疑问,为什么在Button.tsx中没有引入它的样式文件_button.scss,而是在使用时引入全局样式或者单独引入_button.scss呢?

//单独引入组件样式import{Button}from'pony-react-ui';import'pony-react-ui/lib/styles/button.scss';//全局引入组件样式,打包时抽离出来的样式import'pony-react-ui/lib/styles/index.scss';这跟样式的权重有关,通过import引入的样式权重会低于JSX中className定义的样式,因此才可以在组件外部修改内部的样式。

举个实例:

import{Button}from'pony-react-ui';import'pony-react-ui/lib/styles/button.scss';importstylesfrom'./index.module.scss';constDemo=()=>(submit

)引入组件库中的Button.scss和本地的index.module.scss在打包后会以标签注入到页面中,而且顺序是:

//Button.scss的样式//index.module.scss的样式因此,index.module.scss中的样式权重是高于Button.scss中的样式,可以在index.module.scss中修改Button.scss的样式

├──styles├──_button.scss├──_dialog.scss├──_mixins.scss├──_variables.scss└──pony.scss我在style文件下存放所有的样式文件,与_button.scss、_dialog.scss类型的样式文件属于组件的样式文件,_mixins.scss用于存放mixin指令,提高样式逻辑复用

//_mixins.scss@mixincolors($text,$border,$background){color:$text;background-color:$background;border-color:$border;}//设置按钮大小@mixinbutton-size($padding-x,$height,$font-size){height:$height;padding:0$padding-x;font-size:$font-size;line-height:($height-2);}比如,在_button.scss中使用

$values:#ff0000,#00ff00,#0000ff;.primary{@includecolors($values...);}node-sass会将其编译成

.primary{color:#ff0000;background-color:#00ff00;border-color:#0000ff;}_variables.scss用于存放一些样式常量,比如定义不同尺寸按钮的字体大小:

$button-font-size:14px!default;$button-xl-font-size:16px!default;$button-lg-font-size:16px!default;$button-sm-font-size:12px!default;pony.scss会引入所有的样式文件,_mixins.scss、_variables.scss这类工具类样式文件需要置前引入,因为后面的组件样式文件可能依赖它们

@import'variables';@import'mixins';@import'button';@import'dialog';...在对样式文件构建处理时,我没有使用cssmodules去避免样式重名,而是使用BEM规范书写样式规避这一问题。为什么我要这么做呢?

rules:[{test:/\.(sa|sc|c)ss$/,use:[loader:'css-loader',options:{modules:false//禁止cssmodules}]}]因为使用cssmodules导致无法从组件外部修改组件内部样式。通常,从外部修改组件样式一般会这样写:

按钮//修改Button内部样式,假如组件内部样式有个样式类名为pony-button-promary.btn{:global{.pony-button-promary{color:#da2227;}}}但是,采用了cssmodules后,pony-button-promary类名后面会多出一串hash值,而且在每次修改Button组件后,生成的hash都会不同,这将导致在深度遍历查找过程中找不到类名

.btn{:global{//下次修改Button组件构建后,生成的hash不一定为sadf6756.pony-button-promary-sadf6756{color:#da2227;}}}构建打包入口文件src/index.ts为webpack构建入口文件

import'./styles/pony.scss';export*from'./components/Button';export*from'./components/Dialog';这里会引入全局样式文件,在构建时MiniCssExtractPlugin会对样式进行抽离压缩,然后分离输出JS脚本和CSS脚本

在构建之前,我们必须明确组件库的使用场景。现在常见会通过esmodule以及CommonJS引入,有些场景下会直接使用

THE END
1.HomemylibrarymyLibrary uses cookies to collect usage statistics for resource and service planning. Learn MoreOK Databases A - Z | Journals A - Z | Advanced Search | Searching Techniques Welcome Library Services Finding Resources Subject Resources Academic Skills Getting Help https://mylibrary.uca.ac.uk/
2.MyLibraryLibrary Collection Advanced Search Sign in All Availabilityfilter All Formatfilter Popular PublicationsPopular Publications See All 7,000 news and entertainment publications from around the world. United Magazine United Newspaper Unitedhttps://jvdl.axis360.baker-taylor.com/
3.MyLibrarySuteraMallMy Library is under renovation Subscribe and we will inform you on any update on the status of the libraryhttps://www.mylibrary.my/
4.LibraryLibrary News Primo@LibJournal SearchDatabase A-ZLibrary Guides Library Catalogue Search Quick Links My Library RecordPrimo@LibSeminar Room & Solo Pod Booking SystemHKAPA DigitalAudioMiner (campus access only)Library NewsNew AdditionsTurnitin (Online Guide)https://www.hkapa.edu/library/
5.首页library最受国际校区师生青睐的发文期刊公布啦,第一名竟是它! 古韵今风,羽翼翩翩——古宋代禽鸟画主题系列活动圆满落幕 “你选书,我买单”——中外文书展系列活动顺利举办 人性与异化的文学之旅 激励发现,推动创新——利用Web of Science进行创新性科学研究 掌握研究领域前沿信息能帮助发现新的研究方向和切入点,有助于提高https://lib.intl.zju.edu.cn/zh-hans/
6.MyLibraryLibrary tracking web app by Adam Rackishttps://mylibrary.io/
7.MyLibrary个人信息管理软件 桌面端(Windows) 4.2.0.2Android(安卓) 1.35.3帮助文档(必看) 历史版本小红书Q群:902347516https://mylibrary.cc/
8.关于MyLibrary通过使用 MyLibrary,您可以创建自定义文档收集,其中仅包含满足您特定信息需求的内容。您可以与您的团队成员或贵公司的其他组织(如客户支持、顾问、现场工程师等)共享您的自定义收集。 创建第一个库 在docs.vmware.com 中使用您的 MyVMware 帐户登录后,您就可以开始创建自定义收集。 https://docs.vmware.com/cn/MyLibrary/index.html
9.zlibrary电子书图书馆官网/最新镜像网址入口Z-library,被誉为全球范围内最为庞大的数字图书馆之一,其藏书量之丰富令人叹为观止,总计囊括了超过9,826,996册电子书及84,837,646篇学术期刊文章。这座庞大的知识宝库覆盖了从经典文学巨著到前沿理工学科,从人文艺术瑰宝到专业学术论文的广泛领域,几乎能够满足每一位求知者的阅读与学习需求。 https://blog.csdn.net/2409_89114972/article/details/144365797
10.UniversityLibraryUniversityofAdelaideResearchers Research skills, open access, data management, strategic publishing Services for researchers Teaching staff Information literacy skills, course materials, open educational resources Services for teaching staff Find a library service How Do I guides https://www.adelaide.edu.au/library/
11.MyLibrary个人信息管理My Library 个人信息管理官网 一个工作空间,管理所有的项目、日程、笔记以及影视库。 My Library 个人信息管理简介 My Library 个人信息管理是一款高效的信息管理工具,旨在帮助用户更好地组织和管理个人信息。通过这个平台,您可以轻松整理项目、日程、笔记以及影视库,提升工作效率和生活质量。 My Library 个人信息管理功能https://openi.cn/sites/229143.html
12.MainLibraryHomepage? Copyright 2022 - Tun Hussein Onn Sunway Library. All rights reserved. Login to LibApps Quick Links About Us Membership Staff Intranet PDPA (In English) PDPA (In Malay) Sunway University Sunway College GIVING Book Donation Feedback library@sunway.edu.my +603-74918750 Tun Hussein Onn https://library.sunway.edu.my/
13.MyLibraryAccountsMy Library Accounts Your K-State Libraries account and Interlibrary Loan account are two separate accounts. Choose which library account you would like to sign into. K-State Libraries account This account acts as the main account for K-State affiliates and community patron accounts. Renew items Sehttp://www.lib.k-state.edu/library-accounts
14.utm’slibrarySearch OPAC Go Advanced search Online Databases My Library Account Turnitin & Endnote Workshop & Classes Theses & Exam Papers Library Endowment Resources Highlight UTM Open Science UTM Virtual Gallery UTM Institutional Repository (UTM-IR) Theses Reference Services Open Access Initiatives UTM https://library.utm.my/
15.MyLibrary相似应用下载MyLibrary 13次下载 相似应用,小编亲测可用 风车动漫 38.61MB 查看 免费有声听书小说 11.09MB 查看 番茄免费小说 78.56MB 查看 头条搜索极速版 102.77MB 查看 Search for books, DVDs, audiobooks, CDs and more. Place holds, renew or check out items. View new releases/best sellers and our https://m.wandoujia.com/apps/834393
16.MyLibrary待办下载MyLibrary待办免费版下载v3.7.10.0MyLibrary待办旨在帮助用户高效管理、追踪和完成各种任务,是一款功能丰富的待办事项管理软件,它提供了清晰的任务列表,用户可以轻松添加、编辑和删除待办事项,设置提醒以确保不会错过任何重要任务,还支持任务分类和优先级设置,帮助用户更好地组织任务和规划工作流程。 软件功能 MyLibrary是一款注重用户体验,旨在帮助用户更好https://www.crsky.com/soft/586759.html
17.uniappandroid打不开uniapp开发android2、创建Andorid Libray名字为(mylibrary),名称可以自定义 编辑 3、复制代码到mylibrary下的build.gradle中 4、将避免混淆代码写入到consumer-rules.pro与proguard-rules.pro中 5、在mylibrary包下创建一个HelloWorld 类,如下图: 6、 打包arr包 ,步骤可以参考下面链接 https://blog.51cto.com/u_16099200/7576432
18.react官网React The library for web and native user interfaces Learn ReactAPI Reference Create user interfaces from components React lets you build user {video.title} {video.description} <LikeButtonvideo={video}/> ); } My video Video description Whether you work onhttps://www.reactjs.org/
19.华中师范大学图书馆2024级新生寄语:大学,从阅读开始MyLibrary OCLCNetlibrary 院长荐书 翻开一本好书,与智者对话,与经典同行。让我们随着院长们为大家精心挑选与推荐的好书,踏上一段充满智慧与启迪的阅读之旅,共同探索知识的无垠边界,感受阅读带来的心灵滋养与思想启迪。 “院长荐书”详细内容,请扫描下方二维码: https://tuibook.com/tushuguan/33878.html
20.ARM之七主流编译器(armcciargccforarmLLVM(clang))详细使用源对象文件module1.o、module.2.o和module3.o 创建了一个名为mylibrary.a的库文件:iarchive mylibrary.a module1.o module2.o module3.o 列出mylibrary.a中的内容:iarchive --toc mylibrary.a This example replaces module3.o in the library with the content in the module3.o file and appendshttps://cloud.tencent.com/developer/article/2032451
21.矮人要塞dwarffortressFrom my end specifically, it's mostly been technical stuff I needed to catch up on. Textures get cleaned up now rather than sticking around and muddying up subsequent playthroughs, I did some playing around with FMOD (our sound library) to make sure I can handle everything that's incominghttp://www.bay12games.com/dwarves/
22.MyLibraryHalLeonardOnlineWith Hal Leonards MyLibrary, its easy to access online content, such as audio or video files for select Hal Leonard titles, as well as Hal Leonard Digital Books. Not sure if your books includes MyLibrary access? Books that include MyLibrary access usually say "Online Audio" or "Online Videhttps://www.halleonard.com/mylibrary/
23.MyLibraryIn every book lovers home there is always a small or large library and that library always has a great value for them but what if I tell you that you can save your time to read some books than organizing that library of yours? Well, MyLibrary is just for that! Imagine controlling yourhttps://apps.apple.com/cn/app/my-library-personal-library/id1446360124?l=en-GB
24.MyLibrary码农集市专业分享IT编程学习资源MyLibrary 浅颜**半夏上传74.83 MB文件格式zip 用来存储工作上的一些通用代码 (0)踩踩(0) 所需:1积分 carbon-footprint 2024-12-04 00:09:50 积分:1 Sudoku 2024-12-03 23:55:57 积分:1 Intelligent-parking-billing-system 2024-12-03 23:43:00https://www.coder100.com/index/index/content/id/3483639