1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravelにstylelint導入

Last updated at Posted at 2020-01-07

前提

  • Laravelのプロジェクトにstylelintを導入する
  • プロジェクトのCSSはSCSSで書かれている
    → LaravelにSCSSを導入する方法はこちらの記事を参考にしてください。

    LaravelにSCSSを導入する方法

stylelintでできること

コードの一貫性を保ち、エラーを減らすことが可能になる


構文エラーチェックの例だと

.sample {
  background-color: #fffffff;
  disply: block;
}

無効な16進数のカラーコードとdisplayのスペルミスがあります。stylelintのcolor-no-invalid-hexルールとproperty-no-unknownルールでエラーを発見することができます。

その他にもスタイルの重複を避けることができたり、書き方を統一できたり、stylelintを導入すると様々なメリットがあります。

stylelintのルールについて

stylelintの導入

一番簡単な方法は、プロジェクトのルートディレクトリに.stylelintrcファイルを作ることです。
作成した.stylelintrcにルールを設定していきます。

例)

{
  "rules": {
    "color-named": "never",
    "declaration-no-important": true,
    "function-url-quotes": "never",
    "number-leading-zero": "never",
    "property-no-vendor-prefix": true,
    "selector-max-type": 0,
    "selector-no-qualifying-type": true,
    "string-quotes": "single"
  }
}

git commit時にstylelintのルールを適用する

ルートディレクトリのpackage.jsonにコードを追加していく。

package.json
{
  "scripts": {
    "stylelint": "node ./node_modules/stylelint/bin/stylelint.js  resources/assets/sass/*.scss",
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "resources/assets/sass/*.scss": [
      "npm run stylelint",
      "git add"
    ]
  }
}

huskylint-stagedのパッケージをインストールするのを忘れないでくださいね^^

参考記事

チームで美しくメンテナブルなCSSを書くための 「Stylelint」導入のすすめ

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?