Extensions
An extension is a plugin a project ships as its own .ts file. It is the same definePlugin shape — a name plus a command map — with one addition: a brand, so a plain plugin can never be mistaken for one.
Declaring one
// extensions/clock.ts
import { defineExtension } from "crust/extension";
export default defineExtension({
name: "clock",
commands: {
now: () => ({ ok: true, at: new Date().toISOString() }),
},
});
crust/extension is the public entry: package.json maps it to src/extensions/define.ts. The loader has its own: import { loadExtensions } from "crust/extensions". A barrel re-export from crust/plugin is still pending, so use these two subpaths rather than expecting the symbols there.
definePlugin owns every shape rule, so the only extra refusal is the name. An extension may not claim a name startApp already registers — app, window, fs, dialog, notification, tray, autostart, logging, stronghold and the rest — because a name is a namespace, so a duplicate would overlap the handler table rather than override it. The refusal names the collision.
Loading
const { extensions, errors } = await loadExtensions();
loadExtensions never throws. A broken file is collected, because one typo must not take a running app down:
for (const { file, reason } of errors) console.warn("extension", file, reason);
for (const extension of extensions) app.use(extension);
| option | default | meaning |
|---|---|---|
dir | <cwd>/extensions | where to discover *.ts |
names | — | load these instead of discovering: bare names, project-relative paths, or absolute ones |
Discovery reads *.ts in filename order, not directory order, so two machines load the same table. .d.ts and .test.ts are skipped.
Each file must default-export a value built by defineExtension; anything else is one error naming the file and the reason (no default export, default export is not an extension, or the import's own message). Two extensions claiming one name are refused together — the second is an error naming the first, because it would otherwise shadow it silently.
A config entry resolves against the project, so clock.ts and extensions/clock.ts both land, and a bare name gets its .ts back.
Config
{ "extensions": ["extensions/clock.ts", "extensions/status.ts"] }
extensions is a flat array of files, validated as non-empty strings. A relative entry that climbs out of the project (../outside.ts) is refused: it names a file a build cannot embed from a fresh clone. A section object is refused key by key, so the author sees which key they invented.
Absent, the loader discovers extensions/*.ts; present, it loads exactly those files.
How a build embeds them
extensionEntrySource and renderExtensionImports in src/extensions/entry-source.ts generate two blocks, and crust build writes both into the wrapper it compiles: an extensionPlugins() function of guarded dynamic imports, and a registry.register(...) call before startApp. crust dev generates the same extension blocks for the runtime it spawns, so a project behaves the same either way.
// Generated by `crust build`. Edit nothing here.
import { runCrashGuardIfMarked } from "…/src/runtime-crash-guard.ts";
if (await runCrashGuardIfMarked()) process.exit(0);
const config = (await import("/abs/path/crust.config.ts")).default;
const { assets } = await import("/abs/path/dist/crust-assets.ts");
const { startApp } = await import("…/src/runtime.ts");
const { extensionRegistry } = await import("…/src/extensions/registry.ts");
// A built app has no dev server: drop dev.url so the window loads the
// embedded document instead of a port nobody is listening on.
const production = { ...config, dev: undefined };
async function extensionPlugins() {
const extensions = [];
try {
extensions.push((await import("/abs/path/extensions/clock.ts")).default);
} catch (error0) {
console.error(" extensions /abs/path/extensions/clock.ts: " + error0.message);
}
return extensions;
}
const registry = extensionRegistry();
registry.register(...(await extensionPlugins()));
await startApp({
config: production,
assets,
registry,
entryModule: () => import("/abs/path/main.ts"),
});
The crash-guard import is the only static one; every app-side import is dynamic, so evaluating the entry does not evaluate an extension's module body. The app re-runs its own executable as the crash guard, and that process exits at the marker check before any of these dynamic imports run. Each extension import is also guarded: an extension whose native dependency did not travel into the bundle is logged, not fatal before startApp. The function pushes each awaited module's .default, which is the plugin defineExtension returned.
That is why a built app needs no extensions/ directory on the recipient's disk — the extension is a module inside the binary. startApp accepts the registry and registers it into the core set when it builds the bridge, so a command from an embedded extension reaches the page.
With no extension files both blocks vanish, so a project with none produces the same wrapper as before.
A working example
examples/extensions/ is the reference, and every piece is real:
| file | half |
|---|---|
crust.config.ts | extensions: ["extensions/clock.ts"] |
extensions/clock.ts | the extension: defineExtension, a timer it keeps between calls, clock:tick pushed through the handler context |
public/app.js | the page: clock.now, startTimer, stopTimer, and a clock:tick listener |
main.ts | the backend: calls the same commands directly and prints what came back |
bun run src/cli/index.ts dev --dir examples/extensions
The extension owns state — a running interval and a tick count — and emits through ctx.emit, which is how an extension pushes an event rather than only answering a command. Nothing in main.ts registers it.