简体   繁体   中英

Can I get a list of all registered modules in my Gruntfile.js

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.

Your first question: Yes, it is possible. Running grunt --help will give you a list of all available grunt * tasks; whether npm tasks or your own aliases.

As for modularising your Gruntfile, I had a look into this a few days ago . Using this with something like matchdep is totally possible too.

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');
};

All your configuration files then end up in .grunt/config , and all of your tasks end up in .grunt/tasks . Sample task:

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

Sample config for cssmin:

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

Thought this was a nice way of handling this problem so I'm posting it here for future reference. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM