For those of you that don’t know, Cordova has hooks that can run on each of the specific build tasks that Cordova goes through. For example the task that happens after all the platform specific code is set up, the
after_prepare hook is fired.
The project was having an
This project Ross put together was interesting. There have been some blogs on using hooks in Cordova as well as three hooks every cordova / phonegap project needs. Moving forward, it’d be nice to make some of these hooks and share them out much like we share packages on npm.
The only problem with using them as packages, is we need to place code somewhere outside of the
node_modules folder (where the package will be installed from
This is what we’d get if we just used npm install cordova-uglify (notice uglify.js is only in
1 2 3 4 5 6 7 8 9 10
What we actually want has our uglify.js in our
1 2 3 4 5 6 7 8
Then it hit me, we can use npm scripts!
Let’s package up Cordova tools, publish on npm, and then use npm scripts to install/uninstall them as necessary.
npm gives its package owners the ability to run scripts on various events in the npm life cycle. The interesting ones we care about are those being
The idea is this:
- You run
npm install cordova-uglify
- After installing, npm runs the
postinstallscript to copy files into proper location
- Profit $$$
Ross put me up to the challenge, so I took it up. Here’s what I put our package.json to be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
To which I then created a quick script to do the file copying –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
As well as uninstalling it –
1 2 3 4 5 6 7 8 9 10 11 12
Simple and sweet.
Now, is it an anti-pattern? I’m not sure.
Does it make it easier for other developers to get started and using it? Yes.
That’s exactly what I was going for.
Thanks Ross for planting the idea in my head, and more importantly, for the challenge to learn.