API REFERENCE · 2.2.0

wgpu-kit/vite

Vite WGSL source hot reload; matching and lifecycle limitations are documented explicitly.

Auxiliary inputs, dispatch counts, managed batches and typed kernels are released in 2.1.0. The original API retains the published npm 2.0.0 declarations and implementation as its historical audit baseline; Additions in 2.1.0 and 2.2.0 are identified on affected APIs and cite src files, without claiming a fresh audit of the entire API. Only actual public exports of this entry point are listed.

Source reference: 1 ↗

wgpuKitHotReloadfunction

Create a lightweight Vite plugin that broadcasts changed .wgsl source files.

wgpuKitHotReload(): VitePluginLike;

Parameters

This call takes no arguments.

Returns

VitePluginLike Plugin named wgpu-kit:hot-reload with a handleHotUpdate hook.

Behavior & constraints

  • Only files ending exactly in .wgsl are handled. The hook reads source and broadcasts custom event wgpu-kit:kernel with { file, code }, then returns [] to suppress the normal module-update path for that change.
  • Read/broadcast runs in an unawaited async task and has no error handler. This helper does not compile WGSL on the server.

Fields & related types

NameTypeRequiredDefaultMeaning
VitePluginLike (supporting, not exported){ name: string; apply?: string; handleHotUpdate?(ctx: ViteHotUpdateCtx): unknown }——Minimal structural return type. Actual result has name and handleHotUpdate; apply is absent.
ViteHotUpdateCtx (supporting, not exported){ file: string; read(): string | Promise<string>; server: { ws: { send(payload: { type: string; event: string; data: unknown }): void } } }——Hook context: file is the changed path; read obtains source; server.ws.send publishes the custom payload.

Examples

Install the development-server plugin.
// vite.config.ts
import { defineConfig } from 'vite';
import { wgpuKitHotReload } from 'wgpu-kit/vite';
export default defineConfig({ plugins: [wgpuKitHotReload()] });

hotKernelfunction

Connect one ElementKernel to matching Vite WGSL source-change events.

hotKernel(kernel: ElementKernel, hot: HotContext | undefined, file: string): void;

Parameters

NameTypeRequiredDefaultMeaning
kernelElementKernelYes—Kernel whose asynchronous replace(code) method is invoked for matching events. RawKernel is not the declared argument type.
hotHotContext | undefinedYes—Usually import.meta.hot. Passing undefined is an intentional no-op for production.
filestringYes—File suffix selector, normally ./sim.wgsl. Version 2.0.0 derives suffix with file.slice(file.lastIndexOf("/")); always include a slash.

Returns

void No return value.

Behavior & constraints

  • Matching uses only the suffix from the final slash, so equally named WGSL files in different directories can collide. A bare filename without slash degenerates to its final character.
  • Compilation failure is caught and logged; ElementKernel.replace retains the working old implementation. No unsubscribe, deduplication, or HMR-dispose registration is provided, even if hot.off exists.

Fields & related types

NameTypeRequiredDefaultMeaning
HotContext (supporting, not exported){ on(event: string, cb: (data: unknown) => void): void; off?(event: string, cb: (data: unknown) => void): void }——on is required and registers an event listener; off is optional and unused by this implementation. Event payload is checked for string code and file.

Examples

Register the client-side kernel using a slash-containing file path.
import { elementKernel } from 'wgpu-kit';
import { hotKernel } from 'wgpu-kit/vite';
import code from './sim.wgsl?raw';
const kernel = elementKernel({ state: { pos: 'vec2f' }, code });
hotKernel(kernel, import.meta.hot, './sim.wgsl');
// sim.wgsl must define a userFn compatible with this state schema.

Search concepts, APIs, and examples