gruntjs / gruntjs/grunt

loadTasks as they are needed to speed up Grunt load time

Open
#975 41 comments 0 reactions 0 assignees View on GitHub
Type: Enhancement
Dominant language
JavaScript
Stars
12.2k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

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:

``` js
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](https://github.com/sindresorhus/time-grunt) so you can see how long it takes for tasks to load vs their actual run time.
![screenshot](https://f.cloud.github.com/assets/51505/1505852/b1513488-48ef-11e3-8c27-5441b466405c.png)
In that screenshot there are other tasks _loading_ that aren't used but they still contribute to the load time.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.