🌐 Web Development

Svelte

Svelte Framework

A UI framework that compiles to JavaScript at build time. Manipulates the DOM directly without a Virtual DOM.

πŸ“– Detailed Explanation

Svelte is a compiler-based UI framework developed by Rich Harris. Unlike React and Vue, it is compiled into pure JavaScript at build time, without a runtime framework. By manipulating the DOM directly without Virtual DOM diffing, it provides a small bundle size and excellent runtime performance.

Svelte's core philosophy is "Less code, more done." Reactivity is supported at the language level, so reactive declarations are possible with just the $ symbol. Simply writing let count = 0; makes a reactive variable, and $: doubled = count * 2; automatically calculates a derived value. Hooks such as useState and useEffect are not needed.

Svelte 5 (2024) introduced a new reactivity system called Runes. With runes such as $state(), $derived(), and $effect(), more explicit reactivity control is possible. Coexisting with the existing $ syntax, it has become easy to reuse reactive logic outside of components as well.

SvelteKit is Svelte's full-stack framework, comparable to Next.js. It provides file-based routing, SSR/SSG/ISR, API Routes, and a layout system. Vercel has hired Rich Harris, the creator of Svelte, and actively supports Svelte.

πŸ’» Code Example

<!-- Svelte 5 Runes 예제 -->
<script lang="ts">
  // $state - λ°˜μ‘ν˜• μƒνƒœ
  let count = $state(0);
  let name = $state('');
  let items = $state<string[]>([]);

  // $derived - νŒŒμƒ κ°’ (μžλ™ 계산)
  const doubled = $derived(count * 2);
  const isValid = $derived(name.length >= 3);

  // $effect - μ‚¬μ΄λ“œ μ΄νŽ™νŠΈ (useEffect λŒ€μ²΄)
  $effect(() => {
    console.log(`Count changed: ${count}`);
    // 클린업 ν•¨μˆ˜ λ°˜ν™˜ κ°€λŠ₯
    return () => console.log('Cleanup');
  });

  // 이벀트 ν•Έλ“€λŸ¬
  function increment() {
    count++;  // 직접 λ³€κ²½ κ°€λŠ₯
  }

  function addItem() {
    if (name) {
      items.push(name);  // λ°°μ—΄ 직접 μˆ˜μ • κ°€λŠ₯
      name = '';
    }
  }

  // Props μ •μ˜ (Svelte 5)
  interface Props {
    title: string;
    onSave?: (data: string) => void;
  }
  let { title, onSave }: Props = $props();
</script>

<!-- ν…œν”Œλ¦Ώ - HTMLκ³Ό 거의 동일 -->
<div class="container">
  <h1>{title}</h1>

  <button onclick={increment}>
    Count: {count} (doubled: {doubled})
  </button>

  <input
    bind:value={name}
    placeholder="이름 μž…λ ₯"
    class:invalid={!isValid}
  />

  {#if isValid}
    <button onclick={addItem}>μΆ”κ°€</button>
  {/if}

  <ul>
    {#each items as item, index (item)}
      <li>
        {index + 1}. {item}
        <button onclick={() => items.splice(index, 1)}>μ‚­μ œ</button>
      </li>
    {:else}
      <li>ν•­λͺ©μ΄ μ—†μŠ΅λ‹ˆλ‹€</li>
    {/each}
  </ul>
</div>

<style>
  /* μ»΄ν¬λ„ŒνŠΈ μŠ€μ½”ν”„ CSS (μžλ™ μŠ€μ½”ν•‘) */
  .container {
    padding: 1rem;
  }

  .invalid {
    border-color: red;
  }

  button {
    background: #ff3e00;
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
  }
</style>

<!-- μž¬μ‚¬μš© κ°€λŠ₯ν•œ λ°˜μ‘ν˜• 둜직 (Svelte 5) -->
<!-- lib/counter.svelte.ts -->
<script context="module" lang="ts">
export function createCounter(initial = 0) {
  let count = $state(initial);

  return {
    get count() { return count; },
    increment: () => count++,
    decrement: () => count--,
    reset: () => count = initial
  };
}
</script>

πŸ—£οΈ Examples of Practical Conversations

When choosing a framework:

"This is an embed widget where bundle size matters. React is over 30KB by default, but with Svelte only the component is bundled, so it's much smaller. We don't need complex state management either, and concise code is an advantage."

In a technical interview:

"Why is Svelte faster than React?" - "There is no Virtual DOM diffing. At compile time, Svelte determines which DOM to update when which variable changes. Because there are no comparison operations at runtime, it's fast, and no framework code is included in the bundle."

In a team tech stack discussion:

"Won't hiring be difficult if we adopt Svelte?" - "The ecosystem is small, but it's easy to learn. If you have React experience, you can adapt in a few days. The syntax is intuitive, so onboarding is actually faster. However, if you need complex libraries, the React ecosystem is more advantageous."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Cautions

πŸ”— Related Terms

SvelteKit React Vue Virtual DOM Signals

πŸ“š Learn More

πŸ“„ Svelte Official Documentation πŸ“„ Svelte Interactive Tutorial πŸ“„ SvelteKit Documentation