javascript - ESLint 不忽略 .eslintrc.json 中给出的全局变量

标签 javascript node.js webpack eslint

我试图让 eslint 忽略我使用 webpack.ProvidePlugin 全局导入的文件,但该文件仍处于 linted 状态。

//file arrangement
- vendor
  `- google.js
- ...
- webpack.config.js

//relevant webpack configurations
resolve: {
  extensions: ['.js', '.json'],
  alias: {
    'google': path.resolve(__dirname, './vendor/google.js')
  }
},

....
plugins: [
  new webpack.ProvidePlugin({
    'google': 'google'
  })
]

 /*google is successfully available globally*/

//.eslintrc.json
{
  "extends": ["standard", "standard-react"],
  "globals": {
    "google": true
  }
}

//package.json script
"dev": "webpack-dev-server --port 5888",
 ...
"lint": "eslint js/**/*.js webpack.config.js"

但我仍然从 google.js 收到数百个 linting 错误。它被缩小了,但 webpack 文档说虽然这是首选,但可能没有必要,而且 eslint 文档根本没有提到它。

我也尝试过 the docs 中提到的忽略评论风格

//google.js
/*global google:true*/

为什么 ESLint 仍然给我所有这些 linting 错误?

最佳答案

global option仅指使用全局变量,对文件是否进行 linted 没有影响。

如果您不想对特定文件进行 lint 检测,则可以使用 .eslintignore 文件从 linting 过程中排除与模式匹配的文件。它与 .gitignore 非常相似。

您可能想要排除所有 vendor 文件,因此您可以使用 Ignoring files from linting 中建议的模式。 .

**/vendor/*.js

这会给你一个关于被忽略的文件的警告,因为你使用的是一个 glob,它会被你的 shell 扩展,因此你显式地将文件传递给 ESLint(参见 Ignored File Warnings )。您可以通过传递一个目录来消除该警告,ESLint 会检查该目录中的每个 JavaScript 文件,因此不需要您的 glob。

"lint": "eslint js/ webpack.config.js"

关于javascript - ESLint 不忽略 .eslintrc.json 中给出的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44425761/

相关文章:

javascript - 改进 HTML5 Canvas 图像加载

javascript - Chrome 开发工具 - 修改 javascript 并重新加载

javascript - 在 AngularJS 中切换自定义排序图标

javascript - 如何在鼠标悬停时突出显示文本

javascript - Node Js 和 Google Geocode API

mysql - Npm mysql - 我不能使用单个对象查询 WHERE 条件吗?

reactjs - 哪些模块可以在 webpack 解决方案中去除调试?

javascript - Node.js 创建的服务器在首页加载时发出 3 个请求,在每次页面刷新后发出 2 个请求?

javascript - 如何使用 webpack 将 firebase 云消息传递包含到 ReactJS 项目中

webpack - 初始化 vue-cli 时颁发者证书问题