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.
Tonight I had the pleasure of collaborating with my friend Ross Martin over a project he put together. The project is called cordova-uglify and it focuses on uglifying/minifying JavaScript before building your Cordova app. See his comment in response to Dan Moore’s Accessing more build information from your Cordova CLI hooks blog for more information on why.
The project was having an after_prepare
hook in Cordova to uglify the application’s JavaScript once the code is put in place for iOS/Android.
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 npm install
).
This is what we’d get if we just used npm install cordova-uglify (notice uglify.js is only in node_modules
directory):
1 2 3 4 5 6 7 8 9 10 |
|
What we actually want has our uglify.js in our hooks/after_prepare
directory:
1 2 3 4 5 6 7 8 |
|
Then it hit me, we can use npm scripts!
The idea
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 postinstall
and postuninstall
.
The idea is this:
- You run
npm install cordova-uglify
- After installing, npm runs the
postinstall
script 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 – scripts/install.js
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 – scripts/uninstall.js
:
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.