前言
hooks是一些在Cordova执行命令时运行的特殊脚本,能够允许你拓展Cordova命令来适应你自己的需求。官方推荐三个地方定义hooks,分别在工程的config.xml、插件的plugin.xml和hooks文件夹,这里介绍的是第三个地方。任何一门编程语言都能够写hooks,但是考虑到跨平台运行(即需要不同语言的运行环境),推荐使用Node.js。
一、hooks应用场景
hooks应用场景一:修改应用配置信息,我们在应用开发中,引入的插件可能有冲突之类的问题,这个时候原生开发人员会让我们在添加平台加一些配置信息。也就是说我们每次添加平台后都需要手动去改平台中代码,不太方便,程序员就要尽可能的懒起来,这个时候就可以用上hooks了;
hooks应用场景二:环境切换,我们的Cordova工程经常有开发、uat、生产等不同环境。对应不同的环境,我们有不同的接口地址、不同的插件API keys,甚至不同的应用图标和Splash。对于不同接口地址、不同的应用图标和Splash等,我们可以使用自定义gulp任务实现,但是对于插件中的接口地址、API keys不太好做处理。
二、Cordova支持的hooks类型:
Hook Type | Associated Cordova Commands | Description |
---|---|---|
before_platform_add | cordova platform add |
To be executed before and after adding a platform. |
after_platform_add | ||
before_platform_rm | cordova platform rm |
To be executed before and after removing a platform. |
after_platform_rm | ||
before_platform_ls | cordova platform ls |
To be executed before and after listing the installed and available platforms. |
after_platform_ls | ||
before_prepare | cordova prepare cordova platform add cordova build cordova run |
To be executed before and after preparing your application. |
after_prepare | ||
before_compile | cordova compile cordova build |
To be executed before and after compiling your application. |
after_compile | ||
before_build | cordova build |
To be executed before and after building your application. |
after_build | ||
before_emulate | cordova emulate |
To be executed before and after emulating your application. |
after_emulate | ||
before_run | cordova run |
To be executed before and after running your application. |
after_run | ||
before_serve | cordova serve |
To be executed before and after serving your application. |
after_serve | ||
before_clean | cordova clean |
To be executed before and after cleaning your application. |
after_clean | ||
pre_package | N/A | Applicable to Windows 8 and Windows Phone only. This hook is deprecated. |
before_plugin_add | cordova plugin add |
To be executed before and after adding a plugin. |
after_plugin_add | ||
before_plugin_rm | cordova plugin rm |
To be executed before and after removing a plugin. |
after_plugin_rm | ||
before_plugin_ls | cordova plugin ls |
To be executed before and after listing the plugins in your application. |
after_plugin_ls | ||
before_plugin_search | cordova plugin search |
To be executed before and after a plugin search. |
after_plugin_search |