I am working on grunt for making building tool, till now I was putting src and dest addresses directly in my grunt.js file but in this case if I want to change my dest I have to change in every task of file which is not a good practice.for example if my grunt.js file have following task:
concat: {
js: {
src: 'src/js/*.js',
dest: 'dest/js/concat.js'
},
css: {
src: 'src/css/*.css',
dest: 'dest/css/concat.css'
}
},
min: {
js: {
src: 'dest/js/concat.js',
dest: 'dest/js/concat.min.js'
}
},
here if I change my address than i have to change in every place!!!
I want a JSON file in which I can declare src and dest and call in my grunt.js file. How can we do that???
You could add a property to package.json to declare metadata, for example
{
"name": "test",
"meta":{
"src":"someSrcFolder",
"dest":"someDestFolder"
},
....
}
Then in your Gruntfile.js, you could read those properties like this
grunt.initConfig({
pkg: '<json:package.json>',
concat: {
js: {
src: '<%= pkg.meta.src %>/js/*.js',
dest: '<%= pkg.meta.dest %>/js/concat.js'
},
css: {
src: '<%= pkg.meta.src %>/css/*.css',
dest: '<%= pkg.meta.dest %>/css/concat.css'
}
}
});
The important bits are pkg:<json:package.json> which loads the json file into memory and the underscore template interpolations <%= pkg.meta.src %> which evaluate to the contents of the meta property added to the json file.
Keep in mind that the Gruntfile is just a JavaScript file so you could also have an object with references to paths and then interpolate them into your tasks.
This changed in Grunt.js 0.4.0.
You would now use: pkg: grunt.file.readJSON('package.json')