Dritzz
Dritzz

Reputation: 169

Loading Grunt task attributes from external file

I am quite new to grunt, literally only a few hours. I am setting up grunt to cache-bust my website using grunt-cache-breaker. Now I have got it to work when I manually type in every file source example:

grunt.initConfig({

    cachebreaker: {
        dev: {
            options: {
                match: ['.js', '.css', 'min.css'],
                src: {
                    path: 'TEST/Apps/**/*'
                }
            },
            files: {
                src: ['TEST/Apps/AppTemplate/v1.0.0/index.html',
                      'TEST/Apps/Case_Attributes/v1.0.0/index.html',
                      'TEST/Apps/Case_CorrespondenceReferences/v1.0.0/index.html',
                ',    
                ]
            }
        },
    },

});

however what I really want to do is to be able to problematically build the list of files.src from a pre-generated text file like this:

grunt.initConfig({

    cachebreaker: {
        dev: {
            options: {
                match: ['.js', '.css', 'min.css'],
                src: {
                    path: 'TEST/Apps/**/*'
                }
            },
            files: {
                src: function (){
                    return grunt.file.read('config.txt')
                }

            }
        },
    },

});

or something to this affect. Is this possible? Or am I completely off the mark?

Upvotes: 1

Views: 191

Answers (2)

Zé Meirinhos
Zé Meirinhos

Reputation: 41

A different and very versatile way to do that is to generate the task parameters in runtime. Say you load the list of filenames into array list. After having called grunt.initConfig() you can do this:

grunt.config.merge({
    cachebreaker: {
        dev: {
            files: { src: list }
        }
    }
}),

That is, update the config object with these other properties you want.

Upvotes: 0

Dritzz
Dritzz

Reputation: 169

After a few more hours of playing I came up with this solution:

module.exports = function(grunt) {

grunt.initConfig({

   config: grunt.file.readJSON('config.json'),  

   cachebreaker: {
        dev: {
            options: {
                match: ['.js', '.css', 'min.css'],
                src: {
                    path: 'TEST/Apps/**/*'
                }
            },
            files: {
                src: ['<%= config %>'], 
            }
        },
    },
});
grunt.loadNpmTasks('grunt-cache-breaker');
grunt.registerTask('default', ['cachebreaker']);

};

Upvotes: 1

Related Questions