问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/14359473/gruntjs-load-external-config