qat

The program graph

The compiler already knows what each component declares, what reads each signal and what each handler writes. The program graph is that knowledge for the whole app, in one versioned file, so that a tool reads the graph instead of reading the source.

  • qatjs compress writes dist/graph.json, and a slice per route under dist/graph/.
  • qatjs dev keeps .qat/graph.json, updated on every transform (a file's part is replaced, the rest stays).
  • qatjs graph prints a part of it; @qatjs/graph reads it.

What is in it

Nodes — the modules, components and their props, signals (state, derived, store fields, module signal/computed), loads, forms, effects, watches and mounts, template blocks ({if}, {for}, {switch}, {try}), bound DOM nodes, event handlers, declared events, provide/inject, routes, chunks and federation exposes. Each has its source span ([line, col, endLine, endCol] in the .qx or .tsx file) and in, what declares it.

Edges — [from, kind, to]:

kind from → to
reads a DOM node, block, derived value, load, effect, watch or child prop → the signal it depends on
writes a handler, effect, mount or store action → the state it assigns
calls a handler → the load it reloads, the form it submits, the action or event it calls
renders a component or route → a component (a route: its layouts and page)
catches a route → its catch: pages (they render on an error, loaded then)
imports a module → a module (a route → a remote's expose)
emits a child's event → the parent's handler for it
provides a provide → each inject of the same key
chunk a route → the chunks of what it renders (graph.json; a chunk imports the chunks it loads, and the entry's chunks are entry: true: together, the route's initial set)

A route node carries its path, and budget when page(p, { budget }) sets one (bytes, gzip). qatjs budget reads both (compress.md).

Edges across files are resolved: a store field read in five components has five readers, and an event emitted three components down reaches the handler at the top.

Handlers carry their write set (writes) and call set (calls). Bodies are opaque beyond that: a local function the handler calls is followed, nothing else.

Value domains (domain) — per signal, from its declaration: ['false', 'true'], union members (state<'grid' | 'list'>), ['null', 'non-null'], a load's idle | pending | ready | error, a form's pristine | dirty | submitting | submitted | invalid; anything else is 'opaque'. Nothing at runtime depends on it.

A type argument made only of primitives (the one that makes state<T> a plain signal, reactivity.md) gives its members as written, with a primitive kind for each keyword: state<number | null> is ['number', 'null'], state<string> is ['string'], state<1 | 2 | bigint> is ['1', '2', 'bigint']. A kind (number, string, bigint, symbol) stands for every value of it, and a literal it covers is left out ('a' | string is ['string']); undefined and void are null. A type with an object member stays ['null', 'non-null'] or 'opaque'.

Ids

An id is the file's path from the app (always with /) and the declaration:

9 lines
src/pages/cart.qx                      the module
src/pages/cart.qx#Cart                 a component
src/pages/cart.qx#Cart.count           its state
src/stores/cart.js#cart.items          a store field
src/pages/cart.qx#Cart.emit:remove     a declared event
src/pages/cart.qx#Cart.on:click<button>    a handler; the second of its name is …#2
src/pages/cart.qx#Cart.{if}            a block; Cart.<p> a bound DOM node; Cart.effect, Cart.provide:Theme
src/routes.js#/cart                    a route
chunk:cart                             a chunk (its name, not its hashed file)

Ids are names, never positions: an edit elsewhere renames nothing, so a diff between two builds shows what changed. Moving a file changes the path part of its ids and nothing else. In qatjs dev, __qat.explain(element) gives the same ids (id, each signal's id, ownerIds), so what is on screen and the graph join by id.

The route slices

For code that runs in production (devtools through report(), a version switch), compress writes the graph in small files under dist/graph/. graph.json itself is a dev and CI file; nothing loads it in the browser.

file holds
graph/index.json a node per route (its id, path and the file of its slice), then a node per chunk (chunk:<name> and the file of its part): the map from chunk name to hashed part, as the import map is for the JavaScript chunks. The one name without a hash, as index.html is for the JavaScript
graph/routes/<route>-<hash>.json a route's slice: the route and its initial chunk set, the chunks by name only. Small, since it holds no chunk's contents: 198 B gzip at most on examples/shop, 404 B on a route of the generated 300-component scale app (qatjs compress, 2026-11-07); nothing caps it
graph/chunks/<chunk>-<hash>.json a chunk's part: the ids and kinds of what its modules declare and the edges they make (modules: which modules). Shared by every route that loads the chunk, as the JavaScript chunks are

Named by content, kept as the JavaScript is. A part's and a slice's name ends in 8 characters of its sha256 (base64url). An edit renames the parts whose modules changed and rewrites the index, which maps the chunks to them. A slice changes only when its route's chunk set does: a structural edit to a component every route shows (a new state in the header) writes its part, graph.json and the index, not a thousand slices. Everything else keeps its name and its bytes, so a cache keeps it. A build removes what the last build wrote under dist/graph/ and this one no longer names, as it prunes assets/. proof.json lists every file.

Served as manifest.json is. graph/index.json is the one file a deploy changes under a name it keeps, so it is revalidated on every use: qatjs preview and the node adapter send it cache-control: no-cache (as they send manifest.json and the pages), the service worker of --pwa does not precache it, and a CDN in front of the app gives it the short TTL (or the purge on deploy) it gives manifest.json. The parts and slices are hashed: immutable, cached for a year, as assets/ is.

Load lazily. A runtime consumer fetches only what it needs, when it needs it:

  • The devtools, when opened: graph/index.json, then the current route's slice, then the parts the index maps that slice's chunks to. Another route's slice loads when it is shown. A part already loaded under the same name is the same bytes, so it is not fetched again.
  • A version transition (a deployed build replacing the running one): the new build's graph/index.json. A chunk whose part file is the same in the old and the new index did not change; the route's slice is fetched again only when its file differs (its chunk set changed). Only the parts whose names differ are fetched and compared.

The slices are schema graph v2. A v1 build (before D207) has no chunk nodes in its index, and its slices' chunk nodes carry their parts' file: a reader takes the index's file, else the slice's. readManifest() reads v1 as v2 (the same data).

qatjs graph

shell5 lines
qatjs graph                              # counts by kind
qatjs graph --component Cart             # the component, what it declares, every edge in or out
qatjs graph --signal cart.items          # who reads it, who writes it
qatjs graph --route /checkout --mermaid  # as a Mermaid graph, for docs and pull requests
qatjs graph --signal cart.items --json   # the nodes and edges

It reads dist/graph.json, else .qat/graph.json, else makes the graph from the source (the same compile; nothing is written). --from <file> reads another.

@qatjs/graph

JavaScript10 lines
import { readFileSync } from 'node:fs'
import { diff, merge, readGraph } from '@qatjs/graph'

const g = readGraph(readFileSync('dist/graph.json', 'utf8'))
g.readers('src/stores/cart.js#cart.items') // what reads it (or, for a store, any of its fields)
g.writers('src/stores/cart.js#cart.items') // the handlers, effects and actions that assign it
g.reach('src/routes.js#/checkout')         // everything the route renders, reads and calls
g.find('cart.items')                       // the nodes a short name may mean
diff(before, after)                        // nodes added, removed and changed (by field), edges added and removed
merge(shell, remote)                       // a federation: a remote's ids get its name, its exposes meet the shell's

readGraph(json, { validate: true }) checks the file against its schema (graph in @qatjs/schemas) first.