如何处理gulp-jspm引发的错误?

时间:2015-11-17 00:06:58

标签: javascript error-handling gulp jspm

如何添加一个.on侦听器来阻止gulp崩溃,并在下次检测到文件重新加载时再试一次?

这就是我目前所尝试的

'use strict';

const gulp = require('gulp');
const jspm = require('gulp-jspm');

gulp.task('default', () => {
  return gulp.watch('./src/**/*.js', ['bundle'])
    .on('error', function (error) {
      console.log('error');
      this.emit('end');
    });
});

gulp.task('bundle', () => {
  return gulp.src('./src/main.js')
    .pipe(jspm({ selfExecutingBundle : true })
      .on('error', function (error) {
        console.log('error');
        this.emit('end');
      })
    )
    .on('error', function (error) {
      console.log('error');
      this.emit('end');
    })
    .pipe(gulp.dest('./dist/'));
});

其中没有一个捕获错误,或阻止gulp崩溃。

1 个答案:

答案 0 :(得分:0)

您可以在此处查看我的gulpfile.js

const gulp = require('gulp');
const gulp_jspm = require('gulp-jspm');

const paths = {
    //Your main.js
    main: 'public/js/main.js',
    //Your all js files
    scripts: 'public/js/**',
    dest: 'public/dest/'
};

gulp.task('scripts', () => {
    gulp.src(paths.main)
      .pipe(gulp_jspm({
          selfExecutingBundle: true
      }))
      .pipe(gulp.dest(paths.dest));
});

function reportChange(event){  
    console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
}

gulp.task('watch', () => {
    gulp.watch([paths.scripts], ['scripts']).on('change', reportChange);
});

gulp.task('default', ['watch', 'scripts']);
相关问题