triggery (Vite plugin)
Stable · since 0.1.0
A Vite plugin that globs every *.trigger.{ts,tsx,js,jsx} file in the project at build time and generates a virtual module — virtual:triggery-registry — that imports all of them. Import the virtual module once at your app entry point and every trigger registers itself with the runtime automatically; no manual wiring, no per-file import './foo.trigger' lines.
HMR-aware: editing an existing trigger file re-runs its createTrigger(...) call and the runtime’s last-mount-wins replaces the old registration. Adding, removing, or renaming a trigger file invalidates the virtual module so the import list is rebuilt on the next request.
Import
Section titled “Import”import triggery from '@triggery/vite';
// or
import { triggery } from '@triggery/vite';
Signature
Section titled “Signature”function triggery(options?: TriggeryViteOptions): Plugin;
type TriggeryViteOptions = {
readonly glob?: string | readonly string[];
};Options
Section titled “Options”| Field | Type | Default | Description |
|---|---|---|---|
glob | string | readonly string[] | 'src/**/*.trigger.{ts,tsx,js,jsx}' | Pattern(s) for trigger files. Resolved relative to the project root reported by Vite. |
Examples
Section titled “Examples”Default — drop into vite.config
Section titled “Default — drop into vite.config”import { defineConfig } from 'vite';
import triggery from '@triggery/vite';
export default defineConfig({
plugins: [triggery()],
});Then in your app entry:
// src/main.tsx
import 'virtual:triggery-registry';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { App } from './App';
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
);Custom glob
Section titled “Custom glob”import { defineConfig } from 'vite';
import triggery from '@triggery/vite';
export default defineConfig({
plugins: [
triggery({
glob: 'src/features/**/*.trigger.ts',
}),
],
});Multiple globs
Section titled “Multiple globs”import { defineConfig } from 'vite';
import triggery from '@triggery/vite';
export default defineConfig({
plugins: [
triggery({
glob: ['src/**/*.trigger.ts', 'packages/*/src/**/*.trigger.ts'],
}),
],
});Typing the virtual module
Section titled “Typing the virtual module”Add to a global .d.ts so TypeScript stops complaining about the bare-string import:
declare module 'virtual:triggery-registry' {
export {};
}Related
Section titled “Related” createTrigger The constructor each `*.trigger.ts` file calls.
createRuntime Triggers register against the default runtime by default.
Vite project layout guide Conventions for organizing trigger files.
CLI scaffold trigger Generate a new `*.trigger.ts` from the terminal.