【问题标题】:Cannot stop Gulp with Ctrl+C when using gulp-nodemon & gulp.watch together一起使用 gulp-nodemon 和 gulp.watch 时无法使用 Ctrl+C 停止 Gulp
【发布时间】:2018-07-20 04:54:14
【问题描述】:

当我在没有 node 任务的情况下运行 gulp 时,它可以正常工作并按预期处理客户端文件,如果我运行 gulp node 它会按预期处理服务器文件。但是,如果我同时运行gulp,它会按预期同时处理客户端和服务器文件,但是,它不会让我通过按“Ctrl + C”退出(在 Windows 10 和 Mac El Capitan 上尝试过)。我在这里做错了吗?

'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']);

【问题讨论】:

  • 你能用最少的代码来证明你的问题吗?
  • 我已经删除了不需要的部分

标签: javascript gulp


【解决方案1】:

就在你的顶部

var monitorCtrlC = require('monitorctrlc');

在您拥有的watch 任务中

monitorCtrlC();

好像是this library

当 Ctrl+C 时,此功能将阻止发送 SIGINT 信号 按下。相反,将调用指定(或默认)的回调。

【讨论】:

  • 这是我在 windows 上开发时使用的东西。由于在 Windows 上需要先按“Ctrl + C”,然后确认“是/否”来结束批处理,monitorCtrlC() 只是有助于删除 Windows 中的额外步骤。但是我已经尝试完全删除它,因为我在 Mac 上不需要它,最终结果仍然是一样的。
  • 你可能不应该把它包括在这个问题中,因为它完全符合你的抱怨:)
  • 抱歉,为了避免混淆,我已将其删除
【解决方案2】:

在您要查找的内容之前,我遇到了类似的问题:

process.on('SIGINT', function() {
  setTimeout(function() {
    gutil.log(gutil.colors.red('Successfully closed ' + process.pid));
    process.exit(1);
  }, 500);
});

只需将此代码添加到您的 gulp 文件中即可。它将监视 ctrl + C 并正确终止该过程。如果需要,您也可以在超时中添加一些其他代码。

【讨论】:

    【解决方案3】:

    以防万一它对其他人有帮助,我删除了 node_modules 并做了 npm install 为我解决了这个问题...

    【讨论】:

      【解决方案4】:

      SIGINT 解决方案对我不起作用,可能是因为我也在使用 gulp-nodemon 但这有效:

          var monitor = $.nodemon(...)
          // Make sure we can exit on Ctrl+C
          process.once('SIGINT', function() {
          monitor.once('exit', function() {
                console.log('Closing gulp');
                process.exit();
              });
          });
          monitor.once('quit', function() {
              console.log('Closing gulp');
              process.exit();
          });
      

      从here得到它。

      【讨论】:

        猜你喜欢
        • 2016-08-29
        • 2016-10-01
        • 2016-03-29
        • 2017-08-10
        • 2015-12-17
        • 2016-12-17
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        相关资源
        最近更新 更多