Coding Convention [3]: Prettier
Prerequisites
[1] Coding-Convention-1-Code-Formatter
Prettier
Prettier is a code formatter and a VSCode Extension.
Prettier is a JavaScript library that has become the most popular among code formatters and is almost becoming a standard.
Open source projects and many companies are using Prettier as their official code formatter. (Facebook, React, Jest, Yarn, Babel, Webpack, Dropbox, Storybook, Paypal, MongoDB, Salesforce, β¦)
The reason why Prettier has quickly become popular is that, unlike other existing code formatters, there is little room for configuration. In other words, it is difficult to significantly deviate from the default coding style in Prettier. == Ease to set!
This means that once you start using Prettier, you donβt need to argue with your team members about coding style!π
Install Prettier Library
The Prettier library is uploaded to the npm public repository as a package named prettier
.
Therefore, it can be easily installed and used in any JavaScript project through npm package manager.
Since Prettier is not needed for an application execution, we can install it as a development dependency using the -D
option.
1
$ npm i -D prettier
Prettier Setting
- Install IntelliJ IDE Plugins.
- Settings(
Ctrl + Alt + s
) > plugins > SearchPrettier
> Install > IDE restart - Extensions > Search
Pretter
> Install
- Settings(
Specifying the coding style
- Settings(
Ctrl + Alt + s
) > searchPrettier
> configuration - Manage with
.prettierrc.json
or.prettierrc.js
or etc. (Prettier configuration file) - File examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// .prettierrc.json
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"bracketSpacing": true,
"printWidth": 80
}
// prettier.config.js or .prettierrc.js
module.exports = {
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"bracketSpacing": true,
"printWidth": 80
}
- Setting for configuration path
- Config Path >
.prettierrc.json
- Config Path >
Setting for automatic sorting on save
Set the Default Formatter
to Prettier and then check the Format on Save
setting.
πΈ If the settings donβt work, try turning it off and then back on!