zhin-plugin-lifecycle
Original:🇺🇸 English
Translated
Guides creation of Zhin plugins with lifecycle hooks, auto-loading, and hot-reload behavior. Use when developers need plugin structure, lifecycle events, or reloading details.
6installs
Sourcezhinjs/ai-skills
Added on
NPX Install
npx skill4agent add zhinjs/ai-skills zhin-plugin-lifecycleTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Zhin Plugin Lifecycle Guide
Use this skill to help developers scaffold and reason about Zhin plugins, including lifecycle hooks, auto-loading behavior, and hot-reload interactions.
Plugin Scaffold
Start with the minimal plugin entry file that uses so Zhin can create a plugin instance for the file:
usePlugin()ts
import { usePlugin } from '@zhin.js/core'
const plugin = usePlugin()
plugin.onMounted(() => {
plugin.logger.info(`Plugin ${plugin.name} mounted`)
})
plugin.onDispose(() => {
plugin.logger.info(`Plugin ${plugin.name} disposed`)
})Key Concepts
- creates (and registers) the plugin instance based on the current file path.
usePlugin() - Plugin names are derived from the file path (package name or folder name).
- runs after contexts are mounted and child plugins start.
plugin.onMounted - runs when the plugin is stopped or reloaded.
plugin.onDispose
Hot Reload Behavior
In development mode (), the core will watch plugin files and trigger reloads when they change.
NODE_ENV=developmentts
plugin.onMounted(() => {
plugin.logger.debug('Plugin ready for HMR')
})Reload Flow
- File changes trigger .
plugin.reload() - The plugin is stopped via .
plugin.stop() - Parent re-imports the plugin entry file.
- lifecycle events fire again.
mounted
If the plugin is the root plugin, reload exits the process () so the CLI can restart it.
exit code 51Adding Child Plugins
Use to load child plugins relative to the current file:
plugin.import()ts
await plugin.import('./sub-plugin/index.ts')Notes:
- The core prevents double-loading the same resolved path.
- Child plugins are stopped automatically when the parent stops.
Common Diagnostics
- returns registered commands, components, crons, and middleware names.
plugin.features - returns a nested tree of features for the plugin and its children.
plugin.info()
Use this to build "health" commands for debugging plugin loads.
Checklist When Authoring Plugins
- Ensure the entry file calls once.
usePlugin() - Use to clean up timers or external connections.
onDispose - Use for dependent plugins.
plugin.import() - Prefer for deferred setup once contexts are ready.
plugin.onMounted