18

在 webpack 1.x 中,我可以在我的 webpack 配置中使用 eslint 属性来启用自动修复我的 linting 错误,方法是:

...

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

...

然而,在 webpack 2.x 中,到目前为止我一直无法使用自动修复功能,因为我不知道在我的 webpack 配置中设置它的位置。在我的 webpack configFile 中使用 eslint 属性会抛出一个WebpackOptionsValidationError.

4

1 回答 1

37

使用 webpack v2(及更高版本)自动修复 linting 规则的最常见方法是使用eslint-loader.

在你的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,
        },
      },
      // ...
    ],
  },
  // ...
};
于 2016-12-08T15:22:24.923 回答