IDE/VSCode

[VS Code] ๐Ÿ’ฝ Prettier ๋„๊ตฌ ์„ค์ •๋ฒ• - ํ•œ๋ฐฉ ์ฝ”๋“œ ํฌ๋งทํ„ฐ

์ธํŒŒ_ 2021. 9. 22. 07:00

Prettier-๋„๊ตฌ-์„ค์ •

VSCode - Prettier 

Visual Studio Code์—์„œ๋Š” ์‚ฌ์šฉ์ž ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ฝ”๋“œ์˜ ์ค„ ๊ฐ„๊ฒฉ์ด๋‚˜ ํ–‰์„ ์ž๋™์œผ๋กœ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•ด์ฃผ๋Š” ํ™•์žฅํ”„๋กœ๊ทธ๋žจ์ด ์กด์žฌํ•œ๋‹ค.

์„ค์น˜๋ฅผ ์™„๋ฃŒํ•˜๋ฉด, ์—๋””ํ„ฐ ๋ฐ‘๋ถ€๋ถ„์— Prettier: โœ” ํ‘œ์‹œ๊ฐ€ ๋˜์–ด์žˆ๋‹ค๋ฉด ํ™œ์„ฑํ™”๋œ ๊ฒƒ์ด๋‹ค.

Prettier-๋„๊ตฌ-์„ค์ •

 

Prettier - Code formatter - Visual Studio Marketplace

Extension for Visual Studio Code - Code formatter using prettier

marketplace.visualstudio.com


Prettier ์„ค์ •

 

Format on Save ํ™•์ธ

  • vscode ์„ค์ •์—์„œ format on save๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋‚˜์˜ค๋Š” ์„ค์ •์— ์ฒดํฌ๋˜์–ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•œ๋‹ค.
  •  ์ด ๊ธฐ๋Šฅ์€ ํ•ญ์ƒ ์ €์žฅ์„ ํ• ๋•Œ๋งˆ๋‹ค, ์•„์ฃผ ์ด์˜์žฅํ•˜๊ฒŒ ํฌ๋งท์„ ์ €์ ˆ๋กœ ์ž๋™์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค.

Prettier-๋„๊ตฌ-์„ค์ •

 

Default Formatter ํ™•์ธ

  • ๊ฐ„ํ˜น ์—๋””ํ„ฐ์˜ default๋กœ ์„ค์ •๋˜์–ด ์žˆ๋Š” formatter๊ฐ€ null ์ด๊ฑฐ๋‚˜ ์„ค์ •์ด ์ž˜๋ชป๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. 
  • ์„ค์ •์ด prettier format (esbenp.prettier-vscode) ๋กœ ์„ค์ •๋˜์–ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

Prettier-๋„๊ตฌ-์„ค์ •

 

Prettier ์˜ต์…˜ ์†์„ฑ

{
  // ํ•œ ์ค„์ด ์ด ๊ธ€์ž์ˆ˜๋ฅผ ๋„˜๊ฒŒ ๋˜๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ๋˜์–ด ์ฝ”๋“œ๊ฐ€ ์„ธ๋กœ๋กœ ์ •๋ฆฌ๋œ๋‹ค. (default: 80)
  "printWidth": 120,
  
  // ํƒญ ๋„ˆ๋น„ (default: 2)
  "tabWidth": 3,
  
  // ํƒญ ์‚ฌ์šฉ ์—ฌ๋ถ€. ์ฐธ์ด๋ฉด ํƒญ์ด์žˆ๋Š” ์ค„์„ ๋“ค์—ฌ ์“ฐ๊ธฐ ํ•œ๋‹ค. (default: false)
  "useTabs": false,
  
  // ์„ธ๋ฏธํด๋ก  ๊ฐ•์ œ ์—ฌ๋ถ€ (default: true)
  "semi": true,
  
  // ํ™‘๋”ฐ์˜ดํ‘œ๋ฅผ ์“ธ๊ฑด์ง€ ์„ค์ •. ๊ธฐ๋ณธ๊ฐ’์€ ์Œ๋”ฐ์˜ดํ‘œ. ์ฝ”๋”ฉ์„ ํ•˜๋ฉด์„œ ํ™‘๋”ฐ์˜ดํ‘œ๋ฅผ ์ผ๋‹ค๋ฉด ๊ฐ•์ œ๋กœ ์Œ๋”ฐ์˜ดํ‘œ๋กœ ๋ณ€๊ฒฝ. (default: false)
  "singleQuote": true,
  
  /* 
  ๊ฐ์ฒด, ๋ฐฐ์—ด, ํ•จ์ˆ˜ ๋“ฑ์˜ ํ›„ํ–‰์— ์‰ผํ‘œ๋ฅผ ์ฐ์„์ง€ ์ œ์–ด
  "none" - ์‰ผํ‘œ๋ฅผ ๋ถ™ํžˆ์ง€ ์•Š์Œ
  "es5" - ES5์—์„œ ์œ ํšจํ•œ ํ›„ํ–‰ ์‰ผํ‘œ ๋ถ™ํž˜ (๊ฐ์ฒด, ๋ฐฐ์—ด ๋“ฑ)
  "all" - ๊ฐ€๋Šฅํ•˜๋ฉด ํ›„ํ–‰ ์‰ผํ‘œ๋ฅผ ๋ถ™ํž˜ (ํ•จ์ˆ˜ ์ธ์ˆ˜) 
  */
  "trailingComma": "all",
  
  // ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ์‚ฌ์šฉ์‹œ ๊ด„ํ˜ธ์— ๊ณต๋ฐฑ ์‚ฝ์ž… ์—ฌ๋ถ€ (default: true)
  "bracketSpacing": true,
  
  // JSX์˜ ๋งˆ์ง€๋ง‰ `>`๋ฅผ ๋‹ค์Œ ์ค„๋กœ ๋‚ด๋ฆด์ง€ ์—ฌ๋ถ€ 
  "jsxBracketSameLine": true,
  
  // ๋‹จ๋… ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ฃผ์œ„์— ๊ด„ํ˜ธ๋ฅผ ์ž๋™์œผ๋กœ ๋ถ™ํž˜ (default: 'avoid')
  "arrowParens": "avoid",
  
  // vue ํŒŒ์ผ์˜ script์™€ styleํƒœ๊ทธ ๋“ค์—ฌ์“ฐ๊ธฐ ์—ฌ๋ถ€
  "vueIndentScriptAndStyle": false,
  
  // ๋งจ๋งˆ์ง€๋ง‰ ์ค„ ๋„ฃ๋Š”์ง€ ์—ฌ๋ถ€
  "endOfLine": "auto",
  
  // ์ด ํ™•์žฅ ๊ธฐ๋Šฅ์„ ๋น„ํ™œ์„ฑํ™” ํ•  ์–ธ์–ด ID ๋ชฉ๋ก
  "disableLanguages" : ["vue"]
}

 

Prettier ์„ค์ • ํŒŒ์ผ ์šฐ์„  ์ˆœ์œ„

Prettier์˜ ์„ค์ • ํŒŒ์ผ์€ ์•„๋ž˜์˜ ์šฐ์„ ์ˆœ์œ„๋กœ ์ ์šฉ์ด ๋œ๋‹ค.

  1. ๋จผ์ € settings.json์— ์„ค์ •ํ•œ ์„ธํŒ…๊ฐ’์„ ์ ์šฉํ•œ๋‹ค.
  2. ๋งŒ์•ฝ ํ”„๋กœ์ ํŠธ์— EditorConfig์˜ ์„ค์ •ํŒŒ์ผ์ธ .editorconfig๊ฐ€ ์žˆ์œผ๋ฉด ์ด ์„ค์ •์ด ๋ฎ์–ด์“ด๋‹ค.
  3. ๋งˆ์ง€๋ง‰์œผ๋กœ Prettier์˜ ๊ณ ์œ ํ•œ ์„ค์ •ํŒŒ์ผ์ธ .prettierrc๊ฐ€ ํ”„๋กœ์ ํŠธ์— ์žˆ์œผ๋ฉด ์ด ๊ฐ’์„ ์ตœ์ข…์ ์œผ๋กœ ์ ์šฉํ•˜๊ฒŒ ๋œ๋‹ค

VScode์˜ settings.json์— ํ”„๋ฆฌํ‹ฐ์–ด ์†์„ฑ๊ฐ’์„ ์„ค์ •ํ•  ๊ฒฝ์šฐ, ๊ธฐ๋ณธ์„ค์ •๊ณผ ๋‹ฌ๋ฆฌ ์„ธํŒ…๊ฐ’์˜ ๋ง๋จธ๋ฆฌ์— prettier.๋ฅผ ์ผ์ผํžˆ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

// .settings.json
{
  "prettier.useEditorConfig": false,
  "prettier.printWidth": 120,
  "prettier.tabWidth": 3,
  "prettier.useTabs": false,
  "prettier.semi": true,
  "prettier.singleQuote": true,
  "prettier.trailingComma": "all",
  "prettier.bracketSpacing": true,
  "prettier.jsxBracketSameLine": true,
  "prettier.arrowParens": "avoid",
  "prettier.vueIndentScriptAndStyle": false,
  "prettier.endOfLine": "auto"
}

 

๋งŒ์ผ ๋กœ์ปฌ์— ์žˆ๋Š” ํ”„๋ฆฌํ‹ฐ์–ด ์„ค์ •์„ ๋ฌด์‹œํ•˜๊ณ , ํ”„๋กœ์ ํŠธ ์„ค์ •์— ์žˆ๋Š” ์ปจ๋ฒค์…˜์— ๋งž๊ฒŒ ํ”„๋ฆฌํ‹ฐ์–ด๊ฐ€ ์ž‘๋™ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด, ๊ฐœ๋ฐœํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— .prettierrc.json๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

// .prettierrc.json
{
  "printWidth": 120,
  "tabWidth": 3,
  "useTabs": false,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "all",
  "bracketSpacing": true,
  "jsxBracketSameLine": true,
  "arrowParens": "avoid",
  "vueIndentScriptAndStyle": false,
  "endOfLine": "auto"
}