grunt.registerMultiTask() 从字符串中去除变量

grunt.registerMultiTask() strips out variable from a string

我正在编写一个必须处理任务中定义的模板的 grunt 插件。我遇到的问题是,甚至在 grunt.template.process() 之前,模板中的变量就被剥离了。

任务的选项如下所示:

wrap: {
    html: {
        footer: '<script>require(["<%= filename %>"])</script>',
        src: ['templates/*.*'],
        dest: 'new_templates'
    }
}

简化形式的任务本身如下所示:

grunt.registerMultiTask('wrap', 'Blah-blah', function() {
    grunt.log.writeln(this.data.footer); // outputs "<script>require([""]); </script>"
});

Gruntfile in simplified form

Grunt 在其配置 调用任务之前使用 <% %> 分隔符自动扩展模板,请参阅 http://gruntjs.com/configuring-tasks#templates

因此,如果您想按原样获取模板字符串,一种解决方案是在您的配置字符串中使用自定义分隔符(例如 [%{%),然后在您的 grunt.template.process 调用(参见 http://gruntjs.com/api/grunt.template#grunt.template.process and http://gruntjs.com/api/grunt.template#grunt.template.adddelimiters