简体   繁体   English

Vue 2 - ESLint + 标准 + 更漂亮

[英]Vue 2 - ESLint + Standard + Prettier

How do I create a Vue 2 project that uses ESLint + StandardJS + Prettier?如何创建一个使用 ESLint + StandardJS + Prettier 的 Vue 2 项目?

StandardJS's rules should naturally take precedence over Prettier's. StandardJS 的规则自然应该优先于 Prettier 的规则。

vue create only provides the following options: vue create 只提供以下选项:

  1. ESLint + Standard ESLint + 标准
  2. ESLint + Prettier ESLint + 更漂亮

I tried 2 things:我尝试了两件事:

  1. I mixed the eslint configurations of both of the above options.我混合了上述两个选项的 eslint 配置。 It resulted in a dependency hell, and once that was solved it didn't really work as expected.这导致了依赖地狱,一旦解决了它,它并没有真正按预期工作。
  2. I added the prettier-standard package to my eslintrc.js, it didn't work as expected either.我在 eslintrc.js 中添加了 prettier-standard 包,它也没有按预期工作。 It's worth mentioning that prettier-standard works well when manually executing it from the command line.值得一提的是,prettier-standard 在从命令行手动执行时效果很好。

I'm of course looking to set this up at the project config level and not at the IDE level.我当然希望在项目配置级别而不是在 IDE 级别进行设置。

Can you try this repo I've just created?你能试试我刚刚创建的这个 repo 吗? Looks like it's working great from what I've tested.从我的测试来看,它看起来效果很好。

https://github.com/kissu/so-eslint-standard-prettier-config https://github.com/kissu/so-eslint-standard-prettier-config

Notes笔记

  • I created 2 projects and dumped the configuration of the standard one into the Prettier one, the changes can be seen in this commit我创建了 2 个项目并将standard一个的配置转储到Prettier一个中,更改可以在此提交中看到
  • CLI's current version of @vue/eslint-config-standard is throwing an error ( Environment key "es2021" is unknown ) because it requires ESlint 7 to work, as shown in this changelog CLI 当前版本的@vue/eslint-config-standard抛出错误( Environment key "es2021" is unknown )因为它需要 ESlint 7 才能工作,如变更日志所示
  • bumping ESlint to the latest version 7.29.0 , fixed the issue将 ESlint 升级到最新版本7.29.0 ,修复了问题
  • to check your project's current version of ESlint, you can run npx eslint --version要检查项目当前的 ESlint 版本,您可以运行npx eslint --version
  • of course, you need to have the ESlint extension enabled and Prettier one disabled (if using VScode), otherwise it may conflict当然,你需要启用ESlint扩展和禁用Prettier one(如果使用VScode),否则可能会发生冲突

I've tried to remove @vue/prettier from我试图从@vue/prettier删除

extends: ['plugin:vue/essential', 'eslint:recommended', '@vue/standard', '@vue/prettier']

and see if it's successfully removes any ;并查看它是否成功删除了任何内容; and it does!确实如此!

The errors are indeed coming from ESlint (if we do remove @vue/prettier ), and they're fixed by ESlint only upon save (after an ESlint server + VScode restart)!错误确实来自 ESlint(如果我们确实删除了@vue/prettier prettier ),并且它们仅在保存时由 ESlint 修复(在 ESlint 服务器 + VScode 重新启动后)!

在此处输入图片说明

Putting Prettier back works fine of course.Prettier放回去当然很好。


Luckly, I had a new PC, hence had the opportunity to try a whole fresh config with VScode.幸运的是,我有一台新 PC,因此有机会使用 VScode 尝试全新的配置。 I had to install ESlint only and have those settings into my settings.json我只需要安装ESlint并将这些设置放入我的settings.json

{
  "editor.codeActionsOnSave": {
      "source.organizeImports": false,
      "source.fixAll": true,
      "source.fixAll.eslint": true,
      "source.fixAll.stylelint": true
    }
}

The formatting works perfectly and nothing more is required.格式化工作完美,不需要更多。

I have eslint 7.8.1 with Vue Prettier on and i don't have any problem, maybe the version of eslint that you have is not compatible with Prettier or maybe your eslint have some errors?我有 eslint 7.8.1 和 Vue Prettier,我没有任何问题,也许你的 eslint 版本与 Prettier 不兼容,或者你的 eslint 有一些错误?

In each way i will put my eslint configuration and maybe it will help you!在每种方式中,我都会放置我的 eslint 配置,也许它会帮助你!

module.exports = {
    env: {
        'browser': true,
        'es6': true,
        'node': true
    },
    parserOptions: {
        'parser': 'babel-eslint'
    },
    extends: [
        '@vue/standard',
        'plugin:vue/recommended'
    ],
    rules: {
        "vue/html-indent": ["error", 4, {
            "attribute": 1,
            "closeBracket": 0,
            "switchCase": 0,
            "ignores": []
        }],

        "vue/max-attributes-per-line": [2, {
            "singleline": 10,
            "multiline": {
              "max": 1,
              "allowFirstLine": false
            }
          }],
        'indent': ['error', 4],
        'vue/component-name-in-template-casing': ['error', 'kebab-case'],
        'vue/script-indent': [
            'error',
            4,
            { 'baseIndent': 1 }
        ],
        'space-before-function-paren': ['error', 'never'],
        'semi': [2, "never"],
        'vue/singleline-html-element-content-newline': 'off',
        'vue/multiline-html-element-content-newline': 'off'
    },
    overrides: [
        {
            'files': ['*.vue'],
            'rules': {
                'indent': 'off'
            }
        }
    ]
}

Also maybe you have forgot some of the devDependecies on package.json, those are mine也可能你忘记了 package.json 上的一些 devDependencies,那些是我的

"eslint": "^7.8.1",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-vue": "^6.2.2"

Hope that those will help you !希望那些会帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM