Search code examples
cordovaionic-frameworkhookphonegap-buildhybrid-mobile-app

Cordova hooks order


On the Cordova website a see a long list of hooks in alphabetical order, but what is the correct order in which they trigger and get executed?

I'm trying to add the cordova.js script to the index.html file's head before build/compile.

What happens first between before_build and before_compile, and why? What's the difference between the terms build and compile here?

Does build mean copying all the assets before compile the platform code? So my hook should be in the before_build directory?

Please confirm or correct me if I'm wrong.

Also a correct order of the hooks will be helpful to everybody since the documentation is unclear :)

EDIT:

Does cordova-cli automatically add the cordova.js script for me eventually, in the case it's missing? Because I was testing the hook, and even if it didn't work the script is already present in the html file.


Solution

  • Compile is the native compilation step while prepare is when Cordova does all the copying of preferences and assets. Build is a shortcut for running both of these. You can see this is the order that hooks are fired. (Tested on Cordova 5.4.0)

    On Cordova build:

    before_build
    before_prepare
    after_prepare
    before_compile
    after_compile
    after_build
    

    On Cordova prepare:

    before_prepare
    after_prepare
    

    On Cordova compile:

    before_compile
    after_compile
    

    On Cordova run:

    before_run
    before_prepare
    after_prepare
    after_run
    

    Interestingly run appears to fire the prepare hooks, but not the compile (or build) hooks. This could just be a bug.