gruntjs / gruntjs/grunt

Increase cohesion in the Gruntfile

Open
#1,107 2 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

I was working with my `Gruntfile` and realized that the `grunt.initConfig` and `grunt.loadNpmTasks` are decoupled which creates issues:
- If I want to add or remove a task, I have to add code in two places in the `Gruntfile`.
- If I remove configuration data but forget to remove a task, this could result in undesirable consequences (running a task with default behaviour, which may imply destructive action if plugins are set with unsafe defaults)
- There are implicit id and names used for tasks in Grunt's `config` object and `loadNpmTasks` that would be better served if called out explicitly.

I've added a sample `Gruntfile` where I've created a `loadNpmTasks` to specifically couple the notion of adding a task with id, name, and configuration. If this seems a reasonable approach to the community, I'd suggest adopting a similar method in the `grunt` API to allow for cohesive task management.

``` javascript
/* global module */
module.exports = function (grunt) {
"use strict";

var loadNpmTasks = function (parameters) {
parameters.tasks.forEach(function (task) {
parameters.grunt.loadNpmTasks(task.name);
parameters.grunt.config.set(task.id, task.configuration);
});
};

var globs = {
javascript: ["*.js"],
json: [".jshintrc", "*.json"]
};

grunt.initConfig({
pkg: grunt.file.readJSON("package.json")
});

loadNpmTasks({
grunt: grunt,
tasks: [
{
id: "watch",
name: "grunt-contrib-watch",
configuration: {
options: {
livereload: true
},
javascript: {
files: [].concat(globs.javascript, globs.json),
tasks: ["jshint"]
}
}
},
{
id: "jshint",
name: "grunt-contrib-jshint",
configuration: {
options: {
jshintrc: true
},
src: [].concat(globs.javascript, globs.json)
}
}
]
});
};
```

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.