• 安装 eslint 配置指南 及 遇到的一些问题记录


    前端eslint配置指南

    背景

    1. 当前前端项目风格混乱,每个人有自己的开发习惯,有自己的格式化习惯,不便于项目的风格统一,不利于代码维护
    2. 有的项目eslint没有用起来,没有起到规范代码的作用,导致出现一些基础代码问题,如:重复命名

    目标

    1. 统一的代码规范
    2. 统一的格式化规范
    3. 统一的强校验规范

    策略

    1. 使用eslint配置前端标准化的代码规范
    2. 使用prettier格式化代码,形成统一风格
    3. 使用husky添加git钩子,配合lint-staged强校验staged阶段的代码
    4. 使用.editorconfig

    配置步骤

    准备工作:

    1. 使用node: 12.16.0
    2. 使用cnpm

    添加eslint校验:

    1. 安装依赖包c

    1. cnpm i eslint@7.32.0 eslint-webpack-plugin@2.7.0 eslint-plugin-vue@7.20.0 --save -D

    1. 修改eslintrc

    1. module.exports = {
    2. root: true,
    3. parser: 'vue-eslint-parser',
    4. parserOptions: {
    5. parser: 'babel-eslint',
    6. ecmaVersion: 12,
    7. sourceType: 'module',
    8. },
    9. env: {
    10. node: true,
    11. browser: true,
    12. },
    13. extends: [
    14. 'eslint:recommended',
    15. 'plugin:vue/essential',
    16. ],
    17. // add your custom rules here
    18. rules: {
    19. // allow async-await
    20. 'generator-star-spacing': 'off',
    21. // allow debugger during development
    22. 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    23. 'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }],
    24. }
    25. }

    1. 修改Webpack.config.base.js

    1. // 添加插件
    2. const ESLintPlugin = require('eslint-webpack-plugin');
    3. // 配置插件
    4. plugins: [
    5. new vueLoaderPlugin(),
    6. new webpack.ProvidePlugin({
    7. jQuery: 'jquery',
    8. $: 'jquery',
    9. moment: 'moment'
    10. })
    11. // 新增插件配置
    12. ].concat(config.dev.useEslint ? (new ESLintPlugin()) : []),
    13. // 移除module.rules中的eslint配置
    14. rules:[
    15. // 移除以下代码
    16. ...(config.dev.useEslint ? [createLintingRule()] : []),
    17. ]

    添加prettier格式化:

    1. 安装依赖包

    cnpm install prettier@2.8.8 eslint-config-prettier@8.8.0 eslint-plugin-prettier@4.2.1 --save -D
    

    1. 新增.prettierrc.js文件

    1. module.exports = {
    2. printWidth: 180,
    3. tabWidth: 2,
    4. useTabs: false,
    5. semi: false,
    6. singleQuote: true,
    7. quoteProps: 'as-needed',
    8. jsxSingleQuote: true,
    9. bracketSameLine: false,
    10. trailingComma: 'es5',
    11. bracketSpacing: true,
    12. jsxBracketSameLine: true,
    13. arrowParens: 'avoid',
    14. htmlWhitespaceSensitivity: 'ignore',
    15. vueIndentScriptAndStyle: false,
    16. embeddedLanguageFormatting: 'auto',
    17. };

    1. 修改eslitrc.js文件

    1. // 新增extends
    2. extends: [
    3. 'plugin:prettier/recommended'
    4. ]

    添加强校验:

    1. 我们期望在代码commit的时候自动eslint校验我们的代码
    2. 我们期望只校验我们本次修改的代码,而不是全项目校验(对历史项目友好,加快校验效率)

    步骤

    1. 添加依赖包

    cnpm install husky@7.0.4 lint-staged@11.2.6 --save -D
    

    1. 启用git-hooks画u

    npx husky install
    

    执行完成后,会新增一个.husky文件夹

    1. 创建pre-commit钩子,用来校验staged的代码

    npx husky add .husky/pre-commit "npx lint-staged"
    

    1. package.json中配置lint-staged

    1. "lint-staged": {
    2. "*.{js,vue}": "eslint --fix"
    3. }

    配置完成后,当我们commit的时候就会自动校验我们的代码

    配置.editorconfig

    editorconfig的作用是用来规范我们的编辑习惯的

    1. root = true
    2. [*]
    3. charset = utf-8
    4. indent_style = space
    5. indent_size = 2
    6. end_of_line = lf
    7. insert_final_newline = true
    8. trim_trailing_whitespace = true

    备注:以上的所有配置,各项目根据自己的实际情况做微调,有则改之无则加勉,eslint规则切忌随意off

    vscode校验配置

    遇到eslint问题,我们可以用上面的操作格式化当前文件,或者save的时候校验

    记录遇到eslint的一些校验问题

         1、'$' is not defined

    env: {

     browser: true,

     jquery: true

    },

      2、The template root requires exactly one element     

    'vue/no-multiple-template-root': 'off',

  • 相关阅读:
    新闻分析报告:Active Directory 证书服务是企业网络的一大安全盲点
    Java多线程、常用类、枚举类、注解、集合、泛型、IO、反射
    C++vector模拟实现
    Java中的异常
    Python 文件处理指南:打开、读取、写入、追加、创建和删除文件
    Ubuntu18.04安装ROS系统+turtle测试
    MySQL - DCL(数据控制语言)介绍
    applicationContext.getBean 为null
    自定义MVC增查
    【原创】常用元器件(电阻)选型之阻值有多少-cayden20220910
  • 原文地址:https://blog.csdn.net/u013994400/article/details/134457892