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;
function onClickButton(event) {
// highlight-next-line
count += 1;
}
</script>
<button on:click={onClickButton}>Clicked {count}</button>
loading Svelte compiler...
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

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

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

function onClickButton(event) {
// highlight-next-line
$.set(count, $.get(count) + 1);
}

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

$.reset(button);
$.template_effect(() => $.set_text(text, `Clicked ${$.get(count) ?? ''}`));
$.event('click', button, onClickButton);
$.append($$anchor, button);
}
result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 117
    • end: 174
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (2)
        • Text {...}
          • type: "Text"
          • start: 116
          • end: 117
          • raw: "\n"
          • data: "\n"
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 117
          • end: 174
          • name: "button"
          • attributes: [...] (1)
            • OnDirective {...}
              • start: 125
              • end: 149
              • type: "OnDirective"
              • name: "click"
              • expression: Identifier {...}
                • type: "Identifier"
                • start: 135
                • end: 148
                • loc: {...}
                  • start: {...}
                    • line: 8
                    • column: 18
                    }
                  • end: {...}
                    • line: 8
                    • column: 31
                    }
                  }
                • name: "onClickButton"
                }
              • modifiers: []
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (2)
              • Text {...}
                • type: "Text"
                • start: 150
                • end: 158
                • raw: "Clicked "
                • data: "Clicked "
                }
              • ExpressionTag {...}
                • type: "ExpressionTag"
                • start: 158
                • end: 165
                • expression: Identifier {...}
                  • type: "Identifier"
                  • start: 159
                  • end: 164
                  • loc: {...}
                    • start: {...}
                      • line: 8
                      • column: 42
                      }
                    • end: {...}
                      • line: 8
                      • column: 47
                      }
                    }
                  • name: "count"
                  }
                }
              ]
            }
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 116
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 107
        • 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"
            }
          • FunctionDeclaration {...}
            • type: "FunctionDeclaration"
            • start: 28
            • end: 106
            • loc: {...}
              • start: {...}
                • line: 3
                • column: 2
                }
              • end: {...}
                • line: 6
                • column: 3
                }
              }
            • id: Identifier {...}
              • type: "Identifier"
              • start: 37
              • end: 50
              • loc: {...}
                • start: {...}
                  • line: 3
                  • column: 11
                  }
                • end: {...}
                  • line: 3
                  • column: 24
                  }
                }
              • name: "onClickButton"
              }
            • expression: false
            • generator: false
            • async: false
            • params: [...] (1)
              • Identifier {...}
                • type: "Identifier"
                • start: 51
                • end: 56
                • loc: {...}
                  • start: {...}
                    • line: 3
                    • column: 25
                    }
                  • end: {...}
                    • line: 3
                    • column: 30
                    }
                  }
                • name: "event"
                }
              ]
            • body: BlockStatement {...}
              • type: "BlockStatement"
              • start: 58
              • end: 106
              • loc: {...}
                • start: {...}
                  • line: 3
                  • column: 32
                  }
                • end: {...}
                  • line: 6
                  • column: 3
                  }
                }
              • body: [...] (1)
                • ExpressionStatement {...}
                  • type: "ExpressionStatement"
                  • start: 91
                  • end: 102
                  • loc: {...}
                    • start: {...}
                      • line: 5
                      • column: 4
                      }
                    • end: {...}
                      • line: 5
                      • column: 15
                      }
                    }
                  • expression: AssignmentExpression {...}
                    • type: "AssignmentExpression"
                    • start: 91
                    • end: 101
                    • loc: {...}
                      • start: {...}
                        • line: 5
                        • column: 4
                        }
                      • end: {...}
                        • line: 5
                        • column: 14
                        }
                      }
                    • operator: "+="
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 91
                      • end: 96
                      • loc: {...}
                        • start: {...}
                          • line: 5
                          • column: 4
                          }
                        • end: {...}
                          • line: 5
                          • column: 9
                          }
                        }
                      • name: "count"
                      }
                    • right: Literal {...}
                      • type: "Literal"
                      • start: 100
                      • end: 101
                      • loc: {...}
                        • start: {...}
                          • line: 5
                          • column: 13
                          }
                        • end: {...}
                          • line: 5
                          • column: 14
                          }
                        }
                      • value: 1
                      • raw: "1"
                      }
                    }
                  • leadingComments: [...] (1)
                    • Line {...}
                      • type: "Line"
                      • value: " highlight-next-line"
                      • start: 64
                      • end: 86
                      }
                    ]
                  }
                ]
              }
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
Compile Svelte in your Head - Hello World reactivity • Playground • Svelte