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
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
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
// 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
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
kernel | ElementKernel | Yes | — | Kernel whose asynchronous replace(code) method is invoked for matching events. RawKernel is not the declared argument type. |
hot | HotContext | undefined | Yes | — | Usually import.meta.hot. Passing undefined is an intentional no-op for production. |
file | string | Yes | — | 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
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
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
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.Vite WGSL 源码热更新,明确说明路径匹配和生命周期限制。
辅助输入、dispatch count、托管批次与类型化内核已随 2.1.0 发布。原 API 仍以 npm 2.0.0 声明与实现为历史核对基线;2.1.0 和 2.2.0 新增能力在对应 API 中注明并引用 src 文件,不代表整份 API 重新审计。只列出此入口实际导出的公开 API。
源码参考: 1 ↗
wgpuKitHotReloadfunction
创建轻量 Vite 插件,广播变化后的 .wgsl 源码。
wgpuKitHotReload(): VitePluginLike;参数
此调用不接收参数。
返回值
VitePluginLike 名为 wgpu-kit:hot-reload、含 handleHotUpdate 钩子的插件。
行为与约束
- 仅处理以 .wgsl 结尾的文件。钩子读取源码,通过 wgpu-kit:kernel 自定义事件发送 { file, code },并返回 [],抑制该变更的常规模块更新路径。
- 读取/广播在未等待的异步任务中执行,没有错误处理器;该助手不在服务端编译 WGSL。
字段与关联类型
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
VitePluginLike (supporting, not exported) | { name: string; apply?: string; handleHotUpdate?(ctx: ViteHotUpdateCtx): unknown } | — | — | 最小结构化返回类型。实际结果含 name 和 handleHotUpdate,不设置 apply。 |
ViteHotUpdateCtx (supporting, not exported) | { file: string; read(): string | Promise<string>; server: { ws: { send(payload: { type: string; event: string; data: unknown }): void } } } | — | — | 钩子上下文:file 为变化路径;read 获取源码;server.ws.send 发布自定义消息。 |
示例
// vite.config.ts
import { defineConfig } from 'vite';
import { wgpuKitHotReload } from 'wgpu-kit/vite';
export default defineConfig({ plugins: [wgpuKitHotReload()] });hotKernelfunction
将一个 ElementKernel 连接到匹配的 Vite WGSL 源码变更事件。
hotKernel(kernel: ElementKernel, hot: HotContext | undefined, file: string): void;参数
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
kernel | ElementKernel | 是 | — | 匹配事件到达时调用其异步 replace(code) 的 kernel;声明参数类型不是 RawKernel。 |
hot | HotContext | undefined | 是 | — | 通常传 import.meta.hot;传 undefined 时什么都不做,适用于生产环境。 |
file | string | 是 | — | 文件后缀选择器,通常为 ./sim.wgsl。2.0.0 通过 file.slice(file.lastIndexOf("/")) 提取后缀;务必包含斜杠。 |
返回值
void 无返回值。
行为与约束
- 只按最后一个斜杠起的后缀匹配,不同目录同名 WGSL 文件可能冲突。没有斜杠的裸文件名会退化为只取最后一个字符。
- 编译失败会被捕获并记录;ElementKernel.replace 保留可工作的旧实现。即便 hot.off 存在,也不提供取消订阅、去重或 HMR dispose 注册。
字段与关联类型
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
HotContext (supporting, not exported) | { on(event: string, cb: (data: unknown) => void): void; off?(event: string, cb: (data: unknown) => void): void } | — | — | on 必填,用于注册事件监听;off 可选,但当前实现未调用。事件载荷须含字符串 code 和 file。 |
示例
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.