将eslint-webpack-plugin添加到项目中以提供Typescript插入

xu3bshqb  于 2022-12-13  发布在  Webpack
关注(0)|答案(3)|浏览(160)

我很难在我的Webpack项目中插入一个Typescript linter。
显然eslint-loader已经过时了,他们给予了eslint-webpack-plugin作为替代。另外,我使用的是typescript-eslint/parser而不是tslintdeprecated)问题是我在.eslintrc.js中设置的规则似乎没有得到考虑。我在.eslintrc中添加了一个简单的规则,当使用分号时会抛出错误。

export default class A {
  static yes = 'this is static'; // ";" <- should be invalidated by eslint
}

我的相依性为:

"@typescript-eslint/eslint-plugin": "^4.5.0",
"@typescript-eslint/parser": "^4.5.0",
"eslint": "^7.11.0",
"eslint-webpack-plugin": "^2.1.0",
"ts-loader": "^8.0.6",
"typescript": "^4.0.3",
"webpack": "^4.41.3",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"

如果我理解正确的话,它应该是这样工作的:

# # # # # # # # # # # # # # 
@typescript-eslint/parser --> eslint-webpack-plugin --> #                         # 
      (uses eslint?)        (replaces eslint-loader)    #       webpack v4        #
                                                        #  w/ webpack-dev-server  #
       typescript         -->       ts-loader       --> #                         #
                                                        # # # # # # # # # # # # # #

边注:* 我使用的是webpack-dev-server,而不是webpack-cli的服务命令doesn't seem to work quite yet with webpack v5*。
启动服务器时运行的命令:webpack-dev-server --hot --inline

  • 项目结构:
./
├─── src
│    └─── index.ts
└─── dist
     ├─── bundle.js
     └─── index.html <- imports bundle.js

*webpack.配置.js

const path = require('path')
const ESLintPlugin = require('eslint-webpack-plugin')

module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  devtool: 'inline-source-map',
  devServer: {
    contentBase: path.resolve(__dirname, 'dist')
    // publicPath: "/assets", // path of the served resources (js, img, fonts...)
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
    new ESLintPlugin({
      files: 'src/**/*.ts',
    })
  ],
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
}

*.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
  },
  parser: '@typescript-eslint/parser',
  extends: [
    'plugin:@typescript-eslint/recommended'
  ],
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
  },
  rules: {
    'semi': ['error', 'never'],
  },
  ignorePatterns: [ "dist/*" ],
}

*tsconfig.json文件系统

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "dom",
      "dom.iterable",
    ]
  },
  "include": [
    "src/**/*.ts",
    "tests/**/*.ts",
  ],
  "exclude": [
    "node_modules"
  ]
}
ttp71kqs

ttp71kqs1#

我在尝试将eslint-webpack-plugin.vue文件一起使用时遇到了类似的问题。
我的解决方案是使用extensions设置而不是files

new ESLintPlugin({
  extensions: ['ts']
})
dpiehjr4

dpiehjr42#

我修复了这个问题,我当前的ESLintPlugin选项对象如下:

const lintJSOptions = {
  context: path.join(paths.context, paths.js),
  extensions: ['js', 'ts'],
  emitError: true,
  emitWarning: true,
  failOnWarning: false,
  failOnError: false,
  // Toggle autofix
  fix: false,
  cache: false,

  formatter: require('eslint-friendly-formatter'),
};

希望这对你有帮助。
PD:context属性是您的 *.js或 *.ts文件相对于您的package.json文件的根位置,您可以使用path.resolve(__dirname, './src/scripts');或任何您存储脚本的位置。

guykilcj

guykilcj3#

我在尝试使用Webpack设置狭缝时遇到了同样的问题。我通过使用Webpack内部的覆盖配置解决了这个问题

plugins: [
    new ESLintPlugin({
      files: 'src/**/*.ts',
      "overrideConfig": {
        "extends": "eslint:recommended",
        "rules": { }
      }
    })
  ],

您可以在官方eslint文档www.example.com中找到它https://eslint.org/docs/latest/developer-guide/nodejs-api#-new-eslintoptions

相关问题