1

Is it possible to get a list of already registered tasks inside of my Gruntfile.js, or alternatively check if a single taskname is registered.

What I'm trying to do is: Split the grunt module configurations into single files, to be able to add and remove them easily and finally only load the modules and their configuration if they're installed (based on package.json devDependencies with the help from 'matchdep'). This is working fine so far. Additionally I need to have the aliasTask's defined in a JSON (to be able to easily add new tasks programmatically).

The problem is, that the list may contain unregistered tasks and since grunt throws an error if that task is not registered, I have to filter them out from the list, before calling registerTask. Therefor I'm looking for a way to test the current taskName if an appropriate task is registered.

Here's a gist showing the idea https://gist.github.com/MarcelloDiSimone/5631466

In that gist I'm currently checking against the keys of the configuration files, but this is not reliable, since some tasks may not have a configuration (like livereload) and thus they would be falsely filtered out.

4

1 回答 1

0

你的第一个问题:是的,这是可能的。运行grunt --help将为您提供所有可用grunt *任务的列表;无论是 npm 任务还是您自己的别名。

至于模块化你的 Gruntfile,我几天前看过这个。将它与 matchdep 之类的东西一起使用也是完全可能的。

module.exports = function(grunt) {

    "use strict";

    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    var conf = {
        pkg: grunt.file.readJSON('package.json')
    };

    grunt.file.recurse('./.grunt/config', function(abspath, rootdir, subdir, filename) {
        conf[filename.replace(/\.js$/, '')] = require('./' + abspath);
    });

    grunt.initConfig(conf);

    grunt.loadTasks('./.grunt/tasks');
};

然后,您的所有配置文件.grunt/config都以.grunt/tasks. 示例任务:

// .grunt/tasks/default.js
module.exports = function(grunt) {
    grunt.registerTask('default', 'Build a complete, concatenated and minified distribution.', [
        'sass',
        'cssc',
        'cssmin',
        'jshint',
        'uglify'
    ]);
}

cssmin 的示例配置:

// .grunt/config/cssmin.js
exports.minify = {
    src: 'assets/css/master.css',
    dest: 'assets/css/master.css'
};

认为这是处理此问题的好方法,因此我将其发布在这里以供将来参考。:-)

于 2013-09-08T11:44:20.913 回答