gruntjs加载外部配置 [英] gruntjs load external config
问题描述
Ahoy grunt-master!
我想将外部配置文件加载到grunt中,以便我可以这样做:
$ grunt dev:homepage
它会载入 homepage-config.json
,然后运行 watch
$ grunt dev:contact
它会载入 contact-config.json
,然后运行 watch
每个配置文件都会为任务提供一个特定的设置:watch,jshint,concat,等等......
在我的Gruntfile中,我有一个名为 dev的任务
grunt.registerTask('dev','load 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')//正确记录无论在我的外部json文件中指定
grunt.task.r un('watch'); //用外部文件
})指定的配置正确启动watch;
在 dev
任务中,外部加载的配置工作得很好。那个console.log会返回你所期望的,并且 watch
任务开始于外部指定的设置。
我的问题是,一旦 watch
开始触发任务,那些任务似乎不再可以访问这个外部加载的配置。在 dev
任务和 watch
触发的任务之间的某处,动态加载的配置会被取消。
任何人都可以阐明为什么会发生这种情况,以及我可能如何实现目标?
非常感谢,
-James
您需要在手表中指定 nospawn:true
任务配置,以便被调用的任务在相同的上下文中运行。有关更多信息/示例,请参阅文档的本节。
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
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.
这篇关于gruntjs加载外部配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!