Running concurrent tasks in Grunt to speed up build

Tip submitted by @deepu105

The Grunt build in earlier versions of JHipster used to ship with a concurrent task configured for compass, imagemin and svgmin, this was removed as it was not adding any performance improvement in the default JHipster configuration as the tasks configured in concurrent were not heavy enough to benefit from multiple grunt threads.

If you have lots of images, SVG files, a compass task with lots of sass files and Coffeescript tasks to run, you can consider using concurrent to spawn multiple threads to run those time consuming tasks in parallel.

Only projects with many assets will benefit from this, and for other projects, running tasks concurrently will in fact slow down your build: if you enable this feature, you should test it first to see its impact on your specific build.

To have tasks run by concurrent during grunt build, grunt test or grunt serve, first install the grunt-concurrent plugin by running npm grunt-concurrent --save-dev and then add the code below in the files generated by JHipster:

Gruntfile.js

Add the concurrent task definition to grunt.initConfig

concurrent: {
    server: [
        'compass:server' // this can be accompanied by cofee task etc.
    ],
    test: [
        'compass'
    ],
    dist: [
        'compass:dist',
        'imagemin',
        'svgmin'
    ]
},

Replace the tasks configured in concurrent definition in the serve, test, and build tasks so that they are included in the workflow

grunt.registerTask('serve', [
    'clean:server',
    'wiredep',
    'ngconstant:dev',
    'concurrent:server',
    'browserSync',
    'watch'
]);

grunt.registerTask('test', [
    'clean:server',
    'wiredep:test',
    'ngconstant:dev',
    'concurrent:test',
    'karma'
]);

grunt.registerTask('build', [
    'clean:dist',
    'wiredep:app',
    'ngconstant:prod',
    'useminPrepare',
    'ngtemplates',
    'concurrent:dist',
    'concat',
    'copy:dist',
    'ngAnnotate',
    'cssmin',
    'autoprefixer',
    'uglify',
    'rev',
    'usemin',
    'htmlmin'
]);