Templates
Embedded scaffolding used by the CLI to create projects and generate files. Keeps new apps consistent and zero-config.
Overview
- Lives inside the engine and is embedded into the CLI.
webstir initlays down a full-stack project by default.- Generators add files in the right place with sensible defaults.
Layout
Created by webstir init unless you choose client-only or server-only:
- Frontend:
src/frontend/- App shell:
src/frontend/app/app.html,src/frontend/app/*.js|css|png(copied as-is) - Pages:
src/frontend/pages/<page>/index.html|css|ts(seed includeshome)
- App shell:
- Backend:
src/backend/index.ts(Node API server entry) - Shared:
src/shared/**(types/modules used on both sides) - Types:
types/**(ambient.d.tsand tsconfig support) - Tests:
tests/**(optional; used bywebstir test)
Conventions
- Base HTML requires a
<main>insrc/frontend/app/app.htmlfor page merge. - Page folder names must be URL-safe: letters, numbers,
_and-. - Each page has
index.html,index.css,index.ts. - Backend entry is
src/backend/index.tsand must export an HTTP server. - For optional app features (like the router), prefer absolute dynamic imports (e.g.,
await import('/app/router.js')). This avoids relative-path issues between dev (build/) and publish (dist/).
TypeScript
- Uses an embedded
base.tsconfig.jsonreferenced by template tsconfigs. - ESM-only; compiled via
tsc --buildin dev and publish. - Shared code in
src/sharedis compiled for both frontend and backend. - Dev tsconfig enables
sourceMapandinlineSourcesfor smooth debugging; publish never ships source maps and strips anysourceMappingURL. - Dynamic imports are not bundled in v1; they load at runtime. Keep paths absolute (e.g.,
/app/...) for assets undersrc/frontend/app/.
CSS & Assets
- Plain CSS by default; optional CSS Modules in publish.
@importand asset URLs are resolved; files copied to outputs.- Place static app assets under
src/frontend/app/*. - Place Images, Fonts, and Media under
src/frontend/{images|fonts|media}/**.
Client Error Reporting
- The base
app.htmlincludes/app/error.jswhich installs a lightweight client error handler. - It listens for
windowerrorandunhandledrejectionand reports toPOST /client-errorsusingsendBeacon(fallback tofetch). - Behavior:
- Throttled: max 1 event/second; capped at 20 per page session.
- Deduped: repeats suppressed within 60s using a fingerprint of type|message|file:line:col|stack-hash.
- Correlation: includes a client correlation id; server also accepts
X-Correlation-ID.
- Override: set
window.__WEBSTIR_ON_ERROR__ = (event) => { /* custom */ }before errors occur to customize reporting. - Opt-out: remove the
<script src="/app/error.js" async></script>tag from yoursrc/frontend/app/app.html.
Generators
add-page
- Command:
webstir add-page <name> - Delegates to
webstir-frontend add-page(TypeScript CLI) to scaffoldindex.html|css|ts. - Does not modify existing pages or
app.html. - Name normalization: trims, lowercases, replaces spaces with
-.
add-test
- Command:
webstir add-test <name-or-path> - Delegates to
webstir-testing-add(TypeScript CLI) to create<name>.test.tsunder the nearesttests/directory. - Works for both frontend and backend tests.
Backend Template
- Minimal Node server at
src/backend/index.ts. - Exposes health endpoints (
GET /api/health+/healthz) and a readiness probe (/readyz) that returns the manifest summary. - Reads
PORTenv var; defaults handled by the CLI dev server proxy in dev. - Optional auth adapter: set
AUTH_JWT_SECRET(plusAUTH_JWT_ISSUER/AUTH_JWT_AUDIENCEandAUTH_SERVICE_TOKENSwhen needed) to enable bearer-token verification and populatectx.authin module routes. - Observability: install
pino, setLOG_LEVEL/LOG_SERVICE_NAME, and enable metrics viaMETRICS_ENABLED. Every request logs structured JSON and/metricsexposes rolling latency/error stats. - Database & migrations: set
DATABASE_URL(defaults to SQLite in./data/dev.sqlite) and manage schema changes viasrc/backend/db/migrate.ts+src/backend/db/migrations/*.ts. Installbetter-sqlite3for the default SQLite flow orpgfor Postgres, then runnpx tsx src/backend/db/migrate.ts.
Publish Outputs
- Per page:
dist/frontend/pages/<page>/index.html - Fingerprinted assets:
dist/frontend/pages/<page>/index.<timestamp>.{css|js} - Per-page
manifest.jsonlisting hashed asset names. - App assets copied to
dist/frontend/app/*.
Customizing Templates
- Edit templates under
Engine/Templates/in the source. - Keep conventions intact (page structure, base HTML
<main>, server entry path). - After changes, rebuild the CLI to embed updated templates.