Skip to content

loadTasks as they are needed to speed up Grunt load time #975

@dylang

Description

@dylang

The problem is that Grunt loads all tasks every time it runs. Even if the user just wants to run grunt jshint, it will still load grunt-uglify, grunt-sass, etc.

This can be slow if a task has a lot of dependencies that are require'ed before the task is run.

For example, this is a slow to load task:

module.exports = function(grunt) {

    // These `require` statements execute even if this task isn't run.
    var path = require('path'),
        semver = require('semver'),
        q = require('q'),
        utils = require('./utils/utils'),
        constants = require('./utils/constants'),
        local = require('./core/local')(grunt),
        styles = require('./core/styles')(grunt),
        debug = require('./utils/debug')(grunt),
        install = require('./core/install')(grunt);

    grunt.registerTask(
        'build',
        'Build project',
        function(){
           // Code here runs when the task runs.
           // Grunt would load faster if devs put their `require`
           // statements here because they would only run
           // when the task is run but it's much more common
           // in the Node world to put all `require`s at the top.
           // (code removed for this example)
        }
    );

I've made some changes to time-grunt so you can see how long it takes for tasks to load vs their actual run time.
screenshot
In that screenshot there are other tasks loading that aren't used but they still contribute to the load time.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions