javascript - gulp 观察者和 promise 导致崩溃

标签 javascript node.js gulp gulp-watch

我正在尝试 Gulp,并设置了一个任务来监视我的 .js 文件,以便在修改它们时运行我的测试并检查我的代码。

但是,自从我开始在代码中使用 Promise 后,监视任务就中断了。第一次修改监视文件时它会运行良好,但此后每次都会崩溃:

Message:
    Object.keys called on non-object
Stack:
TypeError: Object.keys called on non-object
    at Function.keys (native)
    at processExports (/Users/hfp/code/coverall/node_modules/nodegit/node_modules/promisify-node/index.js:44:16)
    at module.exports (/Users/hfp/code/coverall/node_modules/nodegit/node_modules/promisify-node/index.js:112:10)
    at Object.<anonymous> (/Users/hfp/code/coverall/node_modules/nodegit/lib/nodegit.js:96:26)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/hfp/code/coverall/lib/archive.js:10:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/hfp/code/coverall/test/archive_spec.js:6:15)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

这是我完整的 Gulpfile,我使用 gulp dev 运行监视任务:

var gulp = require('gulp');
var mocha = require('gulp-mocha');
var gutil = require('gulp-util');
var eslint = require('gulp-eslint');
var console = require('better-console');
var runSequence = require('run-sequence');
var istanbul = require('gulp-istanbul');
var insert = require('gulp-insert');

var spec_files = 'test/**/*_spec.js';
var integration_files = 'test/**/*_integration.js';
var code_files = ['lib/**/*.js', 'helpers/**/*.js', 'index.js'];
var all_files = code_files.concat(spec_files, integration_files);
var coverage_report_dir = 'test/coverage';
var mocha_reporter = 'list';
var eslint_mocha_header = '/*eslint-env mocha */\n';

gulp.task('mocha', function() {
  return gulp.src(spec_files, { read: false })
    .pipe(mocha({ reporter: mocha_reporter }));
});

gulp.task('cov', function(cb) {
  return gulp.src(code_files)
    .pipe(istanbul({ includeUntested: true }))
    .pipe(istanbul.hookRequire())
    .on('finish', function () {
      gulp.src([spec_files, integration_files])
        .pipe(mocha({ reporter: 'dot' }))
        .on('error', function(err) {
          console.log('Error in tests, not checking coverage.');
          cb(err);
        })
        .pipe(istanbul.writeReports( { reporters: ['html', 'text', 'text-summary'] }))
        .on('end', cb);
    });
});

gulp.task('dev', function() {
  return gulp.watch(all_files, ['test'], { read: false });
});

gulp.task('lint', ['eslint-add-mocha-headers'], function(cb) {
  return gulp.src(all_files)
    .pipe(eslint({ useEslintrc: true }))
    .pipe(eslint.format())
    cb(err);
});

gulp.task('clear-console', function() {
  return console.clear();
});

gulp.task('eslint-add-mocha-headers', function(cb) {
  var truncated_header = eslint_mocha_header.substring(0, eslint_mocha_header.length - 1);
  // turn the header into a regex so if I update the header, this task doesn't break
  var header_regex = new RegExp('^' + truncated_header.replace(/\*/gi, '\\*').replace(/\//gi, '\\/'));

  return gulp.src(spec_files)
    .pipe(insert.transform(function(contents, file) {
      if (contents.match(header_regex)) {
        return contents;
      }
      return eslint_mocha_header + contents;
    }))
    .pipe(gulp.dest('test/'));
});

gulp.task('test', function(cb) {
  return runSequence('clear-console',
              'lint',
              'mocha',
              cb);
});

最佳答案

我也遇到过同样的问题,但解决方案因情况而异。这三个之一通常会帮我解决这个问题:

1) 切换到运行序列的非回调语法。例如:

gulp.task('test', function(){ 
    return runSequence('clear-console', 'lint', 'mocha'); 
});

2) 尝试在 runSequence 内部定义 eslint-add-mocha-headers “顺序”依赖项,而不是以两种不同的方式定义所需的运行顺序。这在过去的某些情况下为我解决了这个问题。 (无论如何,它更具可读性)。例如:

gulp.task('test', function(){ 
    return runSequence('eslint-add-mocha-headers', 'clear-console', 'lint', 'mocha'); 
});

3) 这是你最好的选择:尽量不要返回 gulp.watch。 gulp watch 讨厌被返回:在大多数情况下,我已经得到了这样的结果:重复为相同的文件分配更多的观察者,每次对文件进行更改时观察者的数量都会增加。这通常会导致运行 Gulp 的 Node 进程崩溃(由于内存耗尽、文件锁定冲突和/或分配的观察程序超出进程支持的数量)。例如:

gulp.task('dev', function() {
    gulp.watch(all_files, ['test'], { read: false });
});

哪一行代码抛出错误?你有没有得到堆栈跟踪?

关于javascript - gulp 观察者和 promise 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33018763/

相关文章:

javascript - 我们应该在reducer中比较新状态和旧状态吗

javascript - Heroku 与 Express/Nodejs 崩溃?

node.js - 当电子邮件或密码为空时, Passport 本地策略不会被调用

JSON 结合保持文件夹结构的 gulp 任务 - 跨平台

node.js - 链式顺序任务

javascript - 将键盘事件添加到 Openlayers map

javascript - 用于深度链接的移动 Safari 页面卸载/隐藏/模糊

node.js - 如何通过 Nodejs Express 应用程序调用 API 获取 png 图像

javascript - 无法从 'react-native-implementation.js' 找到模块

javascript - 如何在 componentDidMount 中模拟类函数