CodeVetter · Repo Unpack · Behavior traces
sveltejs/svelte
Three flows dominate. (1) Compile: `compile()` resets global state, validates options, parses, strips TS, analyzes, transforms, and re-attaches a public AST. (2) Client startup: `mount()` initialises DOM operation caches, opens a root effect and an error boundary, pushes a component context, invokes the compiled component function, and returns its exports. (3) Update: a `$state` write sets a source, which schedules its reactions into a `Batch`, which is flushed on the microtask or synchronously via `flushSync`. A fourth, the SSR→hydrate round trip, is what the test harness itself exercises on every fixture.
- Compile trace: `remove_bom` → `state.reset` → `validate_component_options` → `_parse(source)` → destructure `parsed.options` into `combined_options` → conditional `remove_typescript_nodes` → `analyze_component(parsed, source, combined_options)` → `transform_component(analysis, source, combined_options)` → `to_public_ast`.
- Transform trace (client): `client_component` seeds state with `hoisted: [b.import_all('$', 'svelte/internal/client')]` and an empty `templates` Map, walks the template AST with the visitor table, appends `template.body` into `component_block.body`, pushes a `$$css` const when styles are injected, then re-orders `module.body` so imports stay on top followed by hoisted statements.
- Mount trace: `mount()` → `_mount()` → `init_operations()` → `component_root(...)` → create an anchor text node via `target.appendChild(create_text())` → `boundary(anchor, { pending }, cb)` → `push({})` to open a component context → assign `props.$$events` if `events` given → set `should_intro = intro` → call `Component(anchor_node, props)`.
- Hydrate trace: `hydrate()` forces `intro ?? false`, scans forward from `get_first_child(target)` for a comment node whose `data === HYDRATION_START`, throws `HYDRATION_ERROR` if absent, calls `set_hydrating(true)` + `set_hydrate_node(anchor)`, then delegates to the same `_mount` with the discovered anchor.
- Hydration failure recovery: non-Svelte errors are `console.warn`ed, `options.recover === false` escalates to `e.hydration_failed()`, otherwise `init_operations()` + `clear_text_content(target)` runs and the component falls back to a fresh `mount(component, options)` — with `hydrating`/`hydrate_node` restored in `finally`.
- Read trace: `get(signal)` adds the signal to `captured_signals`, registers the dependency on `active_reaction`, and takes one of two registration paths depending on whether `REACTION_IS_UPDATING` is set — the post-`await` path deduplicates with `includes.call` because `remove_reactions` would otherwise disconnect still-live deps.
- Synchronous flush trace: `flushSync(fn)` sets `is_flushing_sync`, flushes any non-fork `current_batch` before running `fn`, then loops `flush_tasks()` + `current_batch.flush()` until `current_batch === null`, restoring the previous flag in `finally`.
- Batch flush trace: `Batch.flush()` clears DEV `source_stacks`, sets `is_processing = true` and `current_batch = this`, runs the private `#process()`, then in `finally` resets `flush_count`, `last_scheduled_effect`, `collected_effects`, `legacy_updates`, `current_batch`, `batch_values`, and `old_values`.
- `{@html}` trace: the `html()` block runs inside a `template_effect`, short-circuits on unchanged value, uses `parent_node.innerHTML = value` when it is the only child, otherwise builds a `<template>`/`<svg>`/`<math>` wrapper, sets `wrapper.innerHTML`, and inserts the resulting nodes via `anchor.before(...)`.
- HMR trace: `hmr(fn)` wraps the component in a `source(fn)` signal; a `block()` effect reads it, and on change deletes every key from the old `instance`, calls `destroy_effect(effect)`, and re-creates a `branch()` with `set_should_intro(false)` so the swap plays no transitions.
- Test trace: `runtime_suite` → `common_setup` builds a `CompileOptions` (forcing `accessors: true` by default and `experimental.async` from `runes && async_mode`) and calls `compile_directory(cwd, 'client')` and `compile_directory(cwd, 'server')` → `run_test_variant` imports from `_output/` and calls `render`, `mount`, or `hydrate` per variant.
- `compile_directory` trace: wipe `_output/<generate>`, glob every non-`_`-prefixed file, route `.svelte.js` through `compileModule`, plain `.js` through a re-export shim so `_config.js` can share module state, and `.svelte` through optional `preprocess` then `compile`, honouring `.client.svelte`/`.server.svelte` suffixes.
- Message codegen trace: `scripts/process-messages/index.js` deletes and recreates `documentation/docs/98-reference/.generated`, walks `messages/<category>/*.md`, matches `## CODE` headings, and throws on any duplicate code across categories.
- Benchmark trace: `benchmarking/run.js` in parent mode filters suites by `process.argv` substrings and forks one child per benchmark; in child mode (`BENCH_SINGLE` set) it calls `$.push({}, true)`, runs the benchmark under `with_cpu_profile`, `$.pop()`s, and `process.send`s the result with an exit callback.