LoginSignup
13
4

More than 1 year has passed since last update.

Monorepoでhusky + lint-stagedをpackageごとに異なる設定で動かす

Last updated at Posted at 2022-02-16

これは何

モノレポで複数のパッケージを管理しているリポジトリで、パッケージごとにlint-stagedで走らせるコマンドを設定する方法について説明します。

前提

  • husky: 7.0.4
  • lint-staged: 12.3.3

workspaces

packages以下に2つのパッケージがある状態です。
今回はfrontendパッケージでのみeslintを実行したいと思います。

package.json
{
  "workspaces": [
    "packages/frontend",
    "packages/backend"
  ],
}

インストール

workspaceにlint-staged, huskyをインストールします。

yarn add -D -W lint-staged husky

husky でprecommitの設定

READMEにあるように、以下のコマンドでprecommitの設定をしていきます。

npm set-script prepare "husky install"
npm run prepare

今回はyarnを使ってprecommitのコマンドを設定します。

yarn husky add .husky/pre-commit "yarn lint-staged"

すると以下のようなファイルが生成されました。

.husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged

lint-stagedでeslintの設定

実は、lint-stagedを実行したいpackageに.lintstagedrc.jsonを設置するだけで完了です。

packages/frontend/.lintstagedrc.json
{
  "*.{js,jsx,ts,tsx}": "eslint --fix"
}

これで、対象のパッケージのみprecommitの設定ができるようになりました。

13
4
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
13
4