通过npm包的方式在项目中引入自己定义的eslint、stylelint规则

链接

安装依赖

  1. npm i eslint-config-zc-base --save-dev
  2. npm i stylelint-config-zc-base --save-dev

安装 vscode 插件

ESlint、stylelint

添加 eslint 配置

在 package.json 里新增字段 eslintConfig。(也可以新建文件stylelintrc)

  1. // vue项目(需要安装 eslint-plugin-vue、babel-eslint)
  2. "eslintConfig": {
  3. "root": true,
  4. "extends": [
  5. "plugin:vue/essential",
  6. "eslint-config-zc-base"
  7. ],
  8. "parserOptions": {
  9. "parser": "babel-eslint"
  10. }
  11. }
  12. // node项目引入
  13. "eslintConfig": {
  14. "root": true,
  15. "extends": "eslint-config-zc-base"
  16. }
  17. // 小程序
  18. "eslintConfig": {
  19. "root": true,
  20. "extends": "eslint-config-zc-base",
  21. "rules": {
  22. "no-shadow": 0
  23. },
  24. "globals": {
  25. "env": true,
  26. "App": true,
  27. "Page": true,
  28. "Component": true,
  29. "wx": true,
  30. "getApp": true,
  31. "getPage": true,
  32. "getCurrentPages": true
  33. }
  34. },

添加 stylelint 配置

在 package.json 里新增字段 stylelint。(也可以新建文件stylelintrc)

  1. // vue项目
  2. "stylelint": {
  3. "root": true,
  4. "extends": "stylelint-config-zc-base"
  5. }
  6. // 小程序项目
  7. "stylelint": {
  8. "root": true,
  9. "extends": "stylelint-config-zc-base",
  10. "rules": {
  11. "unit-no-unknown": [
  12. true,
  13. {
  14. "ignoreUnits": [
  15. "rpx"
  16. ]
  17. }
  18. ],
  19. "selector-type-no-unknown": [
  20. true,
  21. {
  22. "ignore": [
  23. "custom-elements",
  24. "default-namespace"
  25. ]
  26. }
  27. ]
  28. }
  29. }

新建 .editorconfig,统一编辑器格式

  1. # EditorConfig is awesome: http://EditorConfig.org
  2. root = true
  3. [**]
  4. # utf-8编码
  5. charset = utf-8
  6. ## 换行符
  7. end_of_line = lf
  8. # 结尾插入新行
  9. insert_final_newline = true
  10. # 使用单引号
  11. quote_type = single
  12. # 空格形式缩进
  13. indent_style = space
  14. indent_size = 4
  15. # 去除结尾空格
  16. trim_trailing_whitespace = true
  17. [*.md]
  18. insert_final_newline = false
  19. trim_trailing_whitespace = false

新建 .vscode/settings.json,覆盖本地的 vscode 配置

  1. {
  2. "editor.formatOnSave": false,
  3. "editor.codeActionsOnSave": {
  4. "source.fixAll": true,
  5. "source.fixAll.eslint": true,
  6. "source.fixAll.stylelint": true
  7. },
  8. "json.schemaDownload.enable": false,
  9. "eslint.format.enable": true,
  10. "eslint.validate": ["javascript", "javascriptreact", "vue", "html", "vue-html", "wxml"],
  11. "eslint.workingDirectories": [{ "mode": "auto" }]
  12. }

新建vue项目的scripts,来实现一键修复lint问题

vue-cli项目

需要安装依赖 @vue/cli-service、@vue/cli-plugin-eslint

  • npm run lint 一键修复所有能自动修复的eslint问题
  • npm run lint-css 检查所有的stylelint问题
  • npm run lint-css:fix 一键修复所有能自动修复的stylelint问题
  1. "scripts": {
  2. "lint": "vue-cli-service lint",
  3. "lint-css": "stylelint src/**/*.{css,less,scss,vue}",
  4. "lint-css:fix": "stylelint src/**/*.{css,less,scss,vue} --fix"
  5. },

react项目或者是webpack项目

  1. "scripts": {
  2. "lint": "eslint --ext .js,.vue src",
  3. "lint:fix": "eslint --fix --ext .js,.vue src"
  4. "lint-css": "stylelint src/**/*.{css,less,scss,vue}",
  5. "lint-css:fix": "stylelint src/**/*.{css,less,scss,vue} --fix"
  6. },

QA:为什么vue-cli只有lint,而其他项目需要加一个lint:fix的脚本?

  • 因为 vue-cli-service lint 会带自动修复的功能,所以只需要一个脚本
  • eslint只是检查,并不带修复功能。而eslint —fix带修复功能

重启 vscode

此时eslint、stylelint都正常工作了,第一次保存的时候格式化功能可能会比较慢。
如果还不行,继续npm install,或者把node_modules和package-lock.json都删了再重新安装依赖。