@kalxjs/plugins
Plugin system for KalxJs framework.
Installation
npm install @kalxjs/plugins
Usage
import { createApp } from '@kalxjs/core';
import { createPlugin, PluginManager, createLoggerPlugin, createPersistencePlugin } from '@kalxjs/plugins';
const myPlugin = createPlugin({
name: 'my-plugin',
install: (app, options) => {
app.myFeature = () => {
console.log('My feature is running!');
};
},
mounted: () => {
console.log('App has been mounted');
},
exposed: {
someUtility: () => {
}
}
});
const app = createApp({
});
const pluginManager = new PluginManager(app);
pluginManager.use(myPlugin, { });
pluginManager.use(createLoggerPlugin({ level: 'info' }));
pluginManager.use(createPersistencePlugin({
key: 'my-app-state',
paths: ['user', 'preferences']
}));
app.mount('#app');
Creating Plugins
Plugins are objects with an install
method and optional lifecycle hooks:
import { createPlugin } from '@kalxjs/plugins';
const myPlugin = createPlugin({
name: 'my-plugin',
install: (app, options, pluginManager) => {
app.myFeature = () => {
};
app.component('MyComponent', { });
const logger = pluginManager.getPlugin('logger');
if (logger) {
logger.exposed.logger.info('My plugin installed');
}
},
beforeCreate: () => { },
created: () => { },
beforeMount: () => { },
mounted: () => { },
beforeUpdate: () => { },
updated: () => { },
beforeUnmount: () => { },
unmounted: () => { },
errorCaptured: (err) => { },
exposed: {
someUtility: () => { },
someValue: 'value'
}
});
Built-in Plugins
Logger Plugin
import { createLoggerPlugin } from '@kalxjs/plugins';
const loggerPlugin = createLoggerPlugin({
level: 'info',
prefix: '[MyApp]',
enabled: true,
logTime: true,
logToConsole: true,
customLogger: null
});
app.logger.info('Application started');
app.logger.warn('Something might be wrong');
app.logger.error('Something went wrong', errorObject);
Persistence Plugin
import { createPersistencePlugin } from '@kalxjs/plugins';
const persistencePlugin = createPersistencePlugin({
key: 'my-app-state',
storage: localStorage,
paths: ['user', 'settings.theme'],
saveOnChange: true,
restoreOnStart: true,
serialize: JSON.stringify,
deserialize: JSON.parse
});
app.saveState();
app.restoreState();
Plugin Manager API
The PluginManager
class provides methods for managing plugins:
use(plugin, options)
: Register a plugin
callHook(hookName, ...args)
: Call a specific lifecycle hook
getPlugin(name)
: Get a plugin by name
hasPlugin(name)
: Check if a plugin is registered
getPlugins()
: Get all registered plugins
getExposed()
: Get all exposed methods and properties
License
MIT