The mionPlugin from @mionkit/devtools/vite-plugin is the single Vite plugin that handles all mion build-time features: deepkit type reflection, pure function extraction, and AOT cache generation.
import {mionPlugin} from '@mionkit/devtools/vite-plugin';
mionPlugin({
// type metadata for RunTypes (omit to disable)
runTypes?: {
tsConfig?: string; // Path to tsconfig.json
include?: string | string[]; // Glob patterns (default: ['**/*.tsx', '**/*.ts'])
exclude?: string | string[]; // Glob patterns (default: 'node_modules/**')
reflection?: 'default' | 'explicit' | 'never';
compilerOptions?: CompilerOptions;
},
// Pure function extraction from client code (omit to disable)
serverPureFunctions?: {
clientSrcPath: string; // Path to client package source directory
include?: string[]; // Glob patterns (default: ['**/*.ts', '**/*.tsx'])
exclude?: string[]; // Glob patterns (default: ['**/node_modules/**', '**/.dist/**', '**/dist/**'])
},
// AOT cache generation (omit to disable)
aotCaches?: {
startServerScript?: string; // Server start script for full route compilation
serverViteConfig?: string; // Server's vite.config.ts for vite-node
excludedFns?: string[]; // JIT function IDs to exclude
excludedPureFns?: string[]; // Pure function names to exclude
cache?: boolean | string; // Disk cache: true (default), false, or custom path
},
});
The server package needs runTypes for type reflection and serverPureFunctions to receive pure functions defined in client code.
import {defineConfig} from 'vite';
import {resolve} from 'path';
import {mionPlugin} from '@mionkit/devtools/vite-plugin';
export default defineConfig({
plugins: [
mionPlugin({
runTypes: {
tsConfig: resolve(__dirname, 'tsconfig.json'),
},
// Scan client source for pureServerFn() and mapFrom() calls
serverPureFunctions: {
clientSrcPath: resolve(__dirname, '../client/src'),
},
}),
],
build: {
lib: {
entry: resolve(__dirname, 'src/init.ts'),
formats: ['es'],
},
rollupOptions: {
external: [/^[^./]/],
},
},
});
The runTypes option enables deepkit's type compiler, which preserves TypeScript type metadata at runtime. This is required for mion's validation and serialization to work.
| Option | Default | Description |
|---|---|---|
tsConfig | auto-discovered | Path to tsconfig.json |
include | ['**/*.tsx', '**/*.ts'] | Files to transform |
exclude | 'node_modules/**' | Files to skip |
reflection | from tsconfig | Override reflection mode ('default' enables for all files) |
compilerOptions | from tsconfig | Additional TypeScript compiler options |
The serverPureFunctions option tells the plugin where to find client source code containing pureServerFn(), registerPureFnFactory(), and mapFrom() calls. The plugin scans these files, extracts the pure functions, and bundles them into a virtual module that the server imports automatically.
| Option | Default | Description |
|---|---|---|
clientSrcPath | required | Path to the client package source directory |
include | ['**/*.ts', '**/*.tsx'] | File patterns to scan |
exclude | ['**/node_modules/**', '**/.dist/**', '**/dist/**'] | File patterns to skip |
The client package needs runTypes for type reflection and aotCaches to pre-compile JIT functions.
import {defineConfig} from 'vite';
import {resolve} from 'path';
import {mionPlugin} from '@mionkit/devtools/vite-plugin';
export default defineConfig({
plugins: [
mionPlugin({
runTypes: {
tsConfig: resolve(__dirname, 'tsconfig.json'),
},
aotCaches: {
// Point to the server's start script for full route compilation
startServerScript: resolve(__dirname, '../server/src/init.ts'),
serverViteConfig: resolve(__dirname, '../server/vite.config.ts'),
},
}),
],
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
formats: ['es', 'cjs'],
},
rollupOptions: {
external: [/^[^./]/],
},
},
});
The aotCaches option enables AOT cache generation. The plugin runs your server start script via vite-node to capture all compiled JIT functions, pure functions, and router metadata.
| Option | Default | Description |
|---|---|---|
startServerScript | internal default routes | Server script that initializes the router |
serverViteConfig | auto-discovered | Server's vite.config.ts for proper transforms |
excludedFns | [] | JIT function IDs to exclude from cache |
excludedPureFns | [] | Pure function names to exclude from cache |
cache | true | Disk cache: true uses Vite's cache dir, string for custom path, false to disable |
If startServerScript is not provided, the plugin generates caches for mion's internal routes only. Your custom routes will be fetched at runtime via fetchRemoteMethodsMetadata(). For best performance, provide your server's start script to pre-cache all routes.
Set MION_AOT_FORCE=true environment variable to force AOT regeneration regardless of the disk cache.
A typical mion project has a server and client package:
my-app/
├── server/
│ ├── src/
│ │ ├── init.ts # Server entry point
│ │ └── routes.ts # Route definitions
│ ├── vite.config.ts
│ └── tsconfig.json
├── client/
│ ├── src/
│ │ └── index.ts # Client code with pureServerFn, mapFrom
│ ├── vite.config.ts
│ └── tsconfig.json
└── package.json
import {defineConfig} from 'vite';
import {resolve} from 'path';
import {mionPlugin} from '@mionkit/devtools/vite-plugin';
export default defineConfig({
plugins: [
mionPlugin({
runTypes: {
tsConfig: resolve(__dirname, 'tsconfig.json'),
compilerOptions: {sourceMap: true},
},
serverPureFunctions: {
clientSrcPath: resolve(__dirname, '../client/src'),
},
}),
],
build: {
lib: {
entry: resolve(__dirname, 'src/init.ts'),
formats: ['es'],
},
outDir: 'dist',
rollupOptions: {
external: [/^[^./]/],
},
},
});
import {defineConfig} from 'vite';
import {resolve} from 'path';
import {mionPlugin} from '@mionkit/devtools/vite-plugin';
export default defineConfig({
plugins: [
mionPlugin({
runTypes: {
tsConfig: resolve(__dirname, 'tsconfig.json'),
},
aotCaches: {
startServerScript: resolve(__dirname, '../server/src/init.ts'),
serverViteConfig: resolve(__dirname, '../server/vite.config.ts'),
},
}),
],
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
formats: ['es', 'cjs'],
},
outDir: 'dist',
rollupOptions: {
external: [/^[^./]/],
},
},
});
To get TypeScript support for virtual module imports, add the type definitions to your tsconfig.json:
{
"compilerOptions": {
"types": ["@mionkit/devtools/virtual-modules"]
}
}
This provides types for all virtual modules:
// AOT caches — combined module that auto-registers all caches
import 'virtual:mion-aot/caches';
// Or import individual cache modules
import {jitFnsCache} from 'virtual:mion-aot/jit-fns';
import {pureFnsCache} from 'virtual:mion-aot/pure-fns';
import {routerCache} from 'virtual:mion-aot/router-cache';
// Server pure functions — extracted from client code
import {serverPureFnsCache} from 'virtual:mion-server-pure-fns';
During development, the plugin supports hot module replacement:
clientSrcPath change, the plugin re-scans for pure functions and invalidates the virtual:mion-server-pure-fns modulestartServerScript change, the plugin regenerates AOT caches and invalidates all AOT virtual modules