In the "Plugin object" section we know that the plugin object contains a setup
function that not only contains an api
object parameter, but also returns a Hooks object.
The setup
function of the plugin will provide an api
object parameter, and you can call some of the methods provided on this object to get information about the configuration, project context, etc.
api.useAppContext
Used to get project context information.
Through the actual type file, we can see that there are some additional fields. However, for Modern.js Module, the fields mentioned above are the only ones that are meaningful. The same applies to other methods of the api
object.
api.useResolvedConfigContext
Used to get the final configuration after parsing.
If you need to get the build-related final configuration, you need to use the beforeBuild
Hook.
api.useHookRunners
Used to get the executors of Hooks and trigger the execution of a specific Hook.
The setup of a CLI plugin can be an asynchronous function that performs asynchronous logic during the initialization process.
Note that the setup function of the next plugin is not executed until the async setup function of the current plugin has finished. Therefore, you should avoid performing time-consuming asynchronous operations in the setup function to avoid slowing down the startup performance of the CLI.
We know that the setup
function returns a Hooks object, which can also be understood as an object with Modern.js Module lifecycle hooks.
Currently there are two main types of hooks.
build
command is executed to build the source code product.buildPlatform
hook: triggered only when the build --platform
command is executed to generate other build artifacts.dev
command.See the API documentation for a full list of lifecycle hooks.