如何使eslint导入/首先了解我的Webpack别名?

时间:2019-10-22 22:32:10

标签: webpack eslint webpack-4 eslintrc

我正在使用eslint-import-resolver-webpack使我的陪同人员知道我的webpack别名。

但是我还有一个问题:
eslint正在报告:Absolute imports should come before relative imports. eslint(import/first)

如何使eslint理解我的别名导入是相对导入而不是绝对导入?


这是有关我的设置的更多信息:

| source
 - | app
    - | containers
       - | CalendarContainer.js
| .eslintrc
| webpack.config.dev.js
| webpack.config.resolve.js

webpack.config.dev.js

const resolveConfig = require('./webpack.config.resolve')
const appConfig = {
  // lots of config,
  ...resolveConfig // to have the resolve key with aliases underneath
}
module.exports = [appConfig, ...]

webpack.config.resolve.js

const path = require('path');

module.exports = {
    resolve: {
        extensions: ['.js'],
        modules: ['node_modules'],
        alias: {
            '~containers': path.resolve(__dirname, 'source/app/containers'),
            // ...
        },
    },
};

.eslintrc

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

CalendarContainer.js

import React, { useEffect, useState } from 'react';

import Button from '../Components/Button';
import SearchModalContainer from '~containers/SearchModalContainer';

除带有以下内容的行外,此方法均正常运行:
import SearchModalContainer from '~containers/SearchModalContainer';

的确,eslint仍然报告错误,内容如下:
Absolute imports should come before relative imports.eslint(import/first)

如何让eslint理解此导入是相对导入而不是绝对导入?

(而且我不想在eslint配置上禁用此规则)

1 个答案:

答案 0 :(得分:-1)

使用eslint-import-resolver-alias

只需将eslint-import-resolver-webpack替换为eslint-import-resolver-alias,您将可以轻松配置eslint的别名。