Grunt. Programmatically merge corresponding files in parallel folders with concat

只愿长相守 提交于 2019-12-11 14:56:11

问题


that's my first post on StackOverflow!:D I mean... ever!

Anyway... I couldn't find any clear answer to this, and maybe I'm missing something really obvious. I'm really new to Grunt as well. I'm looking for a way to merge parallel css files in two different folders in few lines of code. So a better solution than doing this for each couple of files:

concat : {
  options : { separator : '\n' },
      css : {
        files:{

          '<%= pkg.dest %>/app/css/main_blue.css' :
            [ 'app/css/base/main_blue.css', 'app/css/extended/main_blue.css' ],

          '<%= pkg.dest %>/app/css/main_red.css' :
            [ 'app/css/base/main_red.css', 'app/css/extended/main_red.css' ],

          '<%= pkg.dest %>/app/css/home.css' :
           [ 'app/css/base/home.css', 'app/css/extended/home.css' ],
          ...
          '<%= pkg.dest %>/app/css/.../foo/bar/xx.css' :
            ['app/css/base/.../foo/bar/xx.css', 'app/css/extended/.../foo/bar/xx.css' ]
          ...
        }
    }
}

Any Grunt guru out there that can help? :)


回答1:


You could build the config object that you pass to grunt.initConfig dynamically.

var config = {}; // <- your grunt config
var files = ['blue', 'red', 'home'];
for (var i = 0; i < files.length; i++) {
  config['concat']['options']['css']['files']['<%= pkg.dest %>/app/css/'+files[i]+'.css'] = [ 'app/css/base/'+files[i]+'.css', 'app/css/extended/'+files[i]+'.css' ];
}
grunt.initConfig(config);


来源:https://stackoverflow.com/questions/23859514/grunt-programmatically-merge-corresponding-files-in-parallel-folders-with-conca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!