首页 > 其他分享 >react配置eslint、prettier和commitlint规范工程

react配置eslint、prettier和commitlint规范工程

时间:2023-09-13 10:01:16浏览次数:26  
标签:lint staged react mrm eslint config commitlint prettier

1. 配置prettier:

(1). install Prettier locally:

yarn add --dev --exact prettier

Then, create an empty config file to let editors and other tooling know you are using Prettier:

echo {}> .prettierrc.json

Next, create a .prettierignore file to let the Prettier CLI and editors know which files to not format. Here’s an example:

# Ignore artifacts:
build
coverage

Now, format all files with Prettier:

// 本地运行
yarn prettier --write .

(2). Pre-commit Hook:

You can use Prettier with a pre-commit tool. This can re-format your files that are marked as “staged” via git add before you commit.

Make sure Prettier is installed and is in your devDependencies before you proceed.

npx mrm lint-staged

报错:

Preset "default" not found.
We’ve tried to load "mrm-preset-default" and "default" npm packages.

The problem is within mrm which is currently in version 3 that seems to be incompatible with lint-staged, to fix this you got to specify mrm version 2 by running npx mrm@2 lint-staged
问题在于mrm当前版本3似乎与lint-staged不兼容,要解决此问题,您必须通过运行指定mrm版本2 npx mrm@2 lint-staged

npx mrm@2 lint-staged

This will install husky and lint-staged, then add a configuration to the project’s package.json that will automatically format supported files in a pre-commit hook.

// 这块如果是mrm@2安装的,需要手加
"husky": {
  "hooks": {
    "pre-commint": "lint-staged"
  }
},
"lint-staged": {
  "*.{js,css,md,ts,tsx}": "prettier --write"  // add to ts、tsx
}

(3). ESLint (and other linters):

防止与eslint本身的冲突

If you use ESLint, install eslint-config-prettier to make ESLint and Prettier play nice with each other. It turns off all ESLint rules that are unnecessary or might conflict with Prettier. There’s a similar config for Stylelint: stylelint-config-prettier

yarn add eslint-config-prettier -D
"eslintConfig": {
  "extends": [
    "react-app",
    "react-app/jest",
    "prettier" // 使用prettier规则覆盖原来的规则,保证不冲突.
  ]
}

2. commit lint:

# Install commitlint cli and conventional config:
// 表示安装@commitlint/config-conventional、@commitlint/cli两个包
yarn add @commitlint/{config-conventional,cli} -D

# Configure commitlint to use conventional config:
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

# Add in package.json:
yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'

# For example:
"husky": {
  "hooks": {
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  }
}

标签:lint,staged,react,mrm,eslint,config,commitlint,prettier
From: https://blog.51cto.com/u_16237074/7451983

相关文章

  • 遇到错误:ESLint:TypeError:this.libOptions.parse is not a function
    遇到错误:ESLint:TypeError:this.libOptions.parseisnotafunction打开vue文件之后遇到如下错误:ESLint:TypeError:this.libOptions.parseisnotafunction该问题是由ESLint8.23中引入的更改引起的解决方法:将ESLint降级到8.22.x或更早版本npminstalleslint@8......
  • OpenTiny Vue组件库实现跨框架(vue2、vue3、react、solid)
    本文由TinyVue组件库核心成员郑志超分享,首先分享了实现跨框架组件库的必要性,同时通过演示demo和实际操作向我们介绍了如何实现一个跨框架的组件库。前言前端组件库跨框架是什么?前端组件库跨框架是指在不同的前端框架(如React、Vue、Solid等)之间共享和复用组件的能力。这种能力可以让......
  • React框架下如何集成H.265网页流媒体EasyPlayer.js视频播放器?
    H5无插件流媒体播放器EasyPlayer属于一款高效、精炼、稳定且免费的流媒体播放器,可支持多种流媒体协议播放,可支持H.264与H.265编码格式,性能稳定、播放流畅,能支持WebSocket-FLV、HTTP-FLV,HLS(m3u8)、WebRTC等格式的视频流。在功能上,EasyPlayer支持直播、点播、录像、快照截图、MP4播放......
  • react native项目安装需求
    1.创建RN项目2.安装路由依赖3.redux需求安装redux依赖4.第三方组件库5.打包及各种其他操作 安装指定依赖注意:**link**链接库已在RN(reactnative)0.60版本开始支持自动链接,不需要再手动link!!! ###2.安装路由依赖#####安装所需包npminstall@react-navigation/native##......
  • react18-webchat网页聊天实例|React Hooks+Arco Design仿微信桌面端
    React18Hooks+Arco-Design+Zustand仿微信客户端聊天ReactWebchat。react18-webchat基于react18+vite4.x+arco-design+zustand等技术开发的一款仿制微信网页版聊天实战项目。实现发送带有emoj消息文本、图片/视频预览、红包/朋友圈、局部模块化刷新/美化滚动条等功能。使用技术......
  • 超全面详细一条龙教程!从零搭建React项目全家桶(上篇)
    超全面详细一条龙教程!从零搭建React项目全家桶(上篇)兔子先生 ​关注他 101人赞同了该文章 React是近几年来前端项目开发非常火的一个框架,其背景是Facebook团队的技术支持,市场占有率也很高。很多初学者纠结一开始是学react还是vue。个人觉得,有时间的......
  • 从0到1搭建一个react项目
    从0到1搭建一个react项目react分享高级前端工程师​关注他  首先新建一个文件夹,然后用编辑器vscode打开这个文件夹打开文件夹后执行npminit命令,会提示你生成package.json文件然后下载npm包,下面贴下package.json{"name":"demo","versi......
  • 前端科普系列-ESlint:守住优雅的护城河
    前端科普系列-ESlint:守住优雅的护城河无名之辈一个有代码洁癖的前端小开发,热爱生活,追求极致​关注他 47人赞同了该文章摘要本系列文章旨在帮助学习者了解前端,主要覆盖前端的基础知识,但不深入讲解,定位为大而全并非细而精,适合非前端开发的同学对前......
  • chrome插件:一个基于webpack + react的chrome 插件项目模板
    项目结构$tree-L1.├──README.md├──node_modules#npm依赖├──package.json#详细依赖├──pnpm-lock.yaml├──public#里边包含dist,安装的时候安装这个目录即可├──src#源码文......
  • 关于 Angular eslint-disable-next-line 注释的使用
    在Angular开发中,开发者经常会使用Lint工具来保持代码质量的一致性和规范性。其中,ESLint是一个用于识别和修复JavaScript代码问题的流行Lint工具,而@typescript-eslint则是一个专为TypeScript设计的插件,它为ESLint提供了在TypeScript代码中进行静态分析和规则校验......