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 loggedIn = false;

function toggle() {
loggedIn = !loggedIn;
}
</script>

{#if loggedIn}
<button on:click={toggle}>
Log out
</button>
{:else}
<button on:click={toggle}>
Log in
</button>
{/if}
resolving svelte/internal/client
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var root_1 = $.template(`<button>Log out</button>`);
var root_2 = $.template(`<button>Log in</button>`);

export default function App($$anchor) {
let loggedIn = $.mutable_state(false);

function toggle() {
$.set(loggedIn, !$.get(loggedIn));
}

var fragment = $.comment();
var node = $.first_child(fragment);

{
var consequent = ($$anchor) => {
var button = root_1();

$.event('click', button, toggle);
$.append($$anchor, button);
};

var alternate = ($$anchor) => {
var button_1 = root_2();

$.event('click', button_1, toggle);
$.append($$anchor, button_1);
};

$.if(node, ($$render) => {
if ($.get(loggedIn)) $$render(consequent); else $$render(alternate, false);
});
}
result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 92
    • end: 217
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (2)
        • Text {...}
          • type: "Text"
          • start: 90
          • end: 92
          • raw: "\n\n"
          • data: "\n\n"
          }
        • IfBlock {...}
          • type: "IfBlock"
          • elseif: false
          • start: 92
          • end: 217
          • test: Identifier {...}
            • type: "Identifier"
            • start: 97
            • end: 105
            • loc: {...}
              • start: {...}
                • line: 9
                • column: 5
                }
              • end: {...}
                • line: 9
                • column: 13
                }
              }
            • name: "loggedIn"
            }
          • consequent: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (3)
              • Text {...}
                • type: "Text"
                • start: 106
                • end: 108
                • raw: "\n\t"
                • data: "\n\t"
                }
              • RegularElement {...}
                • type: "RegularElement"
                • start: 108
                • end: 155
                • name: "button"
                • attributes: [...] (1)
                  • OnDirective {...}
                    • start: 116
                    • end: 133
                    • type: "OnDirective"
                    • name: "click"
                    • expression: Identifier {...}
                      • type: "Identifier"
                      • start: 126
                      • end: 132
                      • loc: {...}
                        • start: {...}
                          • line: 10
                          • column: 19
                          }
                        • end: {...}
                          • line: 10
                          • column: 25
                          }
                        }
                      • name: "toggle"
                      }
                    • modifiers: []
                    }
                  ]
                • fragment: Fragment {...}
                  • type: "Fragment"
                  • nodes: [...] (1)
                    • Text {...}
                      • type: "Text"
                      • start: 134
                      • end: 146
                      • raw: "Log out"
                      • data: "Log out"
                      }
                    ]
                  }
                }
              • Text {...}
                • type: "Text"
                • start: 155
                • end: 156
                • raw: "\n"
                • data: "\n"
                }
              ]
            }
          • alternate: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (3)
              • Text {...}
                • type: "Text"
                • start: 163
                • end: 165
                • raw: "\n\t"
                • data: "\n\t"
                }
              • RegularElement {...}
                • type: "RegularElement"
                • start: 165
                • end: 211
                • name: "button"
                • attributes: [...] (1)
                  • OnDirective {...}
                    • start: 173
                    • end: 190
                    • type: "OnDirective"
                    • name: "click"
                    • expression: Identifier {...}
                      • type: "Identifier"
                      • start: 183
                      • end: 189
                      • loc: {...}
                        • start: {...}
                          • line: 14
                          • column: 19
                          }
                        • end: {...}
                          • line: 14
                          • column: 25
                          }
                        }
                      • name: "toggle"
                      }
                    • modifiers: []
                    }
                  ]
                • fragment: Fragment {...}
                  • type: "Fragment"
                  • nodes: [...] (1)
                    • Text {...}
                      • type: "Text"
                      • start: 191
                      • end: 202
                      • raw: "Log in"
                      • data: "Log in"
                      }
                    ]
                  }
                }
              • Text {...}
                • type: "Text"
                • start: 211
                • end: 212
                • raw: "\n"
                • data: "\n"
                }
              ]
            }
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 90
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 81
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 7
            • column: 0
            }
          }
        • body: [...] (2)
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 10
            • end: 31
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 1
                }
              • end: {...}
                • line: 2
                • column: 22
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 14
                • end: 30
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 5
                    }
                  • end: {...}
                    • line: 2
                    • column: 21
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 14
                  • end: 22
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 5
                      }
                    • end: {...}
                      • line: 2
                      • column: 13
                      }
                    }
                  • name: "loggedIn"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 25
                  • end: 30
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 16
                      }
                    • end: {...}
                      • line: 2
                      • column: 21
                      }
                    }
                  • value: false
                  • raw: "false"
                  }
                }
              ]
            • kind: "let"
            }
          • FunctionDeclaration {...}
            • type: "FunctionDeclaration"
            • start: 34
            • end: 80
            • loc: {...}
              • start: {...}
                • line: 4
                • column: 1
                }
              • end: {...}
                • line: 6
                • column: 2
                }
              }
            • id: Identifier {...}
              • type: "Identifier"
              • start: 43
              • end: 49
              • loc: {...}
                • start: {...}
                  • line: 4
                  • column: 10
                  }
                • end: {...}
                  • line: 4
                  • column: 16
                  }
                }
              • name: "toggle"
              }
            • expression: false
            • generator: false
            • async: false
            • params: []
            • body: BlockStatement {...}
              • type: "BlockStatement"
              • start: 52
              • end: 80
              • loc: {...}
                • start: {...}
                  • line: 4
                  • column: 19
                  }
                • end: {...}
                  • line: 6
                  • column: 2
                  }
                }
              • body: [...] (1)
                • ExpressionStatement {...}
                  • type: "ExpressionStatement"
                  • start: 56
                  • end: 77
                  • loc: {...}
                    • start: {...}
                      • line: 5
                      • column: 2
                      }
                    • end: {...}
                      • line: 5
                      • column: 23
                      }
                    }
                  • expression: AssignmentExpression {...}
                    • type: "AssignmentExpression"
                    • start: 56
                    • end: 76
                    • loc: {...}
                      • start: {...}
                        • line: 5
                        • column: 2
                        }
                      • end: {...}
                        • line: 5
                        • column: 22
                        }
                      }
                    • operator: "="
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 56
                      • end: 64
                      • loc: {...}
                        • start: {...}
                          • line: 5
                          • column: 2
                          }
                        • end: {...}
                          • line: 5
                          • column: 10
                          }
                        }
                      • name: "loggedIn"
                      }
                    • right: UnaryExpression {...}
                      • type: "UnaryExpression"
                      • start: 67
                      • end: 76
                      • loc: {...}
                        • start: {...}
                          • line: 5
                          • column: 13
                          }
                        • end: {...}
                          • line: 5
                          • column: 22
                          }
                        }
                      • operator: "!"
                      • prefix: true
                      • argument: Identifier {...}
                        • type: "Identifier"
                        • start: 68
                        • end: 76
                        • loc: {...}
                          • start: {...}
                            • line: 5
                            • column: 14
                            }
                          • end: {...}
                            • line: 5
                            • column: 22
                            }
                          }
                        • name: "loggedIn"
                        }
                      }
                    }
                  }
                ]
              }
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
Compile Svelte in your Head - {#if} • Playground • Svelte