如何在每次更改Gulp文件后重新启动Gulp? [英] How can Gulp be restarted upon each Gulpfile change?
本文介绍了如何在每次更改Gulp文件后重新启动Gulp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个 Gulpfile
。一旦它发生变化,它可以重新启动吗?我正在CoffeeScript中开发它。可以 Gulp
观看 Gulpfile.coffee
以便在保存更改时重新启动
<您可以创建一个
任务
,这个任务 gulp.watch
for gulpfile.js
,只需 产卵另一个gulp child_process
。 $ b
var gulp = require ('gulp'),
argv = require('yargs')。argv,//用于args解析
spawn = require('child_process')。spawn;
$ b gulp.task('log',function(){
console.log('CSSs has changed');
});
$ b gulp.task('watching-task',function(){
gulp.watch('*。css',['log']);
});
$ b gulp.task('auto-reload',function(){
var p;
gulp.watch('gulpfile.js',spawnChildren);
spawnChildren();
函数spawnChildren(e){
//杀死前一个产生的进程
if(p){p.kill();}
$ b $ //``spawn`一个孩子连接到父`stdio`的gulp过程
p = spawn('gulp',[argv.task],{stdio:'inherit'});
}
});
我使用 yargs
,以便接受主要任务在我们需要重新启动后运行。所以为了运行它,你可以调用:
gulp auto-reload --task watching-task
要测试,请调用 touch gulpfile.js
或触摸a.css
查看日志。
I am developing a Gulpfile
. Can it be made to restart as soon as it changes? I am developing it in CoffeeScript. Can Gulp
watch Gulpfile.coffee
in order to restart when changes are saved?
解决方案
You can create a task
that will gulp.watch
for gulpfile.js
and simply spawn
another gulp child_process.
var gulp = require('gulp'),
argv = require('yargs').argv, // for args parsing
spawn = require('child_process').spawn;
gulp.task('log', function() {
console.log('CSSs has been changed');
});
gulp.task('watching-task', function() {
gulp.watch('*.css', ['log']);
});
gulp.task('auto-reload', function() {
var p;
gulp.watch('gulpfile.js', spawnChildren);
spawnChildren();
function spawnChildren(e) {
// kill previous spawned process
if(p) { p.kill(); }
// `spawn` a child `gulp` process linked to the parent `stdio`
p = spawn('gulp', [argv.task], {stdio: 'inherit'});
}
});
I used yargs
in order to accept the 'main task' to run once we need to restart. So in order to run this, you would call:
gulp auto-reload --task watching-task
And to test, call either touch gulpfile.js
or touch a.css
to see the logs.
这篇关于如何在每次更改Gulp文件后重新启动Gulp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文