Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage cordova plugins with npm + package.json

What Caused Our Issue

We had originally used this Ionic + Cordova + Grunt seed project to spawn our initial app. The project includes a number of Cordova hooks that, among other things, add and remove platforms and plugins from the relevant cordovaPlatforms and cordovaPlugins sections in package.json when you run the corresponding command (i.e. cordova plugin add cordova-plugin-media adds a line to cordovaPlugins).

To better support local testing (trying new versions of a plugin, for example), and to prevent cross-dev dependency issues, we disabled the seed project hook and now hand-craft the package.json as needed.

Properly Managing Cordova Plugins

In turns out, the Ionic CLI uses package.json to manage Cordova app state in terms of platforms and plugins (as of version 1.3.19, it appears).

Populating package.json with two sections, cordovaPlatforms and cordovaPlugins has allowed us to do a simple ionic state restore to get the Cordova environment in shape for emulation, building, etc.

Specifying Versions

To further lock-down our app's state and development environment, we've also specified the target version of the Cordova platforms and plugins that we are using by adding the version number. Here's what we use:

{
  ...
  "cordovaPlatforms": [
    "[email protected]",
    "[email protected]"
  ],
  "cordovaPlugins": [
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]"
  ]
}

tl;dr

Once you have the above in your package.json, you can then ensure that your local environment is in the proper state via ionic state restore (v1.3.19+) which will chew through package.json and install platforms and plugins as needed.


You can add a postinstall command. Look below

{
  "cordovaPlugins": [
    "[email protected]",
  ],
  "cordovaPlatforms": [
    "[email protected]",
  ],
  "scripts": {
    "postinstall": "ionic state restore",
    "clean": "ionic platform remove android; ionic platform remove ios; ionic platform remove browser; git checkout package.json"
  }
}

Bonus : use npm run clean followed by npm install if you wish to start clean i.e reinstall all platforms


may be it's a bit late into the game, but this is my postinstall script

    "postinstall": "bower i && gulp && ionic state reset && ionic config build"
  • it installs the bower dependencies, e.g. ionic lib
  • it restores Cordova plugins
  • it re-builds the configuration you did via ionic config command