Home » Cordova Hooks

Cordova Hooks

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 TypeAssociated Cordova CommandsDescription
before_platform_addcordova platform addBefore and after adding the platform, this hook type is executed.
after_platform_add
before_platform_rmcordova platform rmBefore and after removing a platform, this hook type is executed.
after_platform_rm
before_platform_lscordova platform lsBefore and after listing the installed and available platforms, this hook type is executed.
after_platform_ls
before_preparecordova prepare
cordova platform add
cordova build
cordova run
Before and after preparing your application, this hook type is executed.
after_prepare
before_compilecordova compile
cordova build
This hook type is executed before and after compiling your application.
after_compile
before_deploycordova emulate
cordova run
Before deploying your application, this hook type is executed.
before_buildcordova buildBefore and after building your application, this hook type is executed.
after_build
before_emulatecordova emulateBefore and after emulating your application, this hook type is executed.
after_emulate
before_runcordova runBefore and after running your application, this hook type is executed.
after_run
before_servecordova serveBefore and after serving your application, this hook type is executed.
after_serve
before_cleancordova cleanBefore and after cleaning your application, this hook type is executed.
after_clean
before_plugin_addcordova plugin addBefore and after adding a plugin, this hook type is executed.
after_plugin_add
before_plugin_rmcordova plugin rmBefore and after removing a plugin, this hook type is executed.
after_plugin_rm
before_plugin_lscordova plugin lsBefore and after listing the plugins in the project, this hook type is executed.
after_plugin_ls
before_plugin_installcordova plugin addThis hook type is executed before and after installing a plugin for the platforms.
after_plugin_install
before_plugin_uninstallcordova plugin rmBefore uninstalling a plugin, this hook type is executed.

Ways to define hooks

Config.xml

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.

Example:

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:

before_build
      before_prepare
      after_prepare
      before_compile
      after_compile
after_build

Next TopicCordova Plugins

You may also like