0
0

More than 3 years have passed since last update.

webpackにESLintを導入する

Last updated at Posted at 2019-11-20

webpack4.xがインストール済みのところにESLint v6.6.0を導入して使えるようにするまで。

ESLintインストール

package.jsonがあるディレクトリで以下を実行。

yarn add --dev eslint eslint-loader

続いて初期化。コマンドラインでいくつかの質問に答えると、それに応じた内容の.eslintrc.jsが作られます。

eslint --init

もし、eslintがない!?ということになったら、./node_modules/.bin/とかにあるのではないかと思いますので、確認してみてください。その場合は、./node_modules/.bin/eslint --initです。

webpack.config.jsに追記

module:のブロックが追記部分です。

module.exports = {
  mode: 'development',
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: __dirname + "/dist"
  },
  module: {
    rules: [
      // For *.js
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'eslint-loader'
          }
        ]
      }
    ]
  }
}

これで使えるようになります。

おまけ

外部のライブラリを使っている場合などで、ESLintの対象としたくない場合、webpack.config.jsのexclude:に指定します。もとから/node_modules/が記載されていると思います。追加する場合は配列にすることで複数指定できます。

exclude: [/node_modules/, /^\/usr\/local\/lib/],

フルパスに対してのパターンマッチなので、~/lib/にあるものは除外したい場合は、以下のように指定することができます。

exclude: [/node_modules/, /.+\/lib\//],

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0