4
2

More than 3 years have passed since last update.

PHPでprettierを有効にしてみた

Last updated at Posted at 2021-03-11

はじめに

VSCode上でPHPを使用しているときにprettierを有効にする手順

参考サイト
https://github.com/prettier/plugin-php

手順

1.prettier+@prettier/plugin-phpインストール

npmコマンド

npm install --save-dev prettier @prettier/plugin-php
# or globally
npm install --global prettier @prettier/plugin-php

2.設定ファイル

.prettierrcをルートディレクトリに作成し次の内容を記述

{
  "singleQuote": true,
  "trailingComma": "all"
}

singleQuoteをTrueにすることによって、
ダブルクォーテーションの代わりシングルクォーテーションを使用するように設定する。

3.拡張機能を追加
VSCodeの拡張機能のRun on Saveをインストールします。

Setting.jsonに下記の内容を追記

  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": ".php$",
        "cmd": "prettier ${file} --write"
      },
    ]
  }

以上がPHPでprettierを有効にする手順です。

matchで様々なファイルを設定することができます。

また現在HTMLとPHPを含んだ、PHPファイルは整形することができません。
https://github.com/prettier/plugin-php/issues/845

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