问题
I want my gulp watch task to run only once, then stop watching. Now, if I run 'gulp' from the command line, the watch tasks keeps the gulp task running. I have to hit Ctrl-C to stop it.
回答1:
when you run gulp watch, it will keep watching for file changes until you cancel it with CTRL-C. I you do not want this, then you can make a separate gulp task to do your building.
You would have 2 tasks then: gulp watch and gulp build. As the build wouldn't run the watch task, it will stop when finished.
Please take a look at a GitHub project of mine where I do things like this: skeletonSPA on GitHub.
If you take a look at the gulpfile.js
, you see that the default
task will execute the build
task.
The build task on its turn will execute info
, clean
, styles
, scripts
, images
, copy
, todo
; leaving you with a full build and working frontend.
After running these tasks, it will stop and gives you focus to the command line.
回答2:
Here's a very basic example GulpFile.js
:
var gulp = require('gulp'),
watch = require('gulp-watch'),
runSequence = require('gulp-sequence'),
webpack = require('webpack-stream');
gulp.task('webpack', function() {
return gulp.src('client/index.js')
.pipe(webpack({
output: {
filename: 'dist/bundle.js'
},
devtool: 'source-map'
}))
.pipe(gulp.dest('.'));
});
// other tasks
gulp.task('build', ['webpack', 'other tasks... ']);
gulp.task('watch', function(callback) {
gulp.watch('./client/**/*.js', {}, ['webpack']);
// more watches if required
});
gulp.task('default', function(callback) {
runSequence('build', 'watch', callback);
});
$ gulp build
> will just run build
the once and then exit.
$ gulp watch
> will start watching (ctrl-c
to exit).
$ gulp
> will execute build
and after that's complete it will run watch
(ctrl-c
to exit).
Note: I'm using the gulp-sequence
plug to ensure that when running $ gulp
the build will complete before the watch begins.
来源:https://stackoverflow.com/questions/27930288/terminate-gulp-watch-task