rimo030 / nestjs-e-commerce-frame

โœ๏ธ NestJS๋กœ ๊ตฌํ˜„ํ•œ Commerce API
47 stars 1 forks source link

prettier-plugin-sort-imports ์ ์šฉํ•˜๊ธฐ #48

Open rimo030 opened 1 year ago

rimo030 commented 1 year ago

๐Ÿ“˜prettier-plugin-sort-imports

์ œ๊ณต๋œ ์ •๊ทœ์‹ ์ˆœ์„œ์— ๋”ฐ๋ผ import ์„ ์–ธ์„ ์ •๋ ฌํ•ด์ฃผ๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์ด๋‹ค.

prettier-plugin-sort-imports ์„ค์น˜

npm install --save-dev @trivago/prettier-plugin-sort-imports

prettier config file ์ž‘์„ฑ

prettierrc์— ๋‹ค์Œ์„ ์ž‘์„ฑํ•œ๋‹ค.

"plugins": ["@trivago/prettier-plugin-sort-imports"],
"importOrder": ["^@nestjs/(.*)$", "^src/(.*)$", "^[./]"],
"importOrderParserPlugins": ["typescript", "decorators-legacy"]

importOrder

๋ฌธ์ž์—ด ์ •๊ทœ์‹์„ ์ž…๋ ฅ๋ฐ›๊ณ , ์ œ๊ณต๋˜๋Š” ์ •๊ทœ์‹ ์ˆœ์„œ๋กœ ์ •๋ ฌ์„ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด, @nestjs/... src/... ๊ทธ ์™ธ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ฒŒ ๋œ๋‹ค. ํŠน์ • import๋ฌธ์„ ๋„ฃ๊ณ ์‹ถ๋‹ค๋ฉด ์ ์ ˆํ•œ ์œ„์น˜์— ์ •๊ทœ์‹์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

importOrderParserPlugins

A collection of plugins for babel parser. The plugin passes this list to babel parser, so it can understand the syntaxes used in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails, you can use this field to enforce the usage of the plugins' babel parser needs.

๊ธฐ๋ณธ์ ์œผ๋กœ prettier๋ฅผ ์ด์šฉํ•ด ๊ตฌ๋ฌธ์„ ์ธ์‹ํ•˜์ง€๋งŒ ์ž˜ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ด ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

prettier ๊ธฐ๋Šฅ ์ ์šฉํ•˜๊ธฐ

npm run format

์ฐธ๊ณ ์ž๋ฃŒ

kakasoo commented 1 year ago

์•„์ง ๋‚ด์šฉ์ด ๋น„์–ด์žˆ์ง€๋งŒ ์ถ”ํ›„์— ๊ฐ™์ด ์–˜๊ธฐํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฑฐ ๊ฐ™์•„์„œ ๋งํฌ๋งŒ ๊ฑธ์–ด๋‘ก๋‹ˆ๋‹ค.

kakasoo commented 1 year ago
image
{
    "editor.codeActionsOnSave": {
        "editor.formatOnSave": true,
        "source.organizeImports": true // ์ด ๋ถ€๋ถ„
    },
}

source.organizeImports ์˜ต์…˜์ด prettier์ฒ˜๋Ÿผ import ์ˆœ์„œ๋ฅผ ์กฐ์ž‘ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฑฐ ๊ฐ™์•„์š”. VSCode๋ฉด IDE์—์„œ๋„ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฑฐ ๊ฐ™๊ตฐ์š”.