2013-01-16 112 views
3

Ahoy grunt-masters!gruntjs加载外部配置

我想外部配置文件加载到咕噜,这样我可以做这样的事情:

$ grunt dev:homepage

,它会在homepage-config.json加载,然后运行watch

$ grunt dev:contact

它会加载在contact-config.json,然后运行watch

每个配置文件将提供任务特定的设置:手表,jshint,CONCAT,等...

里面我Gruntfile我有一个名为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 

}); 

任务在这一dev任务外部加载配置工作得很好。该console.log将返回您所期望的,并且watch任务将使用外部指定的设置启动。

我的问题是,一旦watch开始触发任务,这些任务似乎不再有权访问此外部加载的配置。在dev任务和watch触发的任务之间的某个位置,动态加载的配置会被吹掉。

任何人都可以阐明为什么会发生这种情况以及我如何实现我的目标?

非常感谢, - 詹姆斯

回答

3

你需要在你的手表任务配置指定nospawn : true使被叫任务在同一个环境中运行。有关更多信息,请参阅文档this section