A Vite plugin that enables seamless integration between SvelteKit and Trigger.dev by allowing you to use your SvelteKit functions directly in your Trigger.dev projects.
npm install -D vite-plugin-triggerkit
vite.config.ts
:import { sveltekit } from '@sveltejs/kit/vite';
import { triggerkit } from 'vite-plugin-triggerkit';
export default defineConfig({
plugins: [
sveltekit(),
triggerkit({
includeDirs: ['src/lib', 'src/routes/api']
})
]
});
// src/lib/email.ts
/**
* Sends a welcome email to a new user
*/
export async function sendWelcomeEmail(userId: string) {
// Your email sending logic
return { message: `Hello, ${userId ?? "world"}`, };
}
import { sendWelcomeEmail } from "$lib";
import { logger, task, wait } from "@trigger.dev/sdk/v3";
export const helloWorldTask = task({
id: "hello-world",
// Set an optional maxDuration to prevent tasks from running indefinitely
maxDuration: 300, // Stop executing after 300 secs (5 mins) of compute
run: async (payload: any, { ctx }) => {
logger.log("Hello, world!", { payload, ctx });
const response = await sendWelcomeEmail(payload.userId);
await wait.for({ seconds: 5 });
return {
message: response.message,
}
},
});
The plugin accepts the following options:
interface PluginOptions {
// Directories to scan for exportable functions
includeDirs?: string[]; // default: ['src/lib', 'src/routes/api']
// File patterns to scan
include?: string[]; // default: ['**/*.ts', '**/*.js', '**/+server.ts']
// Patterns to exclude
exclude?: string[]; // default: ['**/node_modules/**', '**/*.test.ts', '**/*.spec.ts']
// Virtual module ID for accessing functions
virtualModuleId?: string; // default: 'virtual:sveltekit-functions'
}
You can access metadata about your functions using the exported functions
object:
import { functions } from 'virtual:sveltekit-functions';
console.log(functions.sendWelcomeEmail.metadata);
// Output:
// {
// isAsync: true,
// parameters: [
// { name: 'userId', type: 'string', optional: false }
// ],
// returnType: 'Promise<{ success: boolean }>',
// docstring: 'Sends a welcome email to a new user'
// }
Function Organization: Keep trigger-related functions in dedicated directories for better organization:
src/lib/triggers/
āāā email.ts
āāā notifications.ts
āāā users.ts
Type Safety: Always define types for function parameters and return values:
export async function createUser(data: UserData): Promise<User> {
// Implementation
}
Documentation: Add JSDoc comments to your functions for better developer experience:
/**
* Creates a new user in the database
* @param data - User creation data
* @returns Newly created user
*/
export async function createUser(data: UserData): Promise<User> {
// Implementation
}
// src/lib/auth.ts
export async function verifyUser(token: string): Promise<boolean> {
// Verification logic
}
// trigger/auth.ts
import { verifyUser } from 'virtual:sveltekit-functions';
export const userVerificationJob = trigger.job({
id: "verify-user",
run: async (payload) => {
const isValid = await verifyUser(payload.token);
if (!isValid) {
throw new Error('Invalid user token');
}
}
});
import { functions } from 'virtual:sveltekit-functions';
// Get all available functions
const availableFunctions = Object.keys(functions);
// Check function parameters
const params = functions.verifyUser.metadata.parameters;
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.