Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gruntjs load external config

Tags:

gruntjs

Ahoy grunt-masters!

I would like to load external config files into grunt so that I can do something like this:

$ grunt dev:homepage

and it would load in homepage-config.json, then run watch

$ grunt dev:contact

and it would load in contact-config.json, then run watch

Each config file would provide a particular setup for tasks: watch, jshint, concat, etc...

Inside my Gruntfile I have a task called dev

grunt.registerTask('dev', 'loads in external -config.json file, then runs watch', function(name) {

  grunt.initConfig(grunt.file.readJSON(name + '-config.json'));

  console.log(grunt.config('jshint.pageConfig.src') // correctly logs whatever had been specified in my external json file

  grunt.task.run('watch'); // correctly boots up watch with configuration specified by external file

});

Within that dev task the externally loaded config works just fine. That console.log would return what you'd expect, and the watch task kicks off with the externally specified setup.

My problem is that once that watch starts triggering tasks, those tasks no longer seem to have access to this externally loaded config. Somewhere between the dev task and the tasks triggered by watch, the dynamically loaded config gets blown away.

Can anyone shed light on why this is happening and how I might accomplish my goal?

Many thanks, -James

like image 399
James Avatar asked Sep 17 '25 23:09

James


1 Answers

You need to specify nospawn : true in your watch task configuration so that the called tasks run in the same context. See this section of the docs for more info/examples.

like image 142
go-oleg Avatar answered Sep 23 '25 12:09

go-oleg