当你用 grunt 创建一个新任务时,即:
grunt.registerMultiTask('name', 'description', function() {...});
文件对象在任务函数中作为 this.files
可用。这是在 grunt 配置中查看传递给 task.files
的值并对其进行解析、扩展任何 globbing 的规范化结果。
问题 1:
第一个问题是,我有一个任务可能需要多次递归。每次,它都想使用文件对象来获取与初始配置参数匹配的所有文件等的最新列表。例如:
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);
});
这里的问题是文件对象没有更新以反射(reflect)我对文件结构所做的任何更改,因此下次调用该函数时,文件列表可能已过时。
我希望能够做的是:
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);
});
问题 2:
此外,也许我想制作一个需要两个文件映射的插件,所以在 grunt 配置中我可能有类似的东西:
grunt.initConfig({
...
someplugin: {
filesOne: [{
cwd: "hello"
src: ["something/**"],
dest: "another"
}],
filesTwo: {
"another2": ["soemthing2/*", "!something2/*.js"]
}
}
...
});
并且在插件中,我希望能够规范化任何 filesOne
和 filesTwo
是,就像 grunt 规范化"file"一样,以便用户可以以所有常用格式输入 src-dest 文件映射。
所以..
grunt API 似乎公开了用于扩展模式等的函数,但似乎没有提供任何东西来实现它在幕后对文件对象所做的规范化。
我是否遗漏了什么/一些公认的方法可以做到这一点?
最佳答案
其实有一个方法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);
})
关于javascript - Grunt.js : how do I normalize files myself, Grunt 的工作方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20120400/