zhin-plugin-lifecycle
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZhin Plugin Lifecycle Guide
Zhin 插件生命周期指南
Use this skill to help developers scaffold and reason about Zhin plugins, including lifecycle hooks, auto-loading behavior, and hot-reload interactions.
使用本技能帮助开发者搭建和理解Zhin插件,包括生命周期钩子、自动加载行为以及热重载交互逻辑。
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`)
})从使用的最小化插件入口文件开始,这样Zhin就能为该文件创建一个插件实例:
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
- 会根据当前文件路径创建(并注册)插件实例。
usePlugin() - 插件名称由文件路径(包名或文件夹名)派生而来。
- 会在上下文挂载完成且子插件启动后执行。
plugin.onMounted - 会在插件停止或重载时执行。
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')
})在开发模式下(),核心模块会监听插件文件,当文件发生变化时触发重载。
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 51- 文件变化触发。
plugin.reload() - 通过停止插件。
plugin.stop() - 父插件重新导入插件入口文件。
- 再次触发生命周期事件。
mounted
如果该插件是根插件,重载会终止进程(退出码51),以便CLI重新启动它。
Adding 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.
使用加载相对于当前文件的子插件:
plugin.import()ts
await plugin.import('./sub-plugin/index.ts')注意:
- 核心模块会防止重复加载相同的解析路径。
- 当父插件停止时,子插件会自动停止。
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.
- 会返回已注册的命令、组件、定时任务和中间件名称。
plugin.features - 会返回插件及其子插件的嵌套特性树。
plugin.info()
可使用这些方法构建用于调试插件加载情况的“健康检查”命令。
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
- 确保入口文件仅调用一次。
usePlugin() - 使用清理定时器或外部连接。
onDispose - 使用加载依赖插件。
plugin.import() - 优先使用在上下文准备完成后进行延迟初始化。
plugin.onMounted