問(wèn)題描述
當(dāng)我在沒(méi)有 node
任務(wù)的情況下運(yùn)行 gulp 時(shí),它可以正常工作并按預(yù)期處理客戶端文件,如果我運(yùn)行 gulp node
它會(huì)按預(yù)期處理服務(wù)器文件.但是,如果我同時(shí)運(yùn)行 gulp
它會(huì)按預(yù)期同時(shí)處理客戶端和服務(wù)器文件,但是,它不會(huì)讓我通過(guò)按Ctrl + C"退出(在 Windows 10 和 Mac El Capitan 上嘗試過(guò))).我在這里做錯(cuò)了嗎?
When I run gulp without the node
task, it works fine and processes client file as expected, If I run gulp node
it processes server file as expected. However if I run both gulp
it processes both client and server file as expected, however, it won't let me quit by pressing 'Ctrl + C' (Tried it on windows 10 & Mac El Capitan). Is there something I'm doing wrong here?
'use strict';
var gulp = require('gulp');
var connect = require('gulp-connect');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var nodemon = require('gulp-nodemon');
var config = {
port: 9005,
devBaseUrl: 'http://localhost',
paths: {
html: './src/*.html',
dist: './dist',
js: './src/**/*.js',
images: './src/images/*',
mainJs: './src/main.js',
css: [
'node_modules/bootstrap/dist/css/bootstrap.min.css',
'node_modules/bootstrap/dist/css/bootstrap-theme.min.css'
]
}
};
gulp.task('connect', function () {
connect.server({
root: ['dist'],
port: config.port,
base: config.devBaseUrl,
livereload: true
});
});
gulp.task('html', function () {
gulp.src(config.paths.html)
.pipe(gulp.dest(config.paths.dist))
});
gulp.task('js', function () {
browserify(config.paths.mainJs)
.bundle()
.on('error', console.error.bind(console))
.pipe(source('bundle.js'))
.pipe(gulp.dest(config.paths.dist + '/scripts'))
.pipe(connect.reload())
});
gulp.task('node', function () {
nodemon({
script: 'server/index.js',
ext: 'js',
env: {
PORT: 8000
},
ignore: ['node_modules/**','src/**','dist/**']
})
.on('restart', function () {
console.log('Restarting node server...');
})
});
gulp.task('watch', function () {
gulp.watch(config.paths.js, ['js']);
});
gulp.task('default', ['html', 'js', 'connect', 'node', 'watch']);
推薦答案
我之前遇到過(guò)類(lèi)似的問(wèn)題,這就是您要找的:
I had a similar issue before this is what you're looking for :
process.on('SIGINT', function() {
setTimeout(function() {
gutil.log(gutil.colors.red('Successfully closed ' + process.pid));
process.exit(1);
}, 500);
});
只需將此代碼添加到您的 gulp 文件中即可.它將監(jiān)視 ctrl + C 并正確終止該過(guò)程.如果需要,您也可以在超時(shí)中添加一些其他代碼.
Just add this code to your gulp file. It will watch the ctrl + C and properly terminate the process. You can put some other code in the timeout also if desired.
這篇關(guān)于使用 gulp-nodemon & 時(shí)無(wú)法使用 Ctrl+C 停止 Gulpgulp.watch 一起看的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!