This comprehensive guide walks you through creating, testing, and publishing plugins for Eleva.
Here’s a complete, minimal example of creating and registering a custom plugin:
import Eleva from "eleva";
// 1. Define the plugin
const UtilsPlugin = {
name: "utils", // Required: unique identifier
version: "1.0.0", // Required: semantic version string
install(eleva, options = {}) { // Required: called when plugin is registered
// Add utility methods to the Eleva instance
eleva.utils = {
formatDate: (date) => new Date(date).toLocaleDateString(),
capitalize: (str) => str.charAt(0).toUpperCase() + str.slice(1),
debounce: (fn, delay = 300) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
};
}
};
// 2. Create Eleva instance
const app = new Eleva("MyApp");
// 3. Register the plugin with app.use()
app.use(UtilsPlugin); // Without options
// OR
app.use(UtilsPlugin, { locale: "en-US" }); // With options
// 4. Use the plugin in components
app.component("DateDisplay", {
setup: () => ({
formatted: app.utils.formatDate(Date.now()) // Access via app instance
}),
template: (ctx) => `<p>Today: ${ctx.formatted}</p>`
});
app.mount(document.getElementById("app"), "DateDisplay");
Before you start, ensure you have:
An Eleva plugin is a JavaScript object that extends the framework’s functionality. Every plugin must have:
name property (string)version property following semantic versioning (string)install method that receives the Eleva instance and optionsconst MyPlugin = {
name: "myPlugin",
version: "1.0.0",
install(eleva, options) {
// Plugin implementation
}
};
| Property | Type | Required | Description |
|---|---|---|---|
name |
string |
Yes | Unique identifier for the plugin |
version |
string |
Yes | Semantic version (e.g., “1.0.0”, “2.1.3”) |
install |
function |
Yes | Called with (eleva, options) during registration |
uninstall |
function |
No | Optional cleanup when plugin is removed |
Plugins are registered using the app.use() method. This method calls the plugin’s install function with the Eleva instance and any provided options.
import Eleva from "eleva";
import MyPlugin from "./my-plugin.js";
const app = new Eleva("MyApp");
// Register without options
app.use(MyPlugin);
// Register with options
app.use(MyPlugin, { option1: "value1", option2: true });
Plugins are installed in the order they are registered. If plugins depend on each other, register dependencies first:
// Logger should be registered before Analytics (Analytics uses Logger)
app.use(LoggerPlugin);
app.use(AnalyticsPlugin); // Can now use app.log from LoggerPlugin
After registration, plugin features are available on the Eleva instance:
// In your plugin
install(eleva, options) {
eleva.myFeature = { /* ... */ };
}
// After registration
app.use(MyPlugin);
console.log(app.myFeature); // Available immediately
// In components via the app instance
app.component("MyComponent", {
setup: () => ({
doSomething: () => app.myFeature.someMethod()
}),
template: (ctx) => `<button @click="doSomething">Click</button>`
});
The app.use() method returns the result of plugin.install(). Use this for plugins that expose an API:
// Plugin that returns an API
const RouterPlugin = {
name: "router",
version: "1.0.0",
install(eleva, options) {
const router = new Router(eleva, options);
eleva.router = router;
return router; // Return for direct access
}
};
// Usage
const router = app.use(RouterPlugin, { routes: [...] });
await router.start(); // Direct access to returned API
plugin.install(eleva, options)install() is returned to the caller| ← Back to Examples | Development Guide → |