Skip to content
GitHubXDiscord

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 triggery from '@triggery/vite';

// or
import { triggery } from '@triggery/vite';
function triggery(options?: TriggeryViteOptions): Plugin;

type TriggeryViteOptions = {
  readonly glob?: string | readonly string[];
};
FieldTypeDefaultDescription
globstring | readonly string[]'src/**/*.trigger.{ts,tsx,js,jsx}'Pattern(s) for trigger files. Resolved relative to the project root reported by Vite.
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>,
);
import { defineConfig } from 'vite';
import triggery from '@triggery/vite';

export default defineConfig({
  plugins: [
    triggery({
      glob: 'src/features/**/*.trigger.ts',
    }),
  ],
});
import { defineConfig } from 'vite';
import triggery from '@triggery/vite';

export default defineConfig({
  plugins: [
    triggery({
      glob: ['src/**/*.trigger.ts', 'packages/*/src/**/*.trigger.ts'],
    }),
  ],
});

Add to a global .d.ts so TypeScript stops complaining about the bare-string import:

declare module 'virtual:triggery-registry' {
  export {};
}