BlokdDocsv1.2.3

Docs / Guides

Getting started

Blokd pairs a browser-native generator runtime with Vite+ for development, testing, type checking and production builds. Use Node 20.19 or later for the package.


1. Create and run an app

Use Node.js 20.19 or later. The starter declares Vite+ as a development dependency. Installing the app’s dependencies gives its npm scripts access to the project’s own version of Vite+.

sh
npx blokd my-app --install
cd my-app
npm run dev

Open the local URL printed by the dev server. Edit src/main.tsx, then try the counter in src/counter.tsx. Run npm run build to write a production build to dist/, and npm run preview to serve that build locally.

The optional --install flag (short form -i) runs npm install inside the new app. Omit it to scaffold only. If installation fails, run npm install in the generated directory to retry. Run npx blokd --help for scaffold options. With no directory argument, the launcher creates my-blokd-app. Use an empty directory unless you intend to overwrite matching starter files with --force.

2. Add a UI component

From the new app’s directory, run:

sh
npx blokd-ui add button
npm install @lucide/icons

Import Button from ./ui/button in src/main.tsx. The installer copies the source and its theme and icon helpers into src/ui. Run npx blokd-ui list to browse other components.


Local state

Print one sheet. Both totals change, but the component keeps just one number in memory.

3. Follow a change through the component

Open the local state example. Click the button twice, then reset it. In the source, find the initial count before the loop and the derived value inside it. Move the initial count into the loop in your scaffold and see what happens on the second click. Moving it back restores the counter.

When an app needs to share the count between components, use a signal. Explore shared state and changing props.


4. Add Blokd to an existing Vite+ app

Install @fierst/blokd in the app (for example, npm install --save-exact @fierst/blokd@1.2.3). Add blokd() from @fierst/blokd/vite to your vite.config.ts:

ts
import { defineConfig } from 'vite-plus';
import blokd from '@fierst/blokd/vite';

export default defineConfig({
  plugins: [blokd()],
});

Set "jsx": "react-jsx" and "jsxImportSource": "@fierst/blokd" in tsconfig.json, and write standard .tsx components:

tsx
import { render } from '@fierst/blokd';

export function* Counter() {
  let count = 0;
  for (;;) {
    yield <button onClick={() => count++}>Clicks: {count}</button>;
  }
}

render(<Counter />, document.getElementById('app')!);

The Vite plugin parses JSX and TSX with Oxc before Vite strips TypeScript. Blokd lowers JSX into hoisted template slots, maps expressions back to their original positions, and stamps exported generator components for lazy revival. The Astro integration uses the same compiler.


5. Return HTML from a Fetch handler

Use a TSX server entry in a Fetch-based host. Build and serve the client assets first, then import that build's manifest in your server bundle.

tsx
import manifest from '../dist/blokd-manifest.json' with { type: 'json' };
import { htmlResponse } from '@fierst/blokd/ssr';
import { App } from './app.tsx';

export default {
  fetch(req: Request) {
    const url = new URL(req.url);
    return htmlResponse(<App url={url.pathname + url.search} />, {
      title: 'My App',
      revive: true,
      manifest,
    });
  },
};

For a split client/server build, keep browser-safe components in shared modules. Import the island modules from the client entry (a side-effect import such as import './Counter.tsx' lets the plugin discover its exports). Run vp build for the client, then vp build --ssr src/server.tsx --outDir dist-server for a server entry at that path. Resolve the manifest import relative to the entry file, for example ../dist/blokd-manifest.json from src/server.tsx. Serve the contents of dist/ at the configured public base. Route document requests to your Fetch handler and asset requests to the client files. The handler above is a Fetch-host module, so a Node HTTP server needs its own Request/Response adapter.