Grunt动态变量问题(未定义)

时间:2015-10-06 12:26:04

标签: javascript node.js gruntjs

我的grunt文件中有一个范围问题,我需要一个计数器变量,每次新的时间grunt.task.run完成,但每次我只能使用asp_files [0]我的愿望是遍历存储在数组中的每个asp / html文件,然后在该文件上执行我的各种grunt任务(已经有这个工作的样板代码,我只是无法让我的索引变量改变)

继承人我如何称呼我的grunt模块

for (var x = 0; x < asp_files.length; x++){

    grunt.task.run ( [
        //'clean:build',
        'copy:build',
        'replace:gather',
        'uglify:js',
    ]);

}

继承我的副本模块

copy: {
       build: {
            files: [
                {
                    dest: 'build/copied',
                    expand: true,
                    src:  'html/'+asp_files['<%= counterVar %>'] 
                }
            ]
        }
    },

这里的计数器应该增加

replace: {
            gather: {
                files: [
                    {
                        dest: 'build',
                        expand: true,
                        // This doesn't work, however asp_files[0] works fine, as does asp_files[globalCount] however it never actually increments past 0..
                        src:  'html/'+asp_files['<%= counterVar %>'] 
                    }
                ],
                options: {
                    patterns: [
                        {
                           //Grab the <!--build-js-start--> and <!--build-js-end--> comments and everything in-between
                            match: /\<\!\-\-build\-js\-start[\s\S]*build\-js\-end\-\-\>/,

                            replacement: function ( matchedString ) {

                                js_filename = file_names[globalCount];

                                grunt.config( 'counter', globalCount );
                                globalCount++;
                                var count = grunt.config('counter');

                                // This prints out the count correctly...
                                console.log("The config counter value is: "+count);

我已经尝试了许多不同的方法来尝试获取计数器或任何类型的变量来改变每个构建但是无济于事

我的counterVar做错了什么我永远不会改变我的替换:收集模块? (如果需要,可以使用完整的工作资源:http://pastebin.com/F1cdSeB4

由于

1 个答案:

答案 0 :(得分:0)

Grunt的方法是从另一个角度来看待问题:列出所有文件,并要求每个任务都能在所有文件上运行(而不是在1个文件上运行所有任务):

var aspFiles = ['html/file1.asp', 'html/file2.asp'];
//...
grunt.initConfig({
  copy: {
    build: {
      files: [{
        dest: 'build/copied',
        expand: true,
        src: aspFiles
      }]
    }
  },
  replace: {
    gather: {
      files: [{
        dest: 'build',
        expand: true,
        src: aspFiles 
      }]
    }
  }
});
相关问题