Grunt.js: Tips and Tricks
quickleft.com
quickleft.com
1.) As a build tool
2.) As a continuous build tool (with watch and live reload)
React's Gruntfile is well organized and is a great option for #1.
However, use of this approach for #2 will cause a slower development process. Obviously, part of the performance hit could be offset by conditionally including only the files required for each task. However, reading files from disk will result in a slower reload.
Grunt is to JavaScript as Ant is to Java. "The main usage of Ant is to build Java applications." So, "The main usage of Grunt is to build JavaScript applications".
I'm making a game and I use it for many different tasks:
1. Every time a coffeescript file changes, compile that file to javascript
2. Every time a js file is added or deleted, change the list of script tags in the html file
3. Every time a sass file changes, run compass to compile it into a css file
4. I've got a test suite (qunit, it runs off an html file) that uses the same script list that my main game uses (and more). I've turned the script list into a variable so I don't have to modify either html file by hand if I add/remove a coffeescript file.
5. Every time a js file changes, is added or deleted, run unit tests in the background using phantomjs
6. If a unit test fails, beep 3 times
7. Every time a coffescript file changes, is added or deleted, reload the page so I can see my change. This is configurable so I can turn this feature off.
8. When I build for production, concatenate all the files and minify them.
9. Every time any file changes, update the html file to have a new build-time variable.
I would be super unproductive if I had to do these tasks by hand. Grunt.js lets me automate these tasks and reuse them as many times as I want. For example, I used to run my qunit tests by hand by going to the .html file and refreshing the browser. But sometimes I forgot or I didn't want to lose my train of thought. Sometimes this would lead to me accidentally committing broken code. That's why I automated the tests to run in the background whenever a file changes. Now I can't forget to run the unit tests and I don't have to lose my train of thought.
Grunt is simply too complicated for a lightweight such as myself and I wish the tooling existed to make the pain go away.
RequireJS is incredible when it comes to managing dependencies and organizing JS code in general. Combine it with a requirejs task in Grunt to automatically optimize your JS code and you've got a real slick development-to-production JS pipeline on your hands.
Granted, RequireJS may seem a little funky at first, but I promise you taking the couple of hours necessary to learn its basics will make your life (at least when it comes to developing JS) much easier.
I really hate the amount of effort this took but it was worth it. I feel like this should be a first class feature/plugin. The problem with lineman's solution is that when you concatenate everything, your stack traces are obfuscated. EG: "An error occurred on line 1,000,000 of concatenated.js". Also, it slows your feedback loop to wait for the concatenation to occur. Also it slows my IDE to view and index a huge concatenated file.
Another thing I hate about grunt's watch plugin is how much code it takes to make the watch plugin only act on the changed files instead of all the files it's watching. EG: When a coffeescript file changes, only compile that file, not all the coffeescript files. That takes like 5 lines of code. It should be an option I can set to true.