title: Dynamic mapping for destinations in grunt.js
body: <p>I have a project with several sub folders that contain JavaScript files I want to concatenate. what would be the right way to configure them?</p>
  
  <p>eg.</p>
  
  <p>source: /modules/$modulename/js/*.js (several files)
  dest: /modules/$modulename/js/compiled.js</p>
  
  <p>So what I want to do is to compile js-files of an unknown/unconfigured count of subfolders ($modulename) into one file per subfolder. </p>
  
  <p>Is this possible? </p>
  
  <hr>
  
  <p>The following function (built after hereandnow78's instructions) does the job:</p>
  
  <pre><code>grunt.registerTask('preparemodulejs', 'iterates over all module directories and compiles modules js files', function() {
  
      // read all subdirectories from your modules folder
      grunt.file.expand('./modules/*').forEach(function(dir){
  
          // get the current concat config
          var concat = grunt.config.get('concat') || {};
  
          // set the config for this modulename-directory
          concat[dir] = {
              src: [dir + '/js/*.js', '!' + dir + '/js/compiled.js'],
              dest: dir + '/js/compiled.js'
          };
  
          // save the new concat config
          grunt.config.set('concat', concat);
  
      });
  
  });
  </code></pre>
  
  <p>after that i put preparemodulejs before the concat job in my default configuration.</p>