Grunt: how to dynamically create file objects

I must be missing something very simple here. I am trying to write a function that deals with files. The Grunt API docs state that you can [Build the files object dynamic], but for some reason I cannot get this to work. A simplified version of my Gruntfile.js file is as follows:

module.exports = function(grunt) { grunt.initConfig({ proj: { build: { files: [{ expand: true, cwd: 'src', src: ['**/*.js'], dest: 'dist' }] } } }); grunt.registerTask('proj', function(){ var files = grunt.config('proj.build.files'); console.log(files); }); }; 

I expect the log to map the list of file associations from the src directory to the dist directory. What is actually logged is the proj.build.files object from config, for example:

 Running "proj:build" task [ { expand: true, cwd: 'src', src: [ '**/*.js' ], dest: 'dist' } ] Done, without errors. 

The API docs only talk about this type of configuration in terms of other tasks. I tried looking at the uglify task to see how file associations are retrieved, but I could not figure it out.

+2
gruntjs config
Mar 04 '15 at 5:50
source share
1 answer

Here is a workaround I found for dynamically creating file sets for Grunt tasks:

 uglify: { app: { files: [{ src: '{<%= _prefixSrc(pkg.target, pkg.resources.js) %>}', // Note the brackets! dest: '<%= pkg.target %>min/<%= pkg.name %>.min.js' }] } }, _prefixSrc: function(prefix, files) { return files.map(function(file){ return prefix + file; }); }, 

See also this question / feature request on GitHub and feel free to comment on it if you find it useful: https://github.com/gruntjs/grunt/issues/1307

+1
Mar 24 '15 at 22:47
source share



All Articles