简体   繁体   English

我可以在我的Gruntfile.js中获取所有已注册模块的列表吗

[英]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. 是否可以在我的Gruntfile.js中获取已注册任务的列表,或者检查是否已注册单个任务名。

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'). 我想做的是:将grunt模块配置拆分为单个文件,以便能够轻松添加和删除它们,最后仅在安装了模块及其配置后加载(基于package.json devDependencies和来自“ 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). 另外,我需要在JSON中定义aliasTask,以便能够以编程方式轻松添加新任务。

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. 问题在于,列表可能包含未注册的任务,并且如果未注册该任务,则grunt会引发错误,因此我必须在调用registerTask之前将其从列表中过滤掉。 Therefor I'm looking for a way to test the current taskName if an appropriate task is registered. 因此,我正在寻找一种方法,如果注册了适当的任务,则可以测试当前taskName。

Here's a gist showing the idea https://gist.github.com/MarcelloDiSimone/5631466 这是一个展示想法的要点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. 在这个要点中,我目前正在检查配置文件的密钥,但这并不可靠,因为某些任务可能没有配置(例如livereload),因此它们会被错误地过滤掉。

Your first question: Yes, it is possible. 您的第一个问题:是的,有可能。 Running grunt --help will give you a list of all available grunt * tasks; 运行grunt --help将为您提供所有可用的grunt *任务列表; whether npm tasks or your own aliases. 无论是npm任务还是您自己的别名。

As for modularising your Gruntfile, I had a look into this a few days ago . 关于模块化您的Gruntfile,几天前我对此进行了调查 Using this with something like matchdep is totally possible too. 也可以将它与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');
};

All your configuration files then end up in .grunt/config , and all of your tasks end up in .grunt/tasks . 然后,所有配置文件都以.grunt/config结尾,所有任务都以.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: 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. 认为这是处理此问题的一种好方法,因此我将其发布在这里以供将来参考。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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