Vite Plugin

The realtime() Vite plugin scans src/live/ and generates $live/* virtual imports.

Setup

// vite.config.js
import { sveltekit } from '@sveltejs/kit/vite';
import uws from 'svelte-adapter-uws/vite';
import realtime from 'svelte-realtime/vite';

export default {
  plugins: [sveltekit(), uws(), realtime()]
};

How it works

  1. The plugin watches src/live/ for .js and .ts files
  2. It parses each file for live(), live.stream(), live.cron(), etc. exports
  3. It generates client stub modules under the $live/ virtual import prefix
  4. live() exports become async RPC functions
  5. live.stream() exports become Svelte store factories
  6. Server-only code is stripped from the client bundle

What gets generated

For a file src/live/chat.js that exports:

  • sendMessage via live()
  • messages via live.stream()

The plugin generates $live/chat with:

  • sendMessage(text) - calls the server function over WebSocket
  • messages - a Svelte store that subscribes to the 'messages' topic

File structure

src/live/
├── chat.js        => $live/chat
├── todos.js       => $live/todos
└── admin/
    └── users.js   => $live/admin/users

Nested directories map to nested import paths.

Was this page helpful?