Prettierとは
コードフォーマッターのこと
コマンドを叩くとコードを綺麗にしてくれるもの。
installation
package.jsonを生成し、prettierをインストールする。
terminal
$ npm init -y
$ npm install prettier
scriptsに fmtコマンドを記述する。
package.json
{
"name": "Prettier",
"scripts": {
"fmt": "prettier --write src/**.js"
},
"dependencies": {
"prettier": "^1.19.1"
}
}
綺麗にしたいJSファイルを用意する。
src/app.js
function hello(text){
console.log(text + "!")} hello("Hello")
実際に綺麗にする
terminal
$ npm run fmt
src/app.js
function hello(text) {
console.log(text + "!");
}
hello("Hello");
こんな感じに綺麗にしてくれます。
おわり
今回使用したコードはこちら
ESLintの記事はこちら
ESLintとPrettierの併用こちら