【问题标题】:How to match Node.js server's ESLint rules to my Vue.js frontend's ESLint rules?如何将 Node.js 服务器的 ESLint 规则与我的 Vue.js 前端的 ESLint 规则匹配?
【发布时间】:2020-10-26 05:16:33
【问题描述】:

我想让 Node.js 中的服务器端文件具有与我的 Vue.js 前端完全相同的 ESLint 规则。我的 Vue 项目中的 .eslintrc.js 文件如下所示:

module.exports = {
  root: true,
  env: {
    node: true
  },
  extends: ["plugin:vue/essential", "eslint:recommended", "@vue/prettier"],
  parserOptions: {
    parser: "babel-eslint"
  },
  rules: {
    "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
    "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off"
  }
};

我的 Node.js 有一个 .eslintrc.js,看起来像这样:

module.exports = {
    root: true,
    env: {
        "es6": true,
        "node": true
    },
    extends: [
        "plugin:prettier/recommended"
    ],
    globals: {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    parserOptions: {
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    rules: {
        "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
        "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",
    }
}

如果不导入所有 Vue 特定的 linting 规则(只需要 vanilla javascript 相关规则),我如何在我的 Node.js 上获得相同的 ESLint 规则,而无需手动查找每个前端规则并将其复制到后端?

【问题讨论】:

    标签: node.js vue.js eslint prettier prettier-eslint


    【解决方案1】:

    你可以简单的从前端.eslintrc.js导入

    你的 nodejs .eslintrc.js 应该是:

    const frontEndRc = require("path_to_your_frontend_eslintrc");
    
    module.exports = {
        root: true,
        env: {
            "es6": true,
            "node": true
        },
        extends: [
            "plugin:prettier/recommended"
        ],
        globals: {
            "Atomics": "readonly",
            "SharedArrayBuffer": "readonly"
        },
        parserOptions: {
            "ecmaVersion": 2018,
            "sourceType": "module"
        },
        rules: {
            ...frontEndRc.rules
        }
    }
    

    【讨论】:

    • 但我不希望所有特定于 Vue 的 linting 都在进行,只需要 vanilla javascript linting 规则。抱歉,我意识到我不清楚这一点,只是更新了问题以反映这一点。此外,前端和后端位于两个不同的根项目目录中(根是兄弟姐妹),如果这很重要的话。
    • 将原版 javascript 规则保存并导出到单个文件中。然后,在你的 nodejs eslintrc 和 vuejs eslintrc 中导入这个文件。因此,您的两个 eslintrc 文件都可以访问通用的 vanilla js 规则
    • 如何查看/保存所有创建的 vanilla JS 规则(最好只有非默认规则)?
    猜你喜欢
    • 2020-08-04
    • 2019-11-10
    • 2021-04-14
    • 1970-01-01
    • 2022-01-13
    • 2017-07-24
    • 2021-04-05
    • 2023-01-05
    • 2021-08-20
    相关资源
    最近更新 更多