The compiler
The Reze compiler turns modules that contain JSX and compiler syntax into DOM creation code and reactive bindings. This page covers what the compiler lowers, what it decides for each module, how it groups bindings into effects, and how to read its diagnostics.
Compiler syntax and runtime functions
The compiler recognizes a fixed set of names that you import from reze-js, and it rewrites each use into runtime code. The groups are:
- Reactive primitives:
signal,computed, andaction. The compiler lowers each one. Reactivity covers the declaration and update rules. - Async helpers:
abortSignalandisPending. The compiler acceptsabortSignal()only in the load of an async component andisPending()only in its view. Building interfaces covers async components. - Control-flow tags:
Show,For,Repeat,Switch,Match, andPortal. The compiler compiles them as JSX tags, so they have no runtime value. See Control-flow tags below. - Props helpers:
mergeProps,splitProps, andomitProps. The compiler refuses them as values, so you call them. Components covers props. - Server actions:
server. The compiler requires a schema fromreze-js/schemaas the first argument, inserts a stable id, and lowers the call to the request endpoint. The Vite plugin covers the API.
Other reze-js exports, such as store and effect, are runtime functions. The compiler rewrites their import source to the runtime module, which is private, so you import them from reze-js as usual. Lifecycle covers effect, and Reactivity covers store. render is the one value export that mounts an app in the browser.
Do not import the lowered primitives from reze-js/internal/reactivity. That path is private compiler output. The compiler refuses such an import with INTERNAL_REACTIVITY_IMPORT:
import { signal } from "reze-js/internal/reactivity";
let count = signal(0);
export function bump() {
count += 1;
}
export const view = <p>{count}</p>;
Module boundaries
The compiler works on one module at a time. The output for a module depends on that module's source. Facts about imported modules change the output only when you pass them, which the Vite plugin does for you. Vite plugin describes the plugin options.
The Vite plugin decides which imported modules have facts:
- A package in
node_modulesis opaque unless it ships areze.manifestwhose hashes match its files, or you list the package in the plugin'sanalyzeoption. - When a manifest is stale, the plugin warns once. Regenerate the manifest by running
reze-manifestin that package, or add the package toanalyze.
Each module has a canonical module ID. For application files, the ID is the POSIX path relative to the project root. For files in a package, the ID is the package name followed by the path inside the package. The island and html targets key their output by module, so they need a nonempty module ID. Without one, the compiler reports MISSING_MODULE_ID and compiles nothing. The plugin rejects a second, different compiler input for the same ID, so the source of each module must match across the builds that share its output.
Control-flow tags
Each control-flow tag accepts a fixed set of attributes, and it refuses spread attributes. The required attributes decide what the tag renders:
<Show>acceptswhen(required) andfallback.<For>acceptseach(required),fallback, andkeyed. Its child is a row function.<Repeat>acceptscount(required) andfallback. Its child is a row function.<Switch>acceptsfallback. Its children must be<Match>tags.<Match>acceptswhen(required). It must be a direct child of<Switch>.<Portal>acceptsmount.<Head>accepts no attributes. Its children must be<meta>,<link>and<script>elements.
A tag with an attribute outside this list fails with CONTROL_FLOW_ATTRIBUTE:
import { Show, signal } from "reze-js";
let open = signal(false);
export function toggle() {
open = !open;
}
export const view = (
<Show when={open} keyed>
<p>Open</p>
</Show>
);
A <For> whose child is not a row function fails with CONTROL_FLOW_CHILDREN:
import { For } from "reze-js";
const items = [1, 2, 3];
export const view = (
<ul>
<For each={items}>
<li>row</li>
</For>
</ul>
);
A control-flow tag without its required attribute fails with CONTROL_FLOW_MISSING. A <Head> child that is not a head tag fails with CONTROL_FLOW_CHILDREN, and a head tag without static identity fails with HEAD_ATTR:
import { Head } from "reze-js";
declare const url: string;
export const view = (
<Head>
<script src={url} />
</Head>
);
The diagnostic reference below lists the other control-flow rules. Building interfaces shows conditions, lists, portals, and head tags in use.
Compiled bindings
A binding is an attribute or text child that reads a signal. The compiler decides how each binding stays up to date, and it groups bindings that read the same signals.
Fixed and reactive bindings
A binding is fixed when every signal read in it is one of these:
- A value, or an operand of an arithmetic or comparison operator.
- The test of a conditional expression.
The compiler treats the following reads as reactive, and any binding that contains one is reactive:
- A read in a conditional branch.
- A member read on a signal value, such as
box.a. - A prop read, such as
props.label.
The next sample has one fixed binding and one reactive binding:
import { signal } from "reze-js";
let count = signal(0);
let flag = signal(true);
export function bump() {
count += 1;
flag = !flag;
}
export const view = (
<div>
<p title={count > 1 ? "many" : "few"}>{count}</p>
<p class={flag ? "on" : String(count)}>status</p>
</div>
);
The title binding and the text child read count only in fixed positions. The class binding reads count in a conditional branch, so it is reactive.
A fixed binding keeps the dependencies it tracked on its first run. In a production build, the runtime does not track them again. In a development build, the runtime tracks them on every run. If the set of values read changes, the runtime warns once with [rezejs] A compiled binding read a different set of reactive values. Keep reads inside the binding expression. Do not read signals inside toString, valueOf, or getters of values bound to attributes.
Effect grouping
Bindings that read the same set of signals share one effect. When any binding in the group is reactive, the whole group is reactive. Bindings with different signal sets get separate effects.
import { signal } from "reze-js";
let count = signal(0);
export function bump() {
count += 1;
}
export const view = (
<p title={count > 1 ? "many" : "few"} class={count > 1 ? "big" : String(count)}>
x
</p>
);
Both attributes read only count, so the compiler puts them in one effect. The class binding is reactive, so the whole effect is reactive. The title binding would be fixed if it stood alone.
Text bindings
A text child that reads a signal takes one of two paths. When the compiler can tell that the signal holds a number or a string, it writes the text node directly. Otherwise, it inserts the value through the general insert path. The compiler infers the kind from the initial value and every write, so a write of an unknown value, such as count += props.step, makes the kind unknown.
Constant folding
A signal that is never written in its module, and is not exported, folds into a constant. The compiler reports each fold as the SIGNAL_FOLDED info diagnostic and writes the value into the static template:
import { signal } from "reze-js";
const title = signal("Reze");
export const view = <h1>{title}</h1>;
Exported signals are never folded. A write anywhere in the module keeps the signal reactive.
The compiler also removes a JSX branch whose condition is a literal, and it reports the removal as DEAD_BRANCH_REMOVED:
export const view = <div>{false && <b>debug</b>}</div>;
Transformation scope
By default, the Vite plugin compiles files with the extensions .ts, .tsx, .js, .jsx, .mts, .cts, .mjs, and .cjs. A nonempty extensions option replaces this list. The plugin skips files under node_modules unless the package declares the reze export condition.
The compiler parses each file with the grammar its extension selects. JSX is valid in a .tsx file, but not in a .ts file. A .ts file with JSX fails with PARSE_ERROR, and nothing is compiled:
export const view = <p>x</p>;
Use the direct API
The @rezejs/compiler package exports compile and analyze. The Vite plugin calls these functions for you. Call them directly when you build your own tooling:
import { readFile } from "node:fs/promises";
import { compile } from "@rezejs/compiler";
const source = await readFile("src/counter.tsx", "utf8");
const result = compile(source, "src/counter.tsx", { target: "client" });
for (const diagnostic of result.diagnostics) {
console.error(diagnostic.rendered);
}
compile(source, filename, options?) accepts these options:
| Option | Default | Meaning |
|---|---|---|
target | "client" | Output to produce: "client", "island", or "html". |
moduleId | none | Canonical module ID. Required and nonempty for island and html. |
sourceMap | true | Produces a source map in map. |
debugNames | false | Passes { name } to signal, computed, and action for profiling. |
hot | false | Registers components for hot swapping through import.meta.hot. |
links | none | Module that exports link. Native <a href> elements are passed to it. |
profile | none | Profiling facts for this file, from the profile store. The Vite plugin supplies them. |
facts | none | Reactive facts of imported modules, keyed by import specifier, from analyze. Each entry must be for the exact source the import resolves to. |
The result always has diagnostics. code is absent when nothing in the file is rewritten. When the result has an error diagnostic, code, map, and facts are all absent.
analyze(source, filename) returns { diagnostics, facts }. The facts value describes each export, with its role, such as function, and how it reads reactive values. Pass the facts to compile for modules that import this one.
Diagnostics
The compiler reports each problem with a stable code, such as SIGNAL_READ_ONCE. Each code belongs to one severity.
Severities
errormeans the module is not compiled. The Vite plugin fails the build and prints the rendered text.warnmeans the module compiles. The Vite plugin passes the warning to Vite's warning log.infomeans the compiler made a change you may want to know about. The Vite plugin does not print it. It appears only in the JSON lines file described below.
Rendered output and fixes
Each diagnostic has these fields:
code,severity, andmessage. The message starts with the code in brackets, such as[SIGNAL_READ_ONCE].file,start, andend. Each position hasoffset, which is a byte offset,line, which is 1-based, andcolumn, which is 0-based and counts UTF-16 code units.path, which lists the enclosing components and elements.fixes, where each fix has atitleandedits. Each edit replaces the bytes fromstarttoendwithtext. Edits that are applied together remove the diagnostic.docs, a repair guide URL anchored at the code.rendered, which contains the message, the path, the location, a code frame, and the fixes.
The Vite plugin reports fixes as part of the rendered text, but it does not apply them.
Repair guide and documentation links
Each diagnostic's docs URL points to its section of the compiler's SKILL.md on GitHub. The file is generated from the diagnostic catalog, and each section explains the rule and the repair. The Vite plugin also prints the installed path of the repair guide, in the form node_modules/@rezejs/compiler/skills/reze-compiler-diagnostics/SKILL.md#<code>. It prints this line only the first time a code appears in a process.
Record diagnostics as JSON lines
To keep every diagnostic, including the info ones, set the diagnostics.jsonl option of the Vite plugin. The plugin appends one JSON object per diagnostic to that file, and it creates the directory when it is missing:
import reze from "@rezejs/vite-plugin";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
reze({
diagnostics: { jsonl: "reze-diagnostics.jsonl" },
}),
],
});
Diagnostic reference
The following tables list every diagnostic code with a short meaning. The repair guide has the full explanation and the repair for each code.
Errors:
| Code | Meaning |
|---|---|
PARSE_ERROR | The parser rejects the file, so nothing is compiled. |
PARSER_RELOCATION | The browser parses this markup differently than written, so the template would not match the rendered DOM. |
CONTROL_FLOW_AS_VALUE | A control-flow tag is used as a value. It has no runtime value. |
CONTROL_FLOW_ATTRIBUTE | A control-flow tag has an attribute it does not accept, or a spread attribute. |
CONTROL_FLOW_MISSING | A control-flow tag lacks the attribute that decides what it renders. |
CONTROL_FLOW_CHILDREN | A control-flow tag has children it cannot render. |
MATCH_OUTSIDE_SWITCH | <Match> is not a direct child of <Switch>. |
HEAD_ATTR | A <Head> tag cannot be placed in the document head. |
DOCUMENT_SHAPE | The document module is not one default export that returns <html> with a <head> and a <body>. |
DOCUMENT_DYNAMIC | The document holds a value the compiler cannot write as static HTML. |
DOCUMENT_ROOT | The document has no single element that holds only children, or its id is not unique. |
FLOW_PARAM_CALLED | A flow parameter that reads as a signal is called as a function. |
FOR_KEYED_DYNAMIC | <For keyed> is not true, false, or a key function written in place. |
SIGNAL_NOT_DECLARED | signal or computed is used outside a variable declaration. |
SIGNAL_PATTERN | signal or computed initializes a destructuring pattern. |
SIGNAL_DEFAULT_EXPORT | signal or computed is the default export. |
SIGNAL_ASSIGN_PATTERN | A signal is written through a destructuring pattern or a for loop head. |
SIGNAL_UPDATE_IN_EXPRESSION | ++ or -- is applied to a signal inside an expression. |
COMPUTED_WRITTEN | A computed is written. |
COMPUTED_FUNCTION | A function literal is passed to computed, which takes the expression itself. |
ACTION_ARGUMENT | action is not called with a function literal. |
ACTION_UNSUPPORTED | An action body contains for await, await using, or a generator. |
ACTION_NOT_CALLED | action is used as a value. |
PROPS_AS_VALUE | mergeProps, splitProps, or omitProps is used as a value. |
ISLAND_TRIGGER | The island attribute has a value that is not a trigger. |
ISLAND_MEDIA_MISSING | island="media" lacks islandMedia. |
ISLAND_ON_ELEMENT | island is set on a native element. |
ISLAND_ORPHAN | An island companion attribute appears on a component without island. |
MISSING_MODULE_ID | An island or html build has no moduleId. |
MODULE_STATE_SHARED | A component or route callback writes a module-level let, which every request shares. |
SERVER_INPUT_SCHEMA | A server action has no input schema, so its public endpoint would accept any body. |
ASYNC_COMPONENT_SHAPE | An async component has a form the compiler cannot rewrite. |
ASYNC_VIEW_PROPERTY | A pending or failure view is assigned to a component instead of a <Loading>/<Errored> boundary. |
INTERNAL_REACTIVITY_IMPORT | A reactive primitive is imported from reze-js/internal/reactivity. |
ASYNC_PROPS_READ_IN_AWAIT | An async component reads its props inside an await operand. |
ASYNC_TRY_AROUND_AWAIT | An async component wraps an await in try. |
ABORT_SIGNAL_OUTSIDE_LOAD | abortSignal() is called outside the load of an async component. |
IS_PENDING_OUTSIDE_VIEW | isPending() is called outside the view of an async component. |
Warnings:
| Code | Meaning |
|---|---|
CHILDREN_PROP_IGNORED | A children attribute is set next to nested children. The nested children win, so the attribute is never rendered. |
KEY_ON_ELEMENT | key is set on a native element, where it does nothing. Key list rows with the keyed attribute of <For>. |
DUPLICATE_ATTRIBUTE | The same attribute is set twice on one element, so the earlier value is dead code. |
UNKNOWN_ATTRIBUTE | The attribute is probably a typo. It renders as written, and the browser ignores it. |
EVENT_NAME_LOWERCASE | A lower-case event attribute receives a function, which never attaches a listener. |
PROPS_DESTRUCTURED | Props are destructured in a form the compiler cannot rewrite, so each value is read once and never updates. |
INLINE_EACH | <For each> receives an array literal, which is rebuilt on every evaluation. |
SIGNAL_READ_ONCE | A signal or computed is copied once in a component body, and the copy never updates. |
ACTION_NESTED_WRITE | A write in a function inside action runs after the action moved on, so a failed action does not undo it. |
ISLAND_NOT_SPLIT | An island component is also used in a way that keeps it in the main chunk. Only its execution waits for the trigger. |
Info:
| Code | Meaning |
|---|---|
SIGNAL_FOLDED | A never-written signal compiled to a plain constant. |
DEAD_BRANCH_REMOVED | A JSX branch has a literal condition, so it can never render, and the compiler removed it. |
PROPS_REWRITTEN | Destructured props became one props object, and each destructured name is a lazy read of it. |
AUTO_SELECTOR | A <For> row compares a signal with its own key, and the comparison now reads a selector. |
Development and production builds
The Vite plugin chooses compiler options from your Vite configuration:
debugNamesis on in development builds and off in production builds. Names appear in the output, which helps profiling.hotis on when the dev server runs with hot module replacement. Components register for hot swapping. A swap preserves each mounted instance'ssignalvalues by declaration order: keep signal declarations stable in order and type across edits, because a changed type at a position resets that position, and editing an initializer does not overwrite a preserved value.sourceMapfollows Vite'sbuild.sourcemapin a build anddev.sourcemapin the dev server.
Fixed bindings also run differently in the two modes. Compiled bindings explains the difference.
Related pages
- Reactivity covers
signal,computed,store, andaction. - Components covers props and children.
- Building interfaces covers conditions, lists, portals, and async components.
- Lifecycle covers
effect, cleanup, and context. - Vite plugin covers the plugin options,
analyze,profile, and static site generation.