Svelte (21. 1. 25)

YunKuk Parkยท2022๋…„ 1์›” 25์ผ
2

svelte

๋ชฉ๋ก ๋ณด๊ธฐ
2/5

I. nvm ์‚ฌ์šฉ

๐Ÿ’ก ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ ๋งˆ๋‹ค node -version ์ด ์ƒ์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด์— ๋”ฐ๋ผ ์šฐ๋ฆฌ๋Š” node version ์„ ์ž์œ ๋กญ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

Install

1. nvm ์„ค์น˜

์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ terminal ์—์„œ ์‹คํ–‰

curl -o- [https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh](https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh) | bash

๊ณต์‹ ํ™ˆํŽ˜์ด์ง€: https://github.com/nvm-sh/nvm

2. ~/.zshrc ํŽธ์ง‘

vi ~/.zshrc    # ํŽธ์ง‘๊ธฐ open

## ์ตœ ํ•˜๋‹จ์— ์‚ฝ์ž…
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

3. source ~/.zshrc ์‹คํ–‰

4. nvm install --lts ์‹คํ–‰ํ•œ๋‹ค.

Usage

# version download
nvm install v16

# version use
nvm use v12

II. Why? pnpm

๐Ÿ’ก dependency๋ฅผ ํ•œ๊ณณ์—์„œ ๊ด€๋ฆฌํ•˜๋ฉด์„œ ๋™์ผ ๋ฒ„์ „์˜ ์ค‘๋ณต์ƒ์„ฑ์ด๋‚˜ ๊ฐ™์€ ์ด๋ฆ„ ๋‹ค๋ฅธ ๋ฒ„์ „์˜ dependency ์ƒ์„ฑ์‹œ ํŒจํ‚ค์ง€๊ฐ€ ์‚ญ์ œ๋˜์—ˆ๋‹ค๊ฐ€ ๋‹ค๋ฅธ ์œ„์น˜์— ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋“ฑ ๋ถˆํ•„์š”ํ•œ I/O ๊ณผ์ •์„ ์—†์• ์ค€๋‹ค.

pnpm ??

npm์„ ๋Œ€์ฒดํ•˜๊ธฐ ์œ„ํ•œ ๋” ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์ธ ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €

pnpm์€ ํŒจํ‚ค์น˜๋ฅผ ์„ค์น˜ ํ•  ๋•Œ ์ง์ ‘์ ์œผ๋กœ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋Œ€์‹  ์‹ฌ๋งํฌ(ํ•˜๋“œ๋งํฌ)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

ex) loadsh๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ‚ค์ง€ 100๊ฐœ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ, npm ์ด๋‚˜ yarn ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๊ฐ ํด๋”๋ณ„๋กœ ํŒจํ‚ค์ง€ lodash ํŒจํ‚ค์ง€๊ฐ€ 100๊ฐœ ๋ณต์‚ฌ
but ) pnpm์€ lodash๋ฅผ ๋‹จ ํ•œ๋ฒˆ ์„ค์น˜ํ•˜๊ณ , ๋‹ค๋ฅธ 100๊ฐœ์˜ ํ”„๋กœ์ ํŠธ์—๋Š” ์‹ฌ๋งํฌ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ, ์šฉ๋Ÿ‰ ์ธก๋ฉด์—์„œ ํšจ์œจ์ ์œผ๋กœ ํŒจํ‚ค์ง€๋ฅผ ๊ด€๋ฆฌ

pnpm, ํ”Œ๋žซํ•˜์ง€ ์•Š์€ ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €

III. npm init svelte@next my-app

Options :: Skeleton / TypeScript / EsLint, Prettier

IV. Route ๊ด€๋ จ

routes
โ”œโ”€โ”€ route_a
โ”‚   โ”œโ”€โ”€ index.svelte  ## ์ž๋™์œผ๋กœ index๋ฅผ ์ฐพ์œผ๋ฏ€๋กœ path๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค (url/route_a)
โ”‚   โ””โ”€โ”€ c.svelte      ## path: /route_a/c
โ”œโ”€โ”€ index.svelte      ## path: /
โ””โ”€โ”€ route_b.svelte    ## path: /route_b

V. Component ๊ด€๋ จ

๐Ÿ’ก ์ผ๋ฐ˜์ ์ธ Convention์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
.
**|____lib
| |____Name.svelte**
|____global.d.ts
|____app.html
|____routes
| |____index.svelte
| |____route_a
| | |____index.svelte
| | |____c.svelte
| |____route_b.svelte
| |____route_b
| | |____[id].svelte

src ํด๋” ๋‚ด์— lib ๋ผ๋Š” ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  Component ๋ฅผ ์ œ์ž‘ํ•œ๋‹ค.

import ์‹œ์—๋Š” $lib/Name.svelte ๋ฅผ ํ†ตํ•ด import ํ•œ๋‹ค.

๋”ฐ๋กœ js(ts)config.json ์—์„œ base url ์„ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์•„๋„ $ ๋ฅผ ํ†ตํ•ด์„œ baseUrl ์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ณต์‹๋ฌธ์„œ

$lib


This is a simple alias toย src/lib, or whatever directory is specified as [config.kit.files.lib]. It allows you to access common components and utility modules withoutย ../../../../ย nonsense.

VI. How Management QueryParam & PathParam

๐Ÿ’ก ํ•ด๋‹น ๋‚ด์šฉ์€ routes ์˜ page ์ชฝ์—์„œ๋งŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

<script context="module">
  /** @type {import('@sveltejs/kit').Load} */
  export async function load({ url, params }) {
    const query = url.searchParams;
    const id = params.id;
      return {
        props: { a: query.get('a'), id }
      };
  }
</script>

<script>
  export let a;
  export let id;
</script>

<div>
  {a}
  {id}
</div> 

1. QueryParam

  1. queryString ๊ฐ™์€ ๊ฒฝ์šฐ url.searchParams ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ „์ฒด query๋ฅผ ๊ธ์–ด์˜ค๊ณ 
  2. return ์ธก์— props: { a: query.get(โ€™aโ€™) } ๋ฅผ ํ†ตํ•ด์„œ .....url?a=123123 ์˜ a์ชฝ value๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.

2. PathParam

  1. pathparam ์„ ์‚ฌ์šฉํ•˜๋Š” ์ชฝ์˜ ํŒŒ์ผ๋ช…์€ [pathparam].svelte ๋กœ ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  2. [params.id](http://params.id) ๋ฅผ ํ†ตํ•ด์„œ ํ˜„์žฌ path๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ 
  3. ๊ทธ id๋ฅผ return์ธก props์—๊ฒŒ ์•Œ๋ ค์ค€๋‹ค.

๋งˆ๋ฌด๋ฆฌ

์Šค๋ฒจํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” React ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๋”ฐ๋กœ ์„ค์น˜๋‚˜ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ–ˆ๋˜ ๋ถ€๋ถ„์„ ์กฐ๊ธˆ ๊ฐ„์†Œํ™” ํ•œ ๋Š๋‚Œ์ด ๋“ ๋‹ค.

์ด๋ฏธ ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋– ํ•œ๊ฒŒ ํ•„์š”ํ•œ์ง€ ์•Œ๊ณ  ์žˆ๊ณ , ๊ทธ ๊ฒƒ์— ๋”ฐ๋ผ์„œ ์„ค๊ณ„ํ•œ ๋Š๋‚Œ์ด ๋“ ๋‹ค.

profile
( โ€ข .ฬฎ โ€ข)โ—žโธ’โธ’

0๊ฐœ์˜ ๋Œ“๊ธ€