Nuxt3 实战 (二):配置 Eslint、Prettierrc、Husky等项目提交规范

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!


2. 配置 `nuxt.config.ts`:



modules: [
// Simple usage
‘@nuxtjs/stylelint-module’,

// With options
[‘@nuxtjs/stylelint-module’, { /* module options */ }]
]


3. 新增 `.stylelintrc.cjs` 文件,具体配置请参考[Stylelint 配置]( ):



module.exports = {
extends: [‘stylelint-config-standard’,‘stylelint-config-recommended-vue’], // 这里保证 stylelint-config-recommended-vue 放在最后,不然 vue 文件会报错
plugins: [‘stylelint-order’],
rules: {
// 颜色指定大写
‘color-hex-case’: ‘upper’,
// 禁止空块
‘block-no-empty’: true,
// 颜色6位长度
‘color-hex-length’: ‘long’,
// 兼容自定义标签名
‘selector-type-no-unknown’: [
true,
{
ignoreTypes: []
}
],
// 忽略伪类选择器 ::v-deep
‘selector-pseudo-element-no-unknown’: [
true,
{
ignorePseudoElements: [‘v-deep’]
}
],
// 禁止低优先级的选择器出现在高优先级的选择器之后。
‘no-descending-specificity’: null,
// 不验证@未知的名字,为了兼容scss的函数
‘at-rule-no-unknown’: null,
// 禁止空注释
‘comment-no-empty’: true,
// 禁止简写属性的冗余值
‘shorthand-property-no-redundant-values’: true,
// 禁止值的浏览器引擎前缀
‘value-no-vendor-prefix’: true,
// property-no-vendor-prefix
‘property-no-vendor-prefix’: true,
// 禁止小于 1 的小数有一个前导零
‘number-leading-zero’: ‘never’,
// 禁止空第一行
‘no-empty-first-line’: true,
// 属性的排序
‘order/properties-order’: [
‘position’,
‘top’,
‘right’,
‘bottom’,
‘left’,
‘z-index’,
‘display’,
‘justify-content’,
‘align-items’,
‘float’,
‘clear’,
‘overflow’,
‘overflow-x’,
‘overflow-y’,
‘margin’,
‘margin-top’,
‘margin-right’,
‘margin-bottom’,
‘margin-left’,
‘border’,
‘border-style’,
‘border-width’,
‘border-color’,
‘border-top’,
‘border-top-style’,
‘border-top-width’,
‘border-top-color’,
‘border-right’,
‘border-right-style’,
‘border-right-width’,
‘border-right-color’,
‘border-bottom’,
‘border-bottom-style’,
‘border-bottom-width’,
‘border-bottom-color’,
‘border-left’,
‘border-left-style’,
‘border-left-width’,
‘border-left-color’,
‘border-radius’,
‘padding’,
‘padding-top’,
‘padding-right’,
‘padding-bottom’,
‘padding-left’,
‘width’,
‘min-width’,
‘max-width’,
‘height’,
‘min-height’,
‘max-height’,
‘font-size’,
‘font-family’,
‘font-weight’,
‘text-align’,
‘text-justify’,
‘text-indent’,
‘text-overflow’,
‘text-decoration’,
‘white-space’,
‘color’,
‘background’,
‘background-position’,
‘background-repeat’,
‘background-size’,
‘background-color’,
‘background-clip’,
‘opacity’,
‘filter’,
‘list-style’,
‘outline’,
‘visibility’,
‘box-shadow’,
‘text-shadow’,
‘resize’,
‘transition’
]
}
}


4. 向 `package.json` 的 `scripts` 中添加命令:



“lint:style”: “stylelint src/`/*.{css,less,vue} --fix”, // 这里记得修改 nuxt.config.ts 的 srcDir 值为 ‘src/’



> 
> `stylelint` 的坑比较多,如果大家在配置后发现不生效,可以自行百度解决一下。
> 
> 
> 


### 配置 Husky


1. 执行安装命令



pnpm add husky -D


2. 初始化脚本



pnpm exec husky init


完成之后会在根目录生成一个 `.husky` 文件夹。


### 配置 Lint-staged


1. 执行安装命令



pnpm add lint-staged -D


2. 向 `package.json` 的 `scripts` 中添加命令:



“pre-commit”: “lint-staged”


3. 可以根据项目需要在 `package.json` 中添加配置,或者根目录新建 `.lintstagedrc` 配置文件:



{
“lint-staged”: {
“*.{js,jsx,vue,ts,tsx}”: [
“eslint --fix”,
“prettier --write”
]
}
}


4. 将 `.husky/pre-commit` 脚本的内容改为:



npm run pre-commit


配置完成后,这样当我们每次执行 `git` 命令的时候就会去检查暂存区的文件,有语法错误就会提示。


### 配置 Commitlint


1. 执行安装命令



pnpm add @commitlint/config-conventional @commitlint/cli -D


2. 根目录添加 `commitlint.config.cjs` 配置文件:



module.exports = {
extends: [‘@commitlint/config-conventional’],
rules: {
‘type-enum’: [
// type枚举
2,
‘always’,
[
‘build’, // 编译相关的修改,例如发布版本、对项目构建或者依赖的改动
‘feat’, // 新功能
‘fix’, // 修补bug
‘docs’, // 文档修改

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

711)]
[外链图片转存中…(img-QMKkrj3V-1715680852711)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值