We can define the Cordova Hooks as the special scripts that allow a user to automate the basic features of your Cordova project by using the Cordova CLI. It is a piece of code that can be added by any application, plugin developers, or even by your own build system to improve the development/deployment lifecycle of Apache Cordova application. However, these scripts of code are executed by the Cordova CLI at certain points in your Cordova application build for customizing the Cordova commands.
These scripts of code can be run many times in the build process.
We can relate these hooks as application activities such as before_build, after_build, etc. Furthermore, it may be related to our application plugins such as before_plugin_add, after_plugin_add, etc.
In general, the “hook” folder is available in our project’s root folder that consists of different subfolders like after_platform_add, after_build, before_platform_add, before_build, etc. For running hook scripts, it is being used.
The supported hook types are as follows:
|Hook Type||Associated Cordova Commands||Description|
|before_platform_add||cordova platform add||Before and after adding the platform, this hook type is executed.|
|before_platform_rm||cordova platform rm||Before and after removing a platform, this hook type is executed.|
|before_platform_ls||cordova platform ls||Before and after listing the installed and available platforms, this hook type is executed.|
cordova platform add
|Before and after preparing your application, this hook type is executed.|
|This hook type is executed before and after compiling your application.|
|Before deploying your application, this hook type is executed.|
|before_build||cordova build||Before and after building your application, this hook type is executed.|
|before_emulate||cordova emulate||Before and after emulating your application, this hook type is executed.|
|before_run||cordova run||Before and after running your application, this hook type is executed.|
|before_serve||cordova serve||Before and after serving your application, this hook type is executed.|
|before_clean||cordova clean||Before and after cleaning your application, this hook type is executed.|
|before_plugin_add||cordova plugin add||Before and after adding a plugin, this hook type is executed.|
|before_plugin_rm||cordova plugin rm||Before and after removing a plugin, this hook type is executed.|
|before_plugin_ls||cordova plugin ls||Before and after listing the plugins in the project, this hook type is executed.|
|before_plugin_install||cordova plugin add||This hook type is executed before and after installing a plugin for the platforms.|
|before_plugin_uninstall||cordova plugin rm||Before uninstalling a plugin, this hook type is executed.|
Ways to define hooks
The <hook> elements is used to define the Hooks in project’s config.xml file for example:
Plugin hooks (plugin.xml)
For defining the hook scripts as a plugin developer, we need to use the essential <hook> elements in the plugin.xml file:
Via/hooks directory (Deprecated)
To execute any custom actions occurred when fired the corresponding hook type, we need to use the hook type. This hook type is used for the subfolder inside ‘hooks’ directory. Here, we place our script file. It is necessary for the specified action.
Note that your scripts must be executable.
Order of Hooks execution
Based on Hooks Definition
The Hook scripts can be defined by adding the scripts to the special predefined folder i.e. via configuration files config.xml and plugin.xml, or hooks. However, you should know that the order of hooks is executed serially.
Based on internal order of the execution
We can fix the internal order of execution to the hooks.
Suppose, if the hooks are connected with before_compile, after_compile, before_prepare, after_prepare, before_build and after_build, then a build command will run to execute the hooks in the below order: