Grunt模板中的插值数组被解释为一个字符串 [英] Interpolated array in Grunt template is interpreted as a string

查看:126
本文介绍了Grunt模板中的插值数组被解释为一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一个标题:为什么Grunt的concat任务不使用动态配置值?

我试图动态配置由Grunt连接的文件,这样做我遇到了这个问题,其中 grunt-contrib-concat 插件似乎没有选择动态设置的值。起初我以为我做错了什么,但是在创建我自己的任务并使用相同的动态值后,所有内容都按照预期发布。所以这留下了为什么grunt concat任务没有完成并使用相同的值?

重现行为的grunt文件在下面看到(gist:fatso83

  //显示动态配置(和选项!)值
//的Grunt文件是不在grunt-contrib-concat任务中使用。使用'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标志,该选项可以帮助
调试文件路径问题。


在config(编辑上面,以反映这一点)我得到了这个错误消息,至少显示了一些动态值的东西:

 运行concat:foo(concat)任务
>>找不到源文件file1.txt,file2.txt。
警告:无法写入outfile.txt / file1.txt,file2.txt文件
(错误代码:ENOTDIR)。使用--force继续。

在其他任务中进行了一些调试之后, myTask ,我发现以 this.data 发送给任务的数据是字符串值,而不是数组。考虑到我们做了字符串插值,这可能不是很令人惊讶,但这与其他插值特征不一致。例如,将<%= otherTask.fooTarget.src%> 作为数组获取其他任务的 src 属性值。

现在问题是如何避免将插值值作为数组而不是字符串传递给concat任务? 阅读完 Grunt源代码

发现问题是我们的数组被解释为一个字符串,我很快找到了一个相关问题看起来很有希望。只需将大括号内插的数组字符串括起来,Grunt就能找到这些文件!



不幸的是,我们正在有效创建的globbing模式不会保留指定的文件顺序。在上面的相关问题中,我发布了有关正在发生的事情的详细解释,以及在常规情况下如何解决此问题。

对于我的具体情况,如果我在配置对象中引用一个字段,实际上不需要函数调用来检索它,因为它可以直接在模板中使用自己的范围!因此,我可以不用调用 grunt.config.get('myfiles'),而只需执行<%= myfiles%>

以上例子:

  grunt .initConfig({

concat:{
foo:{
nonull:true,
src:'<%= myfiles%>',
dest:'outfile.txt'
}
},

myTask:{
bar:'<%= myfiles%>'
}
});


Previous title: "Why is Grunt's concat task not using dynamic configuration values?"

I am trying to dynamically configure the files that are concatenated by Grunt, and in doing so I came across this issue where the grunt-contrib-concat plugin does not seem to pick up the dynamically set values. At first I thought I was doing something wrong, but after creating my own task and using the same dynamic values everything came out just as intended. So that leaves the question of why is the grunt concat task not doing picking up and using the same values?

A gruntfile that reproduces the behaviour is seen below (gist: 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'])
}

EDIT: A new lead: After literally hours of going nowhere I came across this sentence on Grunt's homepage:

nonull If set to true then the operation will include non-matching patterns. Combined with grunt's --verbose flag, this option can help debug file path issues.

Adding that to the config (edited above to reflect this) I got this error message which at least show that something is doing something with the dynamic values:

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.

After some more debugging in the other task, myTask, I have found out that the data sent in to the task as this.data is a string value, not an array. This is perhaps not very surprising, given that we do string interpolation, but this is not consistent with other interpolation features. For instance will <%= otherTask.fooTarget.src %> get the other task's src property as an array value.

Now the question is really how can I avoid passing the interpolated value as an array, rather than a string, to the concat task?

解决方案

Updated after reading up on the Grunt source code

After I found out that the problem was our array was interpreted as a string I quickly found a related question with a solution that seemed promising. Simply by enclosing the interpolated array string with curly brackets Grunt was able to find the files!

Unfortunately, the globbing pattern we are effectively creating does not preserve the specified file order. In the related question above I posted a thorough explanation of what was going on and how you can work around it in the general case.

For my specific case, where I reference a field in the configuration object there is actually no need for a function call to retreive it as it is directly available in the templates own scope! Therefore, instead of calling grunt.config.get('myfiles'), I can simply do <%= myfiles %>.

For the example above:

grunt.initConfig({

    concat : {
        foo : {
            nonull : true,
            src: '<%= myfiles %>',
            dest : 'outfile.txt'
        }
    },

    myTask : {
        bar : '<%= myfiles %>'
    }
});

这篇关于Grunt模板中的插值数组被解释为一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆