Friday, 27 September 2013

Gruntfile.js with "require" function and passing parameters

Gruntfile.js with "require" function and passing parameters

I have a Grunt file (Gruntfile.js) to build an angularjs component. I use
an external javascript file (build.config.js) to define the required files
and output directories.
module.exports = {
build_dir: 'build',
app_files: {
src: ['src/**/*.js','!src/**/*.spec.js'],
//...
},
vendor_files: {
js: [ /* vendor files like jquery, bootstrap, etc... */ ]
}
}
And my Gruntfile.js calling it like this
module.exports = function(grunt) {
/* some code */
var userConfig = require('./build.config.js');
var taskConfig = {
pkg: grunt.file.readJSON("package.json"),
/* more code */
},
/* yet more code */
}
In my package.json, I specify a locale variable for i18n purposes that I
can use inside the taskConfig section by putting it like '<%= pkg.locale
%>'. Now there are some javascript files specified in build.config.js that
I need to load depending on the locale specified, but I don't know how to
get this variable since in build.config.js there is no grunt object to
read the package.json file. Therefore, I can't do something like this in
build.config.js:
vendor_files: {
js: [ 'vendor/foo/bar.<%= pkg.locale %>.js' ]
Is there any way I can accomplish this without putting everything from
build.config.js back in Gruntfile.js?

No comments:

Post a Comment