Is there a generic way to consume my dependency's grunt build process?

Problem

Let's say I have a project where I want to use Lo-Dash and jQuery, but I don't need all of the features.

Sure, both these projects have build tools so I can compile exactly the versions I need to save valuable bandwidth and parsing time, but I think it's quite uncomfortable and ugly to install both of them locally, generate my versions and then check them it into my repository.

Much rather I'd like to integrate their grunt process into my own and create custom builds on the go, which would be much more maintainable.

The Lo-Dash team offers this functionality with a dedicated cli and even wraps it with a grunt task. That's very nice indeed, but I want a generic solution for this problem, as it shouldn't be necessary to have every package author replicate this.

I tried to achieve this somehow with grunt-shell hackery, but as far as I know it's not possible to devDependencies more than one level deep, which makes it impossible even more ugly to execute the required grunt tasks.

So what's your take on this, or should I just move this over to the 0.5.0 discussion of grunt?

Problem courtesy of: Stephan Bönnemann

Solution

What you ask assumes that the package has:

  1. A dependency on Grunt to build a distribution; most popular libraries have this, but some of the less common ones may still use shell scripts or the npm run command for general minification/compression.

  2. Some way of generating a custom build in the first place with a dedicated tool like Modernizr or Lo-Dash has.

You could perhaps substitute number 2 with a generic one that parses both your source code and the library code and uses code coverage to eliminate unnecessary functions from the library. This is already being developed (see goldmine), however I can't make any claims about how good that is because I haven't used it.

Also, I'm not sure how that would work in a AMD context where there are a lot of interconnected dependencies; ideally you'd be able to run the r.js optimiser and get an almond build for production, and then filter that for unnecessary functions (most likely Istanbul, would then have to make sure that the filtered script passed all your unit/integration tests). Not sure how that would end up looking but it'd be pretty cool if that could happen. :-)

However, there is a task especially for running Grunt tasks from 'sub-gruntfiles' that you might like to have a look at: grunt-subgrunt.

Solution courtesy of: Ben

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.