简体   繁体   English

Webpack 2 eslint-loader 自动修复

[英]Webpack 2 eslint-loader auto fix

In webpack 1.x I could use the eslint property in my webpack config to enable autofixing my linting errors using:在 webpack 1.x 中,我可以在我的 webpack 配置中使用 eslint 属性来启用自动修复我的 linting 错误,方法是:

...

module.exports = {
  devtool: 'source-map',
  entry: './src/app.js',
  eslint: {
    configFile: '.eslintrc',
    fix: true
  },

...

However, in webpack 2.x, thusfar I have been unable to use the auto fix functionality, because I don't know where to set it in my webpack config.然而,在 webpack 2.x 中,到目前为止我一直无法使用自动修复功能,因为我不知道在我的 webpack 配置中设置它的位置。 Using the eslint property in my webpack configFile throws an WebpackOptionsValidationError .在我的 webpack configFile 中使用 eslint 属性会引发WebpackOptionsValidationError

The most common way to auto fix linting rules with webpack v2 (and higher) is to use the eslint-loader . 使用webpack v2 (及更高版本) 自动修复linting规则的最常用方法是使用eslint-loader

In your webpack.config.js you would do: 在你的webpack.config.js你会这样做:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.jsx?$/, // both .js and .jsx
        loader: 'eslint-loader',
        include: path.resolve(process.cwd(), 'src'),
        enforce: 'pre',
        options: {
          fix: true,
        },
      },
      // ...
    ],
  },
  // ...
};

Webpack 5 with eslint-webpack-plugin:带有 eslint-webpack-plugin 的 Webpack 5:

  const ESLintPlugin = require('eslint-webpack-plugin');
  ....
  plugins: [
    ...
    new ESLintPlugin({fix: true}),
    ...
  ]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM