Plugin Manager (advanced)
This page is about PluginManager: lifecycle, hooks, install/uninstall, and integration orchestration.
If your goal is only to add a reusable business type, start with Extension Overview and Custom DSL Types. PluginManager is the advanced layer for packaging and coordinating those capabilities.
Check whether you need a plugin
Overview
PluginManager is an independent plug-in manager responsible for:
- Register/install/uninstall plug-in
- Manage plugin hooks
- Provide
EventEmittercompatible event system - Expose plugin registry and hook table through
context
Important Note
PluginManageritself will not automatically connect to the execution processes ofs(),Validator, and various Exporters. If you wish to run certain hooks during the validation, compilation or export phases,pluginManager.runHook(...)needs to be called explicitly by your integration code.
quick start
Plug-in object structure
Parameter description
hook system
1. Automatically triggered built-in life cycle
These hooks are automatically triggered by PluginManager:
2. Conventional hook name
The following names are common conventions and PluginManager supports registering them, but whether they are executed depends on whether your code calls runHook()**:
hook()/runHook()supports any string name, not limited to the above table.
3. Register and run hook
4. Declare the hook in the plug-in
event system
PluginManager inherits from EventEmitter, so you can use:
on()once()off()emit()removeListener()removeAllListeners()
Available events
Example
API reference
register(plugin)
Register the plug-in and trigger it automatically:
onBeforeRegister- Write to the registry/Registration plug-in comes with hooks
onAfterRegisterplugin:registered
install(core, [pluginName], [options])
Install the plugin.
install(core): Install all registered plug-insinstall(core, 'name', options): Install the specified plug-ininstall()will pass the third parametercontextto the plug-in- Triggered after successful installation
plugin:installed - Trigger
plugin:errorwhen the installation fails, and then throw an error
unregister(name, [core])
Uninstall the plugin and remove the hooks registered by the plugin.
plugin.uninstall(core, context)The plug-in will be removed only after success.- Triggered after successful uninstall
plugin:uninstalled - Triggers
plugin:errorwhen uninstallation fails, and then throws an error
uninstall(name, [core])
Alias for unregister(), compatible with v1.
hook(name, handler)
Register a hook handler.
unhook(name, handler)
Remove the specified hook handler.
runHook(name, ...args)
Run all processors under a hook asynchronously and return the result array.
- An error thrown by a single handler will not interrupt subsequent handlers.
- It will be triggered when an error is thrown
hook:error onErrorwill be executed at the same time
has(name)
Check if the plugin is registered.
get([name])
get(name): Get a single plug-inget(): Get all plug-insMap
list()
Returns the plugin metadata array:
clear([core])
Uninstall all plug-ins one by one, clear the registry and hooks, and finally trigger plugins:cleared.
property
best practices
1. Naming
Use kebab-case:
2. Error handling
3. Resource cleanup
4. Communication between plug-ins
Troubleshooting
Plug-in not taking effect
Confirm that the plug-in is registered, installed, and install() indeed executes your logic.
hook not triggered
Check two things:
- Are the hook names consistent?
- Does your code really call
pluginManager.runHook('hookName',...)
How the current warehouse is published
The current warehouse has restored the v1 style official plug-in sub-path entry, which can be used directly:
schema-dsl/plugins/custom-formatschema-dsl/plugins/custom-validatorschema-dsl/plugins/custom-type-example
⚠️ Note: The official plug-in sub-path only completes the three sample plug-in entries that already exist in v1;
PluginManagerstill will not automatically connect to thevalidate()/compile()/ exporter process. Whether the hook is executed still depends on whether your integration code explicitly callsrunHook().
Related documents
Corresponding sample file
Example entry: plugin-system.ts
Description: Covers the registration/installation/uninstallation of custom plug-ins, runHook() execution results, and the installation effect of the official custom-format sub-path plug-in.