Skip to content

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, and action. The compiler lowers each one. Reactivity covers the declaration and update rules.
  • Async helpers: abortSignal and isPending. The compiler accepts abortSignal() only in the load of an async component and isPending() only in its view. Building interfaces covers async components.
  • Control-flow tags: Show, For, Repeat, Switch, Match, and Portal. The compiler compiles them as JSX tags, so they have no runtime value. See Control-flow tags below.
  • Props helpers: mergeProps, splitProps, and omitProps. The compiler refuses them as values, so you call them. Components covers props.
  • Server actions: server. The compiler requires a schema from reze-js/schema as 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_modules is opaque unless it ships a reze.manifest whose hashes match its files, or you list the package in the plugin's analyze option.
  • When a manifest is stale, the plugin warns once. Regenerate the manifest by running reze-manifest in that package, or add the package to analyze.

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> accepts when (required) and fallback.
  • <For> accepts each (required), fallback, and keyed. Its child is a row function.
  • <Repeat> accepts count (required) and fallback. Its child is a row function.
  • <Switch> accepts fallback. Its children must be <Match> tags.
  • <Match> accepts when (required). It must be a direct child of <Switch>.
  • <Portal> accepts mount.
  • <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:

OptionDefaultMeaning
target"client"Output to produce: "client", "island", or "html".
moduleIdnoneCanonical module ID. Required and nonempty for island and html.
sourceMaptrueProduces a source map in map.
debugNamesfalsePasses { name } to signal, computed, and action for profiling.
hotfalseRegisters components for hot swapping through import.meta.hot.
linksnoneModule that exports link. Native <a href> elements are passed to it.
profilenoneProfiling facts for this file, from the profile store. The Vite plugin supplies them.
factsnoneReactive 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

  • error means the module is not compiled. The Vite plugin fails the build and prints the rendered text.
  • warn means the module compiles. The Vite plugin passes the warning to Vite's warning log.
  • info means 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, and message. The message starts with the code in brackets, such as [SIGNAL_READ_ONCE].
  • file, start, and end. Each position has offset, which is a byte offset, line, which is 1-based, and column, which is 0-based and counts UTF-16 code units.
  • path, which lists the enclosing components and elements.
  • fixes, where each fix has a title and edits. Each edit replaces the bytes from start to end with text. 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.

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:

CodeMeaning
PARSE_ERRORThe parser rejects the file, so nothing is compiled.
PARSER_RELOCATIONThe browser parses this markup differently than written, so the template would not match the rendered DOM.
CONTROL_FLOW_AS_VALUEA control-flow tag is used as a value. It has no runtime value.
CONTROL_FLOW_ATTRIBUTEA control-flow tag has an attribute it does not accept, or a spread attribute.
CONTROL_FLOW_MISSINGA control-flow tag lacks the attribute that decides what it renders.
CONTROL_FLOW_CHILDRENA control-flow tag has children it cannot render.
MATCH_OUTSIDE_SWITCH<Match> is not a direct child of <Switch>.
HEAD_ATTRA <Head> tag cannot be placed in the document head.
DOCUMENT_SHAPEThe document module is not one default export that returns <html> with a <head> and a <body>.
DOCUMENT_DYNAMICThe document holds a value the compiler cannot write as static HTML.
DOCUMENT_ROOTThe document has no single element that holds only children, or its id is not unique.
FLOW_PARAM_CALLEDA 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_DECLAREDsignal or computed is used outside a variable declaration.
SIGNAL_PATTERNsignal or computed initializes a destructuring pattern.
SIGNAL_DEFAULT_EXPORTsignal or computed is the default export.
SIGNAL_ASSIGN_PATTERNA 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_WRITTENA computed is written.
COMPUTED_FUNCTIONA function literal is passed to computed, which takes the expression itself.
ACTION_ARGUMENTaction is not called with a function literal.
ACTION_UNSUPPORTEDAn action body contains for await, await using, or a generator.
ACTION_NOT_CALLEDaction is used as a value.
PROPS_AS_VALUEmergeProps, splitProps, or omitProps is used as a value.
ISLAND_TRIGGERThe island attribute has a value that is not a trigger.
ISLAND_MEDIA_MISSINGisland="media" lacks islandMedia.
ISLAND_ON_ELEMENTisland is set on a native element.
ISLAND_ORPHANAn island companion attribute appears on a component without island.
MISSING_MODULE_IDAn island or html build has no moduleId.
MODULE_STATE_SHAREDA component or route callback writes a module-level let, which every request shares.
SERVER_INPUT_SCHEMAA server action has no input schema, so its public endpoint would accept any body.
ASYNC_COMPONENT_SHAPEAn async component has a form the compiler cannot rewrite.
ASYNC_VIEW_PROPERTYA pending or failure view is assigned to a component instead of a <Loading>/<Errored> boundary.
INTERNAL_REACTIVITY_IMPORTA reactive primitive is imported from reze-js/internal/reactivity.
ASYNC_PROPS_READ_IN_AWAITAn async component reads its props inside an await operand.
ASYNC_TRY_AROUND_AWAITAn async component wraps an await in try.
ABORT_SIGNAL_OUTSIDE_LOADabortSignal() is called outside the load of an async component.
IS_PENDING_OUTSIDE_VIEWisPending() is called outside the view of an async component.

Warnings:

CodeMeaning
CHILDREN_PROP_IGNOREDA children attribute is set next to nested children. The nested children win, so the attribute is never rendered.
KEY_ON_ELEMENTkey is set on a native element, where it does nothing. Key list rows with the keyed attribute of <For>.
DUPLICATE_ATTRIBUTEThe same attribute is set twice on one element, so the earlier value is dead code.
UNKNOWN_ATTRIBUTEThe attribute is probably a typo. It renders as written, and the browser ignores it.
EVENT_NAME_LOWERCASEA lower-case event attribute receives a function, which never attaches a listener.
PROPS_DESTRUCTUREDProps 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_ONCEA signal or computed is copied once in a component body, and the copy never updates.
ACTION_NESTED_WRITEA write in a function inside action runs after the action moved on, so a failed action does not undo it.
ISLAND_NOT_SPLITAn island component is also used in a way that keeps it in the main chunk. Only its execution waits for the trigger.

Info:

CodeMeaning
SIGNAL_FOLDEDA never-written signal compiled to a plain constant.
DEAD_BRANCH_REMOVEDA JSX branch has a literal condition, so it can never render, and the compiler removed it.
PROPS_REWRITTENDestructured props became one props object, and each destructured name is a lazy read of it.
AUTO_SELECTORA <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:

  • debugNames is on in development builds and off in production builds. Names appear in the output, which helps profiling.
  • hot is on when the dev server runs with hot module replacement. Components register for hot swapping. A swap preserves each mounted instance's signal values 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.
  • sourceMap follows Vite's build.sourcemap in a build and dev.sourcemap in the dev server.

Fixed bindings also run differently in the two modes. Compiled bindings explains the difference.

  • Reactivity covers signal, computed, store, and action.
  • 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.