Lines 15-55typescript
15const UPPERCASE_START = /^[A-Z]/
16const AWAIT_PREFIX = /^await\b/
17const LEADING_KEYWORD = /^\s*(\S+)/
20A minimal compile-time parser for the abide template subset: elements, text with
21`{expr}` interpolation, static/`{expr}`/`on<event>={expr}` attributes, and
22`{#if}/{#for}/{#await}/{#switch}/{#try}` block control flow. Not a full HTML parser — it covers what
23components need and reads brace expressions with quote/nesting awareness so an
24expression containing `<`, `>`, or `}` parses intact. Void elements self-close.
25`<!-- … -->` comments are dropped (no node emitted) so they leave no trace in the
26SSR/client output or hydration cursor.
28A `<style>` becomes a `style` node IN PLACE (not hoisted): its CSS body is read
29structurally (not via a raw-source regex) so a `<style>` sitting inside a `{expr}`
30or attribute — e.g. one quoted in a code sample — is read as that expression's
31text, never mistaken for a real style. Keeping it in the tree lets the front-end
32scope it to its sibling subtree (`analyzeComponent`); the node emits no DOM/markup.
35/* A line-leading static `import` in a nested script body. The `(?=\s)` requires
36 whitespace after the keyword (sparing `import.meta` and no-space `import(...)`),
37 and `(?!\s*\()` spares a dynamic `import (...)` written with whitespace before the
MediumDynamic Require
Package source references dynamic require/import behavior.
src/lib/ui/compile/parseTemplate.tsView on unpkg · L35 38 paren — both legitimate lazy paths — so only a true static import statement matches. */
39const NESTED_STATIC_IMPORT = /^[ \t]*import(?=\s)(?!\s*\()/m
41/* A braced template expression with the absolute source offset of its first
42 (post-trim) character, so the type-checking shadow can map a diagnostic back. */
43type Braced = { code: string; loc: number }
45export function parseTemplate(source: string, baseOffset = 0): { nodes: TemplateNode[] } {
48 /* Reads a `{...}` expression starting at `cursor` (on the `{`), tracking
49 string literals and nested braces so the matching `}` is found. `loc` is the
50 absolute offset (baseOffset-relative) of the trimmed code's first char. */
51 function readBracedExpression(): Braced {
52 cursor += 1 // past '{'
55 while (cursor < source.length && depth > 0) {