简体   繁体   English

Grunt.js:我如何自己规范化文件,Grunt的方式呢?

[英]Grunt.js: how do I normalize files myself, the way that Grunt does?

When you create a new task with grunt, ie: 使用grunt创建新任务时,即:

grunt.registerMultiTask('name', 'description', function() {...});

A files object is made available inside the task function as this.files . 文件对象在任务函数内可用作this.files This is the normalized result of looking at the value passed to task.files in the grunt config and parsing it, expanding any globbing. 这是查看传递给grunt配置中的task.files并解析它的值的规范化结果,扩展任何globbing。

Issue 1: 问题1:

The first problem is, I have a task which needs to recurse potentially several times. 第一个问题是,我有一个任务需要多次递归。 Each time, it wants to use the files object to get an uptodate list of all files etc matching the initial configuration parameters. 每次,它都希望使用files对象来获取与初始配置参数匹配的所有文件的uptodate列表。 Eg: 例如:

grunt.registerMultiTask('sometask', 'description', function sometask() {
    var files = this.files;
    //do some stuff with files, then run this func again if needbe:
    if(somecondition) sometask.call(this);
});

The problem here is that the files object is not updated to reflect any changes that I have made to the file structure, so the next time the function is called, the files list is potentially out of date. 这里的问题是文件对象没有更新以反映我对文件结构所做的任何更改,因此下次调用该函数时,文件列表可能已过期。

What I'd like to be able to do is something like: 我希望能做的是:

grunt.registerMultiTask('sometask', 'description', function sometask(renormal) {
    //update this.files if we need to:
    if(renormal) {
        this.files = renormalize(this.data.files);
    }
    var files = this.files;
    //do some stuff with files, then run this func again if needbe:
    if(somecondition_is_matched) sometask.call(this, renormal);
});

Issue 2: 问题2:

Further, perhaps I want to make a plugin that takes two lots of file mappings, so in the grunt config I might have something like: 此外,也许我想制作一个需要两个文件映射的插件,所以在grunt配置中我可能有类似的东西:

grunt.initConfig({
    ...
    someplugin: {
        filesOne: [{
            cwd: "hello"
            src: ["something/**"],
            dest: "another"
        }],
        filesTwo: {
            "another2": ["soemthing2/*", "!something2/*.js"]
        }
    }
    ...
});

And in the plugin, I'd want to be able to normalize whatever filesOne and filesTwo is, just like grunt normalizes 'files', so that the user can input src-dest file mappings in all of the usual formats. 在插件中,我希望能够规范化任何filesOne和文件filesTwo ,就像grunt规范化'文件'一样,这样用户就可以输入所有常用格式的src-dest文件映射。

So.. 所以..

The grunt API seems to expose functions for expanding patterns etc, but does not seem to provide anything to achieve the normalisation it does under the hood with the files object. grunt API似乎公开了扩展模式等功能,但似乎没有提供任何东西来实现它在文件对象下的标准化。

Is there something I'm missing/some accepted way to do this? 有什么我缺少/一些可接受的方式来做到这一点?

There is actually a method grunt.task.normalizeMultiTaskFiles : 实际上有一个方法grunt.task.normalizeMultiTaskFiles

grunt.registerMultiTask('sometask', 'description', function() {
    var filesOne = grunt.task.normalizeMultiTaskFiles(
        {files: this.data.filesOne}, this.target);

    var filesTwo = grunt.task.normalizeMultiTaskFiles(
        {files: this.data.filesTwo}, this.target);
})

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

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