Grunt - 解析非字符串(例如数组)模板

Roy*_*ove 9 javascript gruntjs

假设我的grunt配置中有一个变量,数组作为值.一个真实世界的例子grunt.regarde.changed来自grunt-regarde插件,它列出了所有已更改的文件.

我想使用模板解析该数组,以便我可以(在这种情况下)复制更改的文件:

  copy: {
    staticWeb: {
      src: '<%= grunt.regarde.changed %>',
      dest: 'someDir'
    },
Run Code Online (Sandbox Code Playgroud)

什么src得到在这种情况下是一个单一的逗号分隔的字符串,而不是一个数组.Grunt的文件处理器不解析字符串,因此无法找到src文件.

我无法删除模板周围的单引号,因为它是无效的javascript.

那么如何将该grunt.regarde.changed数组传递给src变量呢?

oli*_*ren 10

一旦你知道如何,只需几行代码就可以轻松解决这个问题,但是我花了很长时间才从Grunt源代码中挖掘出所有相关信息以便了解该怎么做,所以请耐心等待我带你了解背景......


配置对象上获取属性的一般方法是直截了当的:

<%= some.property %> // fetches grunt.config.get('some.property')
Run Code Online (Sandbox Code Playgroud)

这适用于已在grunt.config对象上设置的所有属性,当然(包括)传入的配置grunt.initConfig().这就是您可以直接引用其他任务变量的原因,例如in <%= concat.typescriptfiles.dest %>,因为config对象中的所有属性都在模板自己的范围内.

从技术上讲,当(LoDash)模板与options对象(如果已定义)或grunt.config对象传递给模板处理器(LoDash' template函数)时,会发生此扩展.

因此,这适用于已在配置本身中设置的值,或通过使用动态分配的值grunt.config.set().有关详细信息,请参阅API文档.

不能以相同方式工作的是访问配置对象上不可用的值.似乎由于某种原因我不太确定,所有其他值总是以字符串结尾.无论您是直接访问还是通过方法调用,都会发生这种情况.例如,尝试通过配置访问数组grunt.config.get()获取一个字符串.

保留文件顺序的问题的解决方法

接受的答案在某种程度上起作用,但由于全局语法,它将由glob()模块解析,而不保留文件顺序.这对我的构建来说是禁忌.

如果要使用的阵列在配置对象上不可用,则解决方法是通过中间任务将其添加到配置中.像下面这样的东西应该工作:

// 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 object
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 %>' // 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']);
}
Run Code Online (Sandbox Code Playgroud)

这将输出

$ grunt
Running "myValSetter" task

Running "debug:using_attribute" (debug) task
data.src:  c/file1.txt,a/file2.txt,b/file3.txt
type:  string

Running "debug:using_task" (debug) task
data.src:  [ 'c/file1.txt', 'a/file2.txt', 'b/file3.txt' ]
type:  Array

Running "debug:no_task_direct_setter" (debug) task
data.src:  [ 'c/file1.txt', 'a/file2.txt', 'b/file3.txt' ]
type:  Array

Done, without errors.
Run Code Online (Sandbox Code Playgroud)

这个例子只是为了说明这个概念,但你应该能够轻松地将它自定义为你的实例:)


小智 8

我和你的问题完全相同,用大括号包围你的模板来解决它.

所以这是你的代码,修改

copy: {
  staticWeb: {
    src: '{<%= grunt.regarde.changed %>}', // added curly brackets
    dest: 'someDir'
  }
}
Run Code Online (Sandbox Code Playgroud)

它将输出srcas {source1,source2,source3},这与使用数组相同.(参见Grunt文档中的Globbing Patterns)