本文介绍了Grunt - 解析非字符串(例如数组)模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设我有一个数组作为值的grunt配置中有一个变量。真实世界的例子是 grunt.regarde.changed grunt-regarde 插件,它列出了所有已更改的文件。 我想使用模板来解析数组,以便我可以(在这种情况下)复制已更改的文件: 复制:{ staticWeb:{ src:'<% = grunt.regarde.changed%>', dest:'someDir'}, 在这种情况下,获取 src 是一个逗号分隔的字符串,而不是数组。 Grunt的文件处理器不解析字符串,因此无法找到src文件。 我无法删除模板中的单引号,因为它无效的javascript。 那么如何将 grunt.regarde.changed 数组传递给 src 变量?解决方案这个问题很容易解决,一旦你知道如何,只需几行代码,但是我花了相当长的一段时间才从Grunt源代码中挖掘所有相关信息,以便理解要做什么,在我带领你穿过背景的时候,对我毫无用处...... 获取配置对象上的属性的一般方法非常简单: <%= some.property%> //获取grunt.config.get('some.property') 适用于所有属性已经设置在 grunt.config 对象上,当然这个对象包含了传入 grunt.initConfig()。这就是为什么你可以直接引用其他任务变量,比如在<%= concat.typescriptfiles.dest%> 中,因为配置对象中的所有属性都在(LoDash)模板与选项一起传递 对象(如果已定义)或 grunt.config 对象添加到模板处理器(LoDash' template 函数)因此,这适用于已在配置本身中设置的值,或者通过使用 grunt.config.set()。有关详细信息,请参阅 API文档。 不以同样的方式工作的是访问配置对象上不可用的值。似乎由于某种原因,我不太确定,所有其他值总是以字符串结尾。无论您是直接访问还是通过方法调用,都会发生这种情况。例如,试图通过 grunt.config.get()访问配置数组会得到一个字符串。 解决保存文件顺序问题的解决方法 接受的答案在某种程度上起作用,但由于globbing语法,它将由 glob() module 不保存文件顺序。这是一个不适用于我的构建。 解决方法是,如果您想使用的数组在配置对象上不可用,则将其添加到通过中介任务进行配置。类似以下内容应该可以工作: //此变量将用于两次演示 //之间的差异直接在grunt对象上设置一个属性 //并在grunt.config对象上使用setter方法 var myFiles = ['c / file1.txt','a / file2.txt',' b / file3.txt'] module.exports = function(grunt){ grunt.initConfig({ debug:{ using_attribute: { src:'<%= grunt.value_as_attribute%>'//将是字符串}, using_task:{ src:'<%= value_by_setter %>'//将是数组}, no_task_direct_setter:{ src:'<%= value_by_setter_early%>'//将是数组} } }); grunt.registerTask('myValSetter',function(){ grunt.config.set('value_by_setter',myFiles); }); //一个将报告我们设定值信息的任务 grunt.registerMultiTask('debug',function(){ grunt.log.writeln('data.src :',this.data.src); grunt.log.writeln('type:',Array.isArray(this.data.src)?Array:typeof this.data.src); }); grunt.value_as_attribute = myFiles; grunt.config.set('value_by_setter_early',myFiles); grunt.registerTask('default',['myValSetter','debug']); } 这会输出 运行myValSetter任务 运行debug:using_attribute(调试)任务 data.src:c / file1.txt,a / file2.txt,b / file3.txt 类型:字符串 运行debug:using_task(调试)任务 data.src:[' c / file1.txt','a / file2.txt','b / file3.txt'] 类型:数组 运行debug:no_task_direct_setter(调试)任务 data.src:['c / file1.txt','a / file2.txt','b / file3.txt'] 类型:数组 $ b $完成,没有错误。 这个例子只是为了说明这个概念,但您应该可以轻松地将其定制到您的实例:) Say I have a variable in my grunt config with an array as a value. A real world example is grunt.regarde.changed from the grunt-regarde plugin, which lists all files that have changed.I want to resolve that array using a template, so that I could (in this case) copy the changed files: copy: { staticWeb: { src: '<%= grunt.regarde.changed %>', dest: 'someDir' },What src gets in this case is a is a single comma delimited string instead of an array. Grunt's file processor does not parse the string, and so it cannot find the src file.I can't remove the single quotes around the template because then it's invalid javascript.So how do I pass that grunt.regarde.changed array to the src variable? 解决方案 The problem is very easy to fix once you know how, just a few lines of code, but it took me quite a while to dig up all the relevant info from the Grunt source code in order to understand what to do, so bare with me while I take you through the background ...The general way of getting hold of a property on the configuration object is straight forward:<%= some.property %> // fetches grunt.config.get('some.property')That works for all properties that have been set on the grunt.config object, which (of course) include the config that is passed into grunt.initConfig(). This is why you can reference other tasks variables directly, such as in <%= concat.typescriptfiles.dest %>, as all the properties in the config object is in the template's own scope.Technically this expansion happens when the (LoDash) template is passed along with either the options object (if defined) or the grunt.config object to the template processor (LoDash' template function).So this works for values that have been set in the config itself, or by using dynamically assigned values through grunt.config.set(). See the API docs for more info.What does not work in the same way is accessing values that are not availabe on the configuration object. It seems that for some reason I am not quite sure of, all other values always ends up as strings. This happens regardless of whether you access them directly or through method calls. For instance trying to get access to an array on the config through grunt.config.get() gets you a string.A workaround for the problem that preserves file orderThe accepted answer works in a way, but due to the globbing syntax it will be parsed by the glob() module which does not preserve file order. This was a no-no for my build.A workaround, in case the array you want to use is not available on the config object, is to add it to the config via an intermediary task. Something like the following should work:// This variable will be used twice to demonstrate the difference// between directly setting an attribute on the grunt object// and using the setter method on the grunt.config objectvar myFiles = ['c/file1.txt', 'a/file2.txt', 'b/file3.txt']module.exports = function(grunt){ grunt.initConfig({ debug : { using_attribute: { src : '<%= grunt.value_as_attribute %>' // will be a string }, using_task: { src : '<%= value_by_setter %>' // will be an array }, no_task_direct_setter: { src : '<%= value_by_setter_early %>' // will be an array } } }); grunt.registerTask('myValSetter', function() { grunt.config.set('value_by_setter', myFiles ); }); // a task that will report information on our set values grunt.registerMultiTask('debug', function(){ grunt.log.writeln('data.src: ', this.data.src); grunt.log.writeln('type: ', Array.isArray(this.data.src)? "Array" : typeof this.data.src); }); grunt.value_as_attribute = myFiles; grunt.config.set('value_by_setter_early', myFiles ); grunt.registerTask('default',['myValSetter', 'debug']);}This will output$ gruntRunning "myValSetter" taskRunning "debug:using_attribute" (debug) taskdata.src: c/file1.txt,a/file2.txt,b/file3.txttype: stringRunning "debug:using_task" (debug) taskdata.src: [ 'c/file1.txt', 'a/file2.txt', 'b/file3.txt' ]type: ArrayRunning "debug:no_task_direct_setter" (debug) taskdata.src: [ 'c/file1.txt', 'a/file2.txt', 'b/file3.txt' ]type: ArrayDone, without errors.This example is just meant to illustrate the concept, but you should be able to easily customize it to your instance :) 这篇关于Grunt - 解析非字符串(例如数组)模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 04:40