bkr

guide

Serving TypeScript and JSX

Serve browser TypeScript and JSX with source maps straight from bkr, without an external build step.

Serve a directory with on-demand transpilation

Map a wildcard route ending in /* to a directory with transpile: true:

import { serve } from "http";

serve({
  port: 8080,
  routes: {
    "/app/*": { dir: "./client", transpile: true },
  },
});
bkr --allow-net=:8080 --allow-read=./client server.ts

When a browser requests /app/main.js, bkr looks for main.js, then main.tsx, main.ts, main.jsx, main.mts, and main.cts. Matching sources are transpiled to text/javascript and cached in memory until their mtime or size changes, so edits show up on the next reload without restarting.

Linked source maps are served at /app/main.js.map by default. Pass sourceMaps: "inline" to embed the map or false to omit it. Non-script files are served as-is with an extension-based Content-Type, and every response includes a weak ETag.

Automatic JSX emits imports from <jsxImportSource>/jsx-runtime (default preact). Include an <script type="importmap"> in your HTML so the browser resolves bare specifiers to your served vendor routes.

Transpile a single file manually

If you want full control over headers or want to pre-render at startup into a static buffered Response, call transpileWithMap() from the typescript module:

import { serve } from "http";
import { readText } from "file";
import { transpileWithMap } from "typescript";

const source = await readText("./app.tsx");
const { code, map } = transpileWithMap(source, {
  filename: "app.tsx",
  jsxImportSource: "preact",
});

serve({
  port: 8080,
  routes: {
    "/app.js": new Response(`${code}\n//# sourceMappingURL=/app.js.map`, {
      headers: { "content-type": "text/javascript; charset=utf-8" },
    }),
    "/app.js.map": new Response(map, {
      headers: { "content-type": "application/json; charset=utf-8" },
    }),
  },
});
bkr --allow-net=:8080 --allow-read=./app.tsx server.ts

For a single self-contained file with an embedded map, call transpile(source, { filename: "app.tsx", sourceMap: "inline" }) instead.

The transpiler strips types and lowers JSX using boomkat's parser. It does not bundle, rewrite import paths, type-check, or downlevel JavaScript.

Related