从零搭建一个组件库(二)创建代码规范
创始人
2024-05-16 12:33:05
0

文章目录

    • 前言
    • 集成`eslint`
      • 1.安装
      • 2.替换默认解析器
      • 3.创建`.eslintrc.yml`配置文件
      • 4.创建忽略文件`.eslintignore`
    • 集成 `prettier`
      • 1.安装
      • 2.创建配置文件`.prettierrc`
    • 集成# `commitizen`
      • 1.安装
      • 2.修改package.json
      • 3.测试
    • className的BEM规范
      • 1.安装
      • 2.BEM概述
      • 3.创建hooks函数
      • 4.使用hooks函数
      • 5.封装scss函数
      • 6.使用scss函数
    • 总结

前言

上节我们搭建了组件库的基本环境架构,这节我们来对项目的代码规范和git提交规范进行配置。

集成eslint

1.安装

pnpm i eslint eslint-plugin-vue -D -w

2.替换默认解析器

因为EsLint默认使用ESpree解析器进行解析,无法识别一些特定的TypeScript语法,因此使用@typescript-eslint/parser替换默认的解析器。

pnpm i @typescript-eslint/parser -D -w

同时,作为eslint的补充,@typescript-eslint/eslint-plugin还提供了一些额外的TypeScript语法支持。

pnpm i @typescript-eslint/eslint-plugin -D -w

3.创建.eslintrc.yml配置文件

## .eslintrc.yml
env:browser: truees2021: truenode: true
extends:- plugin:vue/vue3-essential- standard-with-typescript- prettier
overrides: []
parser: '@typescript-eslint/parser'
parserOptions:ecmaVersion: latestsourceType: module
plugins:- vue- '@typescript-eslint'# - import- prettier
rules: {eqeqeq: offcurly: errorquotes:- error- double
}

4.创建忽略文件.eslintignore

## .eslintignore
node_modules/
dist/
index.html

集成 prettier

1.安装

pnpm i prettier eslint-config-prettier eslint-plugin-prettier --save-dev

2.创建配置文件.prettierrc

## .prettierrc
{"printWidth": 120, // 一行打最大字符数"semi": true, // 行尾添加分号"trailingComma": "all", // 末尾使用逗号"singleQuote": true, // 使用单引号"arrowParens": "always", // 箭头函数只有一个参数时添加括号"bracketSpacing": true // 大括号首位添加空格
}

集成# commitizen

1.安装

pnpm install -D commitizen cz-conventional-changelog @commitlint/config-conventional @commitlint/cli commitlint-config-cz cz-customizable

2.修改package.json

{"script": {..."cz": "git-cz"},"config": {"commitizen": {"path": "./node_modules/cz-conventional-changelog"}}
}

3.测试

image.png

className的BEM规范

1.安装

我们结合sass进行使用

pnpm i sass -D -w

2.BEM概述

BEM分别指的是Block、Element和Modifier。其中,Block描述的是一个元素本身,比如table、button;Element指的是元素的一部分,比如table__item、button__inner;Modifier则是描述了元素的外观、状态和行为,比如button–success、input–disabled。

3.创建hooks函数

// useNameSpace.ts
export const defaultNamespace = 'vl';
const statePrefix = 'is-';const _bem = (namespace: string, block: string, blockSuffix: string, element: string, modifiter: string) => {let className = `${namespace}-${block}`;if(blockSuffix) {str += '-' + blockSuffix;}if(element) {str += '__' + block;}if(midifiter) {str += '--' + midifiter;}
}export const useNamespace = (block: string) {const namespace = defaultNamespace;const b = (blockSuffix = '') => {return _bem(namespace, block, blockSuffix, '', '')}const e = (element = '') => {return _bem(namespace, '', '', element, '')}const m = (modifiter = '') => {return _bem(namespace, '', '', '', modifiter);}const bem = (namespace, block, blockSuffix = '', element= '', modifiter = '') => {return _bem(namespace, block, blockSuffix, element, modifiter);}return {namespace,b,e,m,bem}
}

4.使用hooks函数

// button.vue


使用效果:

image.png

5.封装scss函数

// config.scss
$namespace: 'vl' !default;
$common-separator: '-' !default;
$element-separator: '__' !default;
$modifiter-separator: '--' !default;
$state-prefix: 'is-' !default;
// mixins.scss
@use 'config.scss' as *;
@use 'function.scss' as *;@mixin b($block) {$B: $namespace + '-' + $block !defalut;#{$B} {@content;}
}@mixin e($element) {$E: $element !default;$selector: &;$currentSeletor: '';@each $item in $element {$currentSelector: #{$currentSeletor + '.' + $B + $element-separator + $unit + ','};}// 如果父级选择器包含任意一种特殊规则,将样式放置在该父级选择器内@if hitAllSpecialNestRule($selector) {@at-root {#{$selector} {#{$currentSelector} {@content;}}}} @else {@at-root {#{$currentSelector} {@content;}}}
}@mixin m($modifier) {$selector: &;$currentSelector: '';@each $unit in $modifier {$currentSelector: #{$currentSelector + $selector + $modifier-separator + $unit + ','};}@at-root {#{$currentSelector} {@content;}}
}@mixin when($state) {@at-root {&.#{$state-prefix + $state} {@content;}}
}
// 将选择器转换为字符
@function selectorToString($selector) {$selector: inspect($selector);$selector: str-slice($selector, 2, -2);@return $selector;
}
// 判断父级选择器是否包含'--'
@function containsModifier($selector) {$selector: selectorToString($selector);@if str-index($selector, config.$modifier-separator) {// str-index 返回字符串的第一个索引@return true;} @else {@return false;}
}
// 判断父级选择器是否包含'.is-'
@function containWhenFlag($selector) {$selector: selectorToString($selector);@if str-index($selector, '.' + config.$state-prefix) {@return true;} @else {@return false;}
}
// 判断父级是否包含 ':' (用于判断伪类和伪元素)
@function containPseudoClass($selector) {$selector: selectorToString($selector);@if str-index($selector, ':') {@return true;} @else {@return false;}
}
// 判断父级选择器,是否包含`--` `.is-`  `:`这三种字符
@function hitAllSpecialNestRule($selector) {@return containsModifier($selector) or containWhenFlag($selector) or containPseudoClass($selector);
}

6.使用scss函数

// button.scss
@use 'mixins.scss' as *;@include b(button) {display: inline-flex;justify-content: center;align-items: center;background: #fff;line-height: 1;font-size: 14px;color: #000;border: 1px solid #000;border-radius: 3px;
}

使用效果:

image.png

总结

通过对代码规范的配置,极大地提升了我们开发的效率和体验。正所谓,工欲善其事必先利其器。对于一个团队,尤其是一个刚组建的团队来说,对代码格式的约束是尤为重要的,因为大家在组成一个团队之前,代码风格和编码习惯都各不相同,如果不进行相应的约束,将会造成维护困难、代码难以复用、代码维护困难等问题。而如果事先对这些地方都进行了约束,就能在很大程度上避免这些问题。

相关内容

热门资讯

迷你退出安卓系统了吗,转型新篇... 最近有没有发现你的手机上那个可爱的迷你退出图标突然不见了?别急,让我来给你揭秘迷你退出安卓系统了吗的...
华为优先使用安卓系统,打造自主... 你知道吗?最近科技圈里有个大动作,那就是华为宣布优先使用安卓系统。这可不是一个简单的决定,它背后可是...
安卓系统隐藏了设置,隐藏设置功... 你知道吗?安卓系统这个大宝藏里,竟然隐藏着一些不为人知的设置!是不是听起来就有点小激动呢?别急,今天...
反渣恋爱系统安卓,收获真爱 你有没有听说过那个神奇的“反渣恋爱系统安卓”呢?最近,这款应用在网络上可是火得一塌糊涂,不少单身狗都...
安卓出厂系统能升级,探索无限可... 你知道吗?现在这个时代,手机更新换代的速度简直就像坐上了火箭!而说到手机,安卓系统可是占据了半壁江山...
老安卓刷机系统,从入门到精通 你有没有想过,你的老安卓手机其实还有大大的潜力呢?没错,就是那个陪伴你多年的老安卓,它可不是只能用来...
安卓粉ios系统app,兼容性... 你有没有发现,身边的朋友圈里,安卓粉和iOS系统粉总是争论不休?今天,咱们就来聊聊这个话题,看看安卓...
安卓系统语言下载,探索安卓系统... 你有没有想过,你的安卓手机是不是该换换口味了?没错,就是语言!想象如果你能轻松切换到自己喜欢的语言,...
安卓共有多少种系统,究竟有多少... 你有没有想过,安卓这个我们每天不离手的操作系统,竟然有那么多不同的版本呢?没错,安卓系统就像一个大家...
安卓系统怎么播放swf,And... 你有没有遇到过这种情况:手里拿着一部安卓手机,想看一个SWF格式的动画,结果发现怎么也打不开?别急,...
pos机安卓系统跟win系统,... 你有没有想过,那些在我们生活中默默无闻的POS机,竟然也有自己的操作系统呢?没错,就是安卓系统和Wi...
俄罗斯封禁安卓系统,本土化替代... 俄罗斯封禁安卓系统的背后:技术、经济与社会的影响在数字化浪潮席卷全球的今天,智能手机已成为我们生活中...
安卓系统总是弹出权限,安卓系统... 手机里的安卓系统是不是总爱和你玩捉迷藏?每次打开一个应用,它就跳出来问你要不要给它开权限,真是让人又...
安卓系统测血氧,便捷健康生活新... 你知道吗?现在科技的发展真是让人惊叹不已!手机,这个我们日常生活中不可或缺的小玩意儿,竟然也能变身成...
蓝光助手安卓系统的,深度解析与... 你有没有发现,现在手机屏幕越来越大,看视频、刷抖音,简直爽到飞起!但是,你知道吗?长时间盯着屏幕,尤...
安卓系统如何隐藏提示,Andr... 你是不是也和我一样,在使用安卓手机的时候,总是被那些弹出来的提示信息打扰到?别急,今天就来教你怎么巧...
安卓6.0系统如何分区,And... 你有没有想过,你的安卓手机里那些神秘的分区到底是怎么来的?别急,今天就来给你揭秘安卓6.0系统如何分...
安卓系统图片怎么涂鸦,指尖上的... 你有没有想过,在安卓系统的手机上,那些单调的图片也能变得生动有趣呢?没错,就是涂鸦!今天,就让我来带...
安卓系统40g,40GB存储空... 你有没有发现,最近你的安卓手机突然变得有点“胖”了呢?没错,就是那个传说中的40G!别急,别慌,今天...
安卓5.0系统怎么重置,轻松实... 手机用久了是不是感觉卡得要命?别急,今天就来教你怎么给安卓5.0系统来个彻底的重置,让它焕发新生!一...