上一标题:“为什么 Grunt 的 concat 任务不使用动态配置值?”
我正在尝试动态配置由 Grunt 连接的文件,这样做时我遇到了grunt-contrib-concat
插件似乎没有获取动态设置值的问题。起初我以为我做错了什么,但在创建自己的任务并使用相同的动态值之后,一切都按预期进行。那么问题来了,为什么 grunt concat 任务没有拾取和使用相同的值?
重现该行为的 gruntfile 如下所示(要点:fatso83/73875acd1fa3662ef360)。
// Grunt file that shows how dynamic config (and option!) values
// are not used in the grunt-contrib-concat task. Run using 'grunt'
module.exports = function(grunt){
grunt.initConfig({
concat : {
foo : {
nonull : true,
src: '<%= grunt.config.get("myfiles") %>',
dest : 'outfile.txt'
}
},
myTask : {
bar : '<%= grunt.config.get("myfiles") %>'
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerMultiTask('myTask', function() {
grunt.log.writeln('myTask:' + this.target + ' data=' + this.data);
});
grunt.registerTask('default', ['myTask','concat']);
grunt.config.set('myfiles',['file1.txt', 'file2.txt'])
}
编辑:一个新的线索: 经过几个小时的无处可去后,我在 Grunt 的主页上看到了这句话:
nonull如果设置为 true,则操作将包括不匹配的模式。结合 grunt 的 --verbose 标志,这个选项可以帮助调试文件路径问题。
将其添加到配置中(上面已编辑以反映这一点)我收到此错误消息,它至少表明某些东西正在使用动态值进行某些操作:
Running "concat:foo" (concat) task
>> Source file "file1.txt,file2.txt" not found.
Warning: Unable to write "outfile.txt/file1.txt,file2.txt" file
(Error code: ENOTDIR). Use --force to continue.
在其他任务中进行了更多调试后myTask
,我发现发送到任务的数据this.data
是字符串值,而不是数组。鉴于我们进行字符串插值,这可能并不令人惊讶,但这与其他插值功能不一致。例如,将<%= otherTask.fooTarget.src %>
其他任务的src
属性作为数组值获取。
现在的问题实际上是如何避免将插值作为数组而不是字符串传递给 concat 任务?