简体   繁体   English

有没有办法在不弹出的情况下为 create-react-app 更改 webpack 的 eslint

[英]Is there a way to change the eslint of the webpack for the create-react-app without ejecting

我想在不弹出的情况下更改反应应用程序的 eslint 规则。

We can add new ESLint rules by following just 3 simple steps.我们可以通过以下 3 个简单步骤添加新的 ESLint 规则。

  1. Create .eslintrc.js under the src folder of the your project.在项目的 src 文件夹下创建 .eslintrc.js。

  2. Add the following lines in the file:在文件中添加以下几行:

    module.exports = { "extends": "react-app", "rules" : { "indent": ["error", 4] // A custom style-related rule for example // More custom rules here } } module.exports = { "extends": "react-app", "rules" : { "indent": ["error", 4] // 例如自定义样式相关规则 // 此处有更多自定义规则 } }

This way, we will be telling our IDE to extend the base ESLint rules provided by the create-react-app and then follow some rules defined by us.这样,我们将告诉我们的 IDE 扩展 create-react-app 提供的基本 ESLint 规则,然后遵循我们定义的一些规则。

  1. In your package.json , add the following script:在您的package.json ,添加以下脚本:

    "lint:fix": "eslint src/**/*.js --fix", "lint:fix": "eslint src/**/*.js --fix",

This is mainly for CLI and CI/CD environment.这主要是针对 CLI 和 CI/CD 环境的。

Now default setup in most of the IDEs are that they would read the configuration from src/.eslintrc.js.现在大多数 IDE 的默认设置是它们将从 src/.eslintrc.js 读取配置。 Hence you should now see errors/warnings from your custom eslintrc file on your IDE as well因此,您现在也应该在 IDE 上看到来自自定义 eslintrc 文件的错误/警告

Src - http://rahulgaba.com/front-end/2019/02/17/Use-custom-eslint-config-in-create-react-app-using-three-simple-steps-No-external-dependencies.html Src - http://rahulgaba.com/front-end/2019/02/17/Use-custom-eslint-config-in-create-react-app-using-three-simple-steps-No-external-dependencies。 html

Add an .eslintrc file to the root of your project and in that extend the eslint-config-react-app and add your rules/overrides, eg.eslintrc文件添加到项目的根目录,并在其中扩展eslint-config-react-app并添加您的规则/覆盖,例如

// .eslintrc
{
  "extends": "react-app",
  "rules": {
    // enable additional rules or override from react-app
    "indent": ["error", 4],

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

相关问题 如何在不弹出的情况下将 Webpack 模块联合插件与 create-react-app 一起使用 - How to use Webpack Module Federation plugin with create-react-app without ejecting 有没有办法更改弹出的 create-react-app 的 eslint 规则? - Is there a way to change eslint rules for ejected create-react-app? 我可以在不弹出的情况下更改 create-react-app 中公共文件夹的名称吗? - Can I change the name of the public folder in create-react-app without ejecting? create-react-app 使用自定义 service worker 而不弹出 - create-react-app use custom service worker without ejecting 有没有办法在没有 create-react-app 的情况下在 React 中导入模块? - Is there a way to import modules in React without create-react-app? create-react-app 和 svgr 的 webpack 错误 - webpack errors with create-react-app & svgr ESLint 仅针对特定目录(eslintrc、create-react-app) - ESLint only target a specific directory (eslintrc, create-react-app) 如何扩展 Eslint 以使用 create-react-app - How to Extend Eslint to work with create-react-app 安装 npm create-react-app 后 ESLint 出现问题 - Problem with ESLint after installing npm create-react-app 使用create-react-app时如何在没有webpack依赖的情况下导入节点模块或JavaScript文件? - How to import node module or JavaScript file without webpack dependency when using create-react-app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM