# How it works
Compiled when your server loads, checked by TypeScript, bundled for each request, run by your app: what each step does, and the packages that do it.
A screen goes through four steps: compiled when your server loads its files,
checked by TypeScript, bundled for each request, and run by your app. A new
project has all four set up; this page is for when you want to know what each
one did, or to set up a server by hand.
## 1. Compiled with your server
Your server runs with a loader, `@backtickjs/node-plugin` on Node or
`@backtickjs/bun-plugin` on Bun. As it loads each `.tsx` file, it compiles each
`` cs`…` `` to plain JavaScript, with the compile step your framework names in
`package.json`:
```json
{
"backtick": {
"plugins": ["@backtickjs/jsx-react/plugin"]
}
}
```
An Expo app adds `"@backtickjs/expo/plugin"` after it, so its scripts are
compiled as Expo compiles the app's own code for Hermes.
A compiled script is a module: a function of its splices, which reads each
splice where the script wrote it. A script compiles once, however many requests
use it. The loaders' READMEs,
[node-plugin](https://github.com/backtickjs/backtick/tree/main/packages/node-plugin)
and
[bun-plugin](https://github.com/backtickjs/backtick/tree/main/packages/bun-plugin),
have their flags. To build your server ahead of time instead,
[tspatch-plugin](https://github.com/backtickjs/backtick/tree/main/packages/tspatch-plugin)
does the same with `tspc`.
## 2. Checked by TypeScript
The code inside every script, and every value crossing into it, is checked as
TypeScript. A new project's `npm run typecheck` checks your app and your
server:
```sh
tsc --noEmit && backtick-tsc -p server --noEmit
```
`backtick-tsc` is `tsc` that understands client scripts: it takes `tsc`'s
arguments, and reports errors inside scripts at the line and column you wrote.
Run it in CI as you would `tsc`. The server's `tsconfig.json` needs two
settings, which the template sets and the
[`@backtickjs/jsx-react` README](https://github.com/backtickjs/backtick/tree/main/packages/jsx-react#setup)
explains.
[Backtick for VS Code](https://marketplace.visualstudio.com/items?itemName=backtickjs.backtick-vscode)
highlights scripts as TSX, colours splices, and shows errors, completions and
hover information inside scripts.
## 3. Bundled for each request
Your server bundles a screen when the app asks for it:
```tsx title="server/index.tsx"
import { serve } from "@hono/node-server";
import { createRender } from "@backtickjs/bundler";
import { Hono } from "hono";
import { db } from "./db.js";
import { Home } from "./Home.js";
const app = new Hono();
const render = createRender({
onBundlerError: (error) => console.error(error),
});
// Each screen is bundled for the modules the requesting build carries, from
// its module map.
app.get("/", async (context) => {
const user = await db.userFor(context.req.header("authorization"));
return render(context.req.raw, );
});
serve({ fetch: app.fetch, port: 3000 });
```
`render` runs the server components in `input`, then writes what they
returned as one JavaScript file, for the modules the request lists. For a request from Sam, the screen in
[Thinking in Backtick](/docs/thinking-in-backtick/#one-screen-both-sides)
becomes this bundle. It's generated by the docs' tests, so it's exactly what
the bundler writes:
```js title="server/Home.bundle.js"
"use strict";
const { ScrollView: $i0 } = require("react-native");
const { Text: $i1 } = require("react-native");
const { useState: $i2 } = require("react");
const { Pressable: $i3 } = require("react-native");
const $module0 = require("react/jsx-runtime");
const $modules = {
"hjsejdj37nio:22:9": (module, exports, require) => {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
exports.default = (($splice0, $splice1, $splice2, $splice3, $splice4) => (($ScrollView => (0, jsx_runtime_1.jsxs)($ScrollView, { children: [($Text => (0, jsx_runtime_1.jsxs)($Text, { children: ["Good morning, ", $splice2().name] }))($splice1()), ($ReorderButton => (0, jsx_runtime_1.jsx)($ReorderButton, { order: $splice4() }))($splice3())] }))($splice0())));
},
"hjsejdj37nio:9:22": (module, exports, require) => {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
exports.default = (($splice0, $splice1, $splice2) => (props) => {
const [added, setAdded] = $splice0()(false);
return (($Pressable => (0, jsx_runtime_1.jsx)($Pressable, { onPress: () => setAdded(true), children: ($Text => (0, jsx_runtime_1.jsx)($Text, { children: added ? "Added ✓" : "Reorder " + props.order.name }))($splice2()) }))($splice1()));
});
},
};
const $exports = {
"react/jsx-runtime": $module0,
};
const $require = (id) => {
if (!(id in $exports)) {
const module = { exports: {} };
$exports[id] = module.exports;
$modules[id](module, module.exports, $require);
$exports[id] = module.exports;
}
return $exports[id];
};
const $cache = module.cache ?? new Map();
const $cached = (key, make) => {
if (!$cache.has(key)) {
$cache.set(key, make());
}
return $cache.get(key);
};
const $cs0 = $require("hjsejdj37nio:22:9").default;
const $cs1 = $require("hjsejdj37nio:9:22").default;
const $thunk0 = () => ($i0);
const $thunk1 = () => ($i1);
const $thunk2 = () => ({ id: "u-7", name: "Sam" });
const $thunk3 = () => ($i2);
const $thunk4 = () => ($i3);
const $value5 = $cached("hjsejdj37nio:9:22", () => $cs1($thunk3, $thunk4, $thunk1));
const $thunk6 = () => ($value5);
const $thunk7 = () => ({ id: "o-1042", name: "Flat white" });
const $value8 = $cs0($thunk0, $thunk1, $thunk2, $thunk6, $thunk7);
module.exports = ($value8);
```
From the top:
- **The packages the app provides,** required by name: `react-native`,
`react`, `react/jsx-runtime`. The bundle requires them rather than including
them, and each is checked against the version the requesting build carries.
- **The module table:** each script's compiled module, once, however many
times it's used. Their JSX is React's `jsx` calls.
- **The bundle's constants.** Each splice is a small function, `$thunk`,
that the script calls when it reads the splice: Sam's name and order are
there, as data. `ReorderButton` is `$value5`, made once for the whole
bundle, so React keeps its state across renders. Every script is a
`$value`, made once the same way, unless it reads a variable of the
script around it.
- **The root:** the screen, a value made the same way, `$value8`.
Nothing of `Home` is in the bundle, and `db` isn't either: the phone gets
results, never the code that produced them. Only what a request drew is in
its bundle.
`generate` writes it as CommonJS, `"cjs"`, for React Native, or as an ES
module, `"es"`, for a web page. It can also write a source map into your
server's files: `"inline"` appends it to the code, `"hidden"` returns it
apart, for reading the app's stack traces on your server.
```ts title="server/sourcemap.ts"
import type { Build } from "@backtickjs/bundler";
// The bundle for the app, and its map kept on the server, for reading the
// app's stack traces against your server's files.
export function write(build: Build, maps: Map, id: string) {
const { code, map } = build.generate({ sourcemap: "hidden" });
maps.set(id, map!);
return code;
}
```
A map holds your files' names and lines, never their code. The
[bundler's README](https://github.com/backtickjs/backtick/tree/main/packages/bundler)
has every option.
## 4. Run by your app
Your app fetches the bundle like any request, and runs it with a runtime from
`createRuntime`, in `@backtickjs/core`, made once from its module map: the
packages it ships, imported as it starts. What the bundle exports is the screen, a React
node the app draws:
```tsx title="client/App.tsx"
import { createRuntime } from "@backtickjs/core";
import * as React from "react";
import { Suspense, use, useState } from "react";
import { ActivityIndicator } from "react-native";
import { installedPackages, moduleMap } from "./backtick";
// What fetches and runs the screens the server sends, with the modules the
// app provides at their versions: one per app, so what a screen makes once
// stays made.
const runtime = createRuntime({ moduleMap, installedPackages });
async function fetchScreen(url: string): Promise {
const response = await runtime.fetch(url);
return runtime.exec(await response.text());
}
export default function App() {
const [screen] = useState(() => fetchScreen("https://api.example.com/"));
return (
}>
);
}
function Screen({ screen }: { screen: Promise }) {
return use(screen);
}
```
Both files are what `create-backtick-app` sets up, without its development
extras. Fetching is your app's, and so is when: on launch, on focus, on a pull
to refresh. `runtime.exec` runs the code it's given, as `eval` does, so fetch
bundles only from your own server, over HTTPS.
Because the phone runs code your server wrote, a deploy of your server is a
release of the screen: the next time someone opens it, they get the new one.
## Packages
What a React Native project uses, each linked to its README:
| Package | What it is |
| ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| [`@backtickjs/core`](https://github.com/backtickjs/backtick/tree/main/packages/core) | The `cs` tag, and `createRuntime` to run a bundle |
| [`@backtickjs/jsx-react`](https://github.com/backtickjs/backtick/tree/main/packages/jsx-react) | React's JSX, and its compile step for scripts |
| [`@backtickjs/bundler`](https://github.com/backtickjs/backtick/tree/main/packages/bundler) | Runs server components and bundles a screen |
| [`@backtickjs/node-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/node-plugin) | Compiles scripts as your Node server loads them |
| [`@backtickjs/tsc`](https://github.com/backtickjs/backtick/tree/main/packages/tsc) | `backtick-tsc`: type-checks scripts and splices |
| [`@backtickjs/prettier-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/prettier-plugin) | Formats the code inside scripts |
The [repository's README](https://github.com/backtickjs/backtick#packages)
lists them all, Solid's JSX and Bun included.