gruntjs / grunt

Grunt: The JavaScript Task Runner
http://gruntjs.com/
Other
12.27k stars 1.5k forks source link

Increase cohesion in the Gruntfile #1107

Open andrewshawcare opened 10 years ago

andrewshawcare commented 10 years ago

I was working with my Gruntfile and realized that the grunt.initConfig and grunt.loadNpmTasks are decoupled which creates issues:

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.

/* 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)
                }
            }
        ]
    });
};
robcolburn commented 10 years ago

I think you want: https://www.npmjs.org/package/load-grunt-tasks https://www.npmjs.org/package/load-grunt-config

Article explaining both: http://www.html5rocks.com/en/tutorials/tooling/supercharging-your-gruntfile/

tandrewnichols commented 10 years ago

Or task-master