gulp 在 运行 个任务后不退出

gulp doesn't exit after running tasks

当服务器上的 运行 gulp 完成任务后不会退出。这是我得到的:

[01:16:02] Using gulpfile /test/test/gulpfile.js
[01:16:02] Starting 'clean'...
[01:16:02] Finished 'clean' after 11 ms
[01:16:02] Starting 'default'...
[01:16:02] Starting 'styles'...
[01:16:02] Starting 'scripts'...
[01:16:02] Starting 'watch'...
[01:16:02] Finished 'watch' after 20 ms
[01:16:02] Finished 'default' after 40 ms
[01:16:03] gulp-notify: [Gulp notification] Styles task complete
[01:16:03] Finished 'styles' after 338 ms
[01:16:03] gulp-notify: [Gulp notification] Scripts task complete
[01:16:03] Finished 'scripts' after 921 ms

它卡在那里,没有返回到命令提示符。这是我的 gulpfile.js:

var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
minifyCss = require('gulp-minify-css'),
del = require('del');

 gulp.task('styles', function() {
  return gulp.src('./test/css/*.css') 
.pipe(autoprefixer('last 2 version'))
.pipe(concat('main.css')) 
.pipe(gulp.dest('./dist/styles')) 
.pipe(rename({ suffix: '.min' })) 
.pipe(minifyCss())
.pipe(gulp.dest('./dist/styles'))
.pipe(notify({ message: 'Styles task complete' }));
 });

  gulp.task('scripts', function() {
 return gulp.src('./test/js/*.js')
.pipe(concat('main.js'))
.pipe(gulp.dest('./dist/scripts'))
.pipe(rename({ suffix: '.min' }))
.pipe(uglify())
.pipe(gulp.dest('./dist/scripts'))
.pipe(notify({ message: 'Scripts task complete' }));
 });


gulp.task('clean', function() {
 return del(['dist/styles', './dist/scripts']);
 });


gulp.task('default', ['clean'], function() {
gulp.start('styles', 'scripts', 'watch');
 });

gulp.task('watch', function() {
gulp.watch('./test/css/*.css', ['styles']);
gulp.watch('./test/*.js', ['scripts']);

});

返回命令提示符的唯一方法是使用 Ctrl+C。

如果您 运行 正在执行默认的 gulp 任务(仅 运行 正在 gulp 在命令行中不带任何参数),那么它不会return 因为您的默认任务会调用您的 watch 任务。

gulp.task('watch', function() {
  gulp.watch('./test/css/*.css', ['styles']);
  gulp.watch('./test/*.js', ['scripts']);
}); 

此任务将监视等待更改的指定位置,并且当更改发生时,将运行指定的任务。它本身并不return。您必须手动中断它。

如果您想创建另一个只构建您的代码然后退出的任务,您可以使用以下命令:

gulp.task('build', ['clean', 'styles', 'scripts']);

然后 运行 使用命令:

gulp build