April 5, 2020
Use prettier to focus on the code and not on the formatting.
Prettier is a great tool for automatically formatting your code in a consistent way across your codebases, regardless of individual contributor preferences.
Prettier can take something like:
txt1function HelloWorld({greeting = "hello", greeted = 'World'}) {2if(!greeting){return null};34return greeting+" "+greeted;5}
and format it beautifully like
js1function HelloWorld({ greeting = "hello", greeted = "World" }) {2 if (!greeting) {3 return null;4 }56 return greeting + " " + greeted;7}
Add prettier to your project
shellnpm install prettier --save-dev --save-exact
Run against all your files
shellnpx prettier --write src/**/*
Run prettier automatically when committing files
shellnpm install --save-dev pretty-quick husky
Then add this config to package.json
:
package.json1{2 "husky": {3 "hooks": {4 "pre-commit": "pretty-quick --staged"5 }6 }7}
Further reading...