2 - Minutes of manual minifying files
2-3 minutes is not a lot by itself, but when you add up those minutes when you have changes everyday, then you are bound to spend a lot more of your precious time on doing this tedious and boring work. And that's where bash script comes in handy, you are already using it in your terminal when you compile phonegap hybrid app, or when you install ruby or when you SSH into server.
I always thought bash script or anything you use in terminal makes you a hardcore programmer! That's my impression when I see people use terminal to access database server and application server to see where is the memory leaks and stuff, it makes me jawdrow, like literally, if reality allows.
I didn't know then, that using bash script allows me to do a lot of stuff that you normally would need a GUI that is programmed by other people to do something. And if what you need is from your own set of requirements, then you may need to write your own bash script to automate the tedious procedure that you want.
In my case, I am a Dojotoolkit evangelist. So, naturally, the amount of files for a typical complex web app will easily go up to hundreds of dependency files and widgets. And Dojotoolkit take AMD dependency injection to a whole new automagical level, which will allow your site to run lean and smooth while you can keep your source files organised and legible to a very good extent. Something that Angular.js has failed so far for me, hence my reluctance to move over to Angular. May be when they have figured out how to do widget properly, I will move over.
Almost Useless Dojo Build System
Dojotoolkits does have their build system, but there is a problem, there isn't even one good example of how to properly make a good build profile tutorial in the whole freaking site. It has good documentation, but those documentation is not good enough in my opinion. And require too much effort to learn something which should be painless and easy. Because the whole point is that you want people on board with you on Dojotoolkit, is that it's simpler and easier. I gave myself a good 1 months to tinker with how the build system works. I stopped after 1 week into the research because I find it too troublesome, and guest what, Dojotoolkit tutorial also feature someone else's custom boilerplate for building a profile because they themselves haven't figure how best to organise the file structures!
However, after using Dojotoolkits for all of my projects, including HTML canvas games. I have a verdict, it's good for everything. I even came up with a skeleton template that I can use for almost anything web and even hybrid app. The performance is there, the ease of use is there, the practicality is there, the reusability is there. Everything a web developer can dream of, and now I am tinkering into NodeJS. So might abandon PHP-based Codeigniter and give NodeJS a try in my future project that also runs on Dojo!
Ok, I mentioned my problem. Now comes the solution, I mentioned that I would have a lot of widget files and share elements classes across different folder structures that would require too much effort to minify them one by one and place them back one by one. So I went ahead and research about bash script and how it can automate this process, it took me around 15 minutes for me to search for the relevant syntaxes and command line before I can automate the whole freaking process!
To minify CSS, if you are already using SCSS or SASS, compass can already fulfil that requirement because you can choose to compact when it process your SCSS or SASS.
To minify HTML, since Dojotoolkit's strength lies mostly in the power of widget, the amount of HTML file is also relatively huge because you make a template of whatever you need to reuse. Hence, it's also a good idea to minify those HTML widget file. And for that, I use htmlcompressor found in Google Code. The result is fantastic!
Now comes to the juicy part where we need to stitch up a bash code that automate the process of minifying, and obfuscating.
In order to do that, we need to consider a few things, - Retain the current folder structures as AMD loading style will always refer to the same folder path where the class is defined in Dojo - To switch from minify from source folder should be just a change of application path
K_SRC="WHERE_YOU_SAVE_YOUR_FILE/custom" K_REL="WHERE_YOU_SAVE_YOUR_FILE/custom_min" rm -r $K_REL mkdir -p $K_REL cp -r $K_SRC/* $K_REL
Then we need to define those path that will be used over and over again in the entire script
#!/bin/bash K_SHRINKSAFE="java -jar WHERE_YOU_SAVE_YOUR_FILE/shrinksafe.jar" K_HTMLCOMPRESSOR="java -jar WHERE_YOU_SAVE_YOUR_FILE/htmlcompressor-1.5.3.jar"
The Magical Part
for file in `find "$K_SRC"`; do currentfile=$K_SRC"/"`echo "$file" | cut -d/ -f9-` desinationfile=$K_REL"/"`echo "$file" | cut -d/ -f9-` if [[ $currentfile == *.js* ]] then $K_SHRINKSAFE $currentfile > $desinationfile echo "JS - ".$desinationfile else $K_HTMLCOMPRESSOR $currentfile > $desinationfile echo "HTML - ".$desinationfile fi #echo "$file | cut -d/ -f9-" done