There are a couple conditions to where you might find this useful.
- You have to be using Grunt, which you should be using if you are not
- You have to be in a situation where you may not want to install/load every npm that the Gruntfile references.
I feel like point 2 needs more explaining so let me give you an example. At my job we use Grunt in both development and deployment. We have a similar deployment style to Heroku in that we push pure source code to git and then our deployment servers do anything that needs to be done like, concatinating and minifying JS and CSS. So we use grunt on our deployment servers to do this step.
The way we use Grunt in deployment differs from the way we use it in development, first off it is a small subset of commands that get run in deployment. We have no need for the
watch command/plugin so that dependency goes into the
devDependencies. Same with the
jshint package. On the otherhand we need
contrib-concat in every enviornment so it goes into the
dependencies. This allows us to use
npm install --production on our deployment servers and skip the npm packages we dont need. This would be great except we have all these calls to
grunt.loadNpmTasks(...) in our gruntfile that cause grunt to crash when the npm isn’t installed. This is where lazy loading comes in.
With lazy loading I am able to only load the npm tasks when one of them is actually called.
There is an additional usecase that I ran into briefly. When working on a large team sometimes coworkers will add new grunt plugins to your gruntfile, possibly something like a code complexity plugin. Your current workflow doesn’t need this plugin but now that it is there until you rerun
npm install you will get errors when you try to concatinate your code. Early on in a team’s working with grunt the gruntfile changes a lot more often and it can be mildly annoying.
There is also a theoretical boost in speed to the amount of time it takes grunt to run but you would never notice it. It does give us a nice boost in run time for deploys because the
npm install --production that is run on every deploy has less packages to download.
Checkout the source for grunt-lazyload
I am not going to go into how I did my implementation as I think the code is pretty self explanetory but if you have any questions let me know and I will address them.
A quick note about working with
npm: npm is amazing!