in ,

The Svelte Compiler Handbook, Hacker News

The Svelte Compiler Handbook, Hacker News

Who is this for?

Anyone who

  • is interested in the Svelte compilation process
  • wants to get started in reading Svelte source code
  • Overview

               

  •   OverviewOverview     

    The Svelte compilation process can be broken down into 4-steps

  • Parsing source code into Abstract Syntax Tree (AST)
  • Tracking references and dependencies
  • Creating code blocks and fragments
  • Generate code
  • Which sums out by the following pseudocode:
    const (source =fs readFileSync ‘App.svelte’

    const ast (= parse ( ( )

  • (

    const component (=new (Component)

    ( ast

    )

    const renderer

    =  options

    . generate===(‘ssr’ (SSRRenderer)

    ( component

    )

    DomRenderer

    (component

    ) Overview

    const (js

  • ) , css } = renderer . render .
  • fs (writeFileSync Overview (
  • ‘App.js’
  • , js ) fs (writeFileSync Overview (
  • ‘App.css’ , css )
    1. Parsing source code into AST

          

        

  •        

    const ast (= parse ( ( )

  • (

  • The Svelte syntax is a superset of HTML. Svelte implements its own parser for the Svelte syntax, which handles:

  • HTML syntax
  • Overview
  • Curly brackets
    {data}

  • Logic blocks
    {# each list as item}

  • The The Svelte parser handles specially for tag, it uses Step 1 acorn to parse the content within the tag. When the parser sees a
  • tag, it uses