Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
let count = 0;
const increment = () => {
count += 1;
};
</script>

<button class="button" on:click={increment}>
Count is {count}
</button>
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var root = $.template(`<button class="button"> </button>`);

export default function App($$anchor) {
let count = $.mutable_state(0);

const increment = () => {
$.set(count, $.get(count) + 1);
};

var button = root();
var text = $.child(button);

$.reset(button);
$.template_effect(() => $.set_text(text, `Count is ${$.get(count) ?? ''}`));
$.event('click', button, increment);
$.append($$anchor, button);
}
result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 88
    • end: 161
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (2)
        • Text {...}
          • type: "Text"
          • start: 86
          • end: 88
          • raw: "\n\n"
          • data: "\n\n"
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 88
          • end: 161
          • name: "button"
          • attributes: [...] (2)
            • Attribute {...}
              • type: "Attribute"
              • start: 96
              • end: 110
              • name: "class"
              • value: [...] (1)
                • Text {...}
                  • start: 103
                  • end: 109
                  • type: "Text"
                  • raw: "button"
                  • data: "button"
                  }
                ]
              }
            • OnDirective {...}
              • start: 111
              • end: 131
              • type: "OnDirective"
              • name: "click"
              • expression: Identifier {...}
                • type: "Identifier"
                • start: 121
                • end: 130
                • loc: {...}
                  • start: {...}
                    • line: 9
                    • column: 33
                    }
                  • end: {...}
                    • line: 9
                    • column: 42
                    }
                  }
                • name: "increment"
                }
              • modifiers: []
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (3)
              • Text {...}
                • type: "Text"
                • start: 132
                • end: 144
                • raw: "Count is "
                • data: "Count is "
                }
              • ExpressionTag {...}
                • type: "ExpressionTag"
                • start: 144
                • end: 151
                • expression: Identifier {...}
                  • type: "Identifier"
                  • start: 145
                  • end: 150
                  • loc: {...}
                    • start: {...}
                      • line: 10
                      • column: 12
                      }
                    • end: {...}
                      • line: 10
                      • column: 17
                      }
                    }
                  • name: "count"
                  }
                }
              • Text {...}
                • type: "Text"
                • start: 151
                • end: 152
                • raw: "\n"
                • data: "\n"
                }
              ]
            }
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 86
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 77
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 7
            • column: 0
            }
          }
        • body: [...] (2)
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 11
            • end: 25
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 2
                }
              • end: {...}
                • line: 2
                • column: 16
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 15
                • end: 24
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 6
                    }
                  • end: {...}
                    • line: 2
                    • column: 15
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 15
                  • end: 20
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 6
                      }
                    • end: {...}
                      • line: 2
                      • column: 11
                      }
                    }
                  • name: "count"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 23
                  • end: 24
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 14
                      }
                    • end: {...}
                      • line: 2
                      • column: 15
                      }
                    }
                  • value: 0
                  • raw: "0"
                  }
                }
              ]
            • kind: "let"
            }
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 30
            • end: 76
            • loc: {...}
              • start: {...}
                • line: 4
                • column: 2
                }
              • end: {...}
                • line: 6
                • column: 4
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 36
                • end: 75
                • loc: {...}
                  • start: {...}
                    • line: 4
                    • column: 8
                    }
                  • end: {...}
                    • line: 6
                    • column: 3
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 36
                  • end: 45
                  • loc: {...}
                    • start: {...}
                      • line: 4
                      • column: 8
                      }
                    • end: {...}
                      • line: 4
                      • column: 17
                      }
                    }
                  • name: "increment"
                  }
                • init: ArrowFunctionExpression {...}
                  • type: "ArrowFunctionExpression"
                  • start: 48
                  • end: 75
                  • loc: {...}
                    • start: {...}
                      • line: 4
                      • column: 20
                      }
                    • end: {...}
                      • line: 6
                      • column: 3
                      }
                    }
                  • id: null
                  • expression: false
                  • generator: false
                  • async: false
                  • params: []
                  • body: BlockStatement {...}
                    • type: "BlockStatement"
                    • start: 54
                    • end: 75
                    • loc: {...}
                      • start: {...}
                        • line: 4
                        • column: 26
                        }
                      • end: {...}
                        • line: 6
                        • column: 3
                        }
                      }
                    • body: [...] (1)
                      • ExpressionStatement {...}
                        • type: "ExpressionStatement"
                        • start: 60
                        • end: 71
                        • loc: {...}
                          • start: {...}
                            • line: 5
                            • column: 4
                            }
                          • end: {...}
                            • line: 5
                            • column: 15
                            }
                          }
                        • expression: AssignmentExpression {...}
                          • type: "AssignmentExpression"
                          • start: 60
                          • end: 70
                          • loc: {...}
                            • start: {...}
                              • line: 5
                              • column: 4
                              }
                            • end: {...}
                              • line: 5
                              • column: 14
                              }
                            }
                          • operator: "+="
                          • left: Identifier {...}
                            • type: "Identifier"
                            • start: 60
                            • end: 65
                            • loc: {...}
                              • start: {...}
                                • line: 5
                                • column: 4
                                }
                              • end: {...}
                                • line: 5
                                • column: 9
                                }
                              }
                            • name: "count"
                            }
                          • right: Literal {...}
                            • type: "Literal"
                            • start: 69
                            • end: 70
                            • loc: {...}
                              • start: {...}
                                • line: 5
                                • column: 13
                                }
                              • end: {...}
                                • line: 5
                                • column: 14
                                }
                              }
                            • value: 1
                            • raw: "1"
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            • kind: "const"
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
Counter • Playground • Svelte