reactjs - ESLint 尊重 VS Code jsconfig.json baseUrl 和路径

标签 reactjs visual-studio-code eslint

我的 VS Code 中有一个 jsconfig.json 文件:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./src/",
    "paths": {
      "*": ["*"],
      "components/*": ["components/*"]
    }
  },
  "exclude": ["node_modules", "**/node_modules/*"]
}

拥有路径可以帮助我拥有更短的导入语句。我的代码构建得很好,但是我如何告诉 eslint 尊重这些设置?

import React from 'react';

export default () => <div>Hello guys</div>;

和我的./src/App.js:

import React from 'react';
import HelloWorld from 'components/HelloWorld';

const App = () => (
  <div className="App">
    <header className="App-header">
      <h1 className="App-title">Welcome to React</h1>
    </header>
    <p className="App-intro">
      To get started, edit <code>src/App.js</code> and save to reload.
    </p>
    <HelloWorld />
  </div>
);

export default App;

但是我的第二行有一个红色的波浪线。如何配置我的 eslint 以遵循 jsonfig.json?禁用此规则将是最后的手段。

基本上,我想要像本教程一样进行绝对导入:https://itnext.io/create-react-app-with-vs-code-1913321b48d

这样 ESLint 就不会提示它。

我的 IDE 截图: enter image description here

最佳答案

安装:

npm i -D eslint-import-resolver-webpack eslint-plugin-import

添加到.eslintrc:

"settings": {
  "import/resolver": {
   "webpack": {
     "config": "webpack.config.js"
   }
  }
}

添加到webpack.config.js:

const path = require('path')

resolve: {
  modules: [
   'node_modules',
   path.resolve('./assets'),  //your path
   path.resolve('./constans'), //your path
]}

关于reactjs - ESLint 尊重 VS Code jsconfig.json baseUrl 和路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49352090/

相关文章:

javascript - Meteor React 验证包

c# - 如何在微软新的Visual Studio Code中编译c#?

vue.js - 让 Prettier 删除 async 关键字后的 <space>

json - Q : Correct way posting JSON from Reactstrap form

javascript - 在 react 中添加新项目到数组开头时出现问题

c# - .NET 5 + 自定义 appsettings.json - SPA 默认页面中间件无法返回默认页面

macos - 找不到与命令 "dotnet-ef"匹配的可执行文件 (Csproj)

windows - 无法在 cmd、Powershell 或 Vscode 中创建或更新文件。但我可以通过 Sublime、git bash 和文件资源管理器

typescript - 哪个规则可以在 ESLint CLI 输出中抛出 "Module has no exported member"?

javascript - ESlint - import.meta 导致致命解析错误