-
You will need an existing or new project to use Prettier
-
To learn Prettier with a dummy project, create an
example-code
directory with the following file extensions ie:
example-code
┣ test.css
┣ test.js
┣ test.ts
┗ test.tsx
-
Install Prettier VSCode extension
- via the GUI -> Search for the "Prettier" extension in the Extensions Tab in VSCode
- via the CLI ->
code --install-extension esbenp.prettier-vscode
-
Integrate project with Prettier extension and VSCode via
.vscode
config directory -
Create
.vscode
directory at the root of your project with a file calledsettings.json
with the following contents ie:
{
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true
}
- Your project should now look like this:
.vscode
┣ settings.json
example-code
┣ test.css
┣ test.js
┣ test.ts
┗ test.tsx
- Customize Prettier behavior via the
.prettierrc.js
file at the root of your project with the following contents ie:
module.exports = {
overrides: [
{
files: "*.js",
options: {
semi: false,
trailingComma: "es5",
singleQuote: true,
},
},
{
files: ["*.css"],
options: {
tabWidth: 4,
},
},
],
};
- Your project should now look like this:
.vscode
┣ settings.json
example-code
┣ test.css
┣ test.js
┣ test.ts
┗ test.tsx
.prettierrc.js