我正在做Wes Bos的反应教程,但经过几次gulp重新绑定它停止工作并停止在这个应用程序状态。这真的很令人沮丧,因为每当某些东西不起作用时,我必须检查它的代码或gulp是否停止工作。
这是我的gulp代码:
var source = require('vinyl-source-stream');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var babelify = require('babelify');
var watchify = require('watchify');
var notify = require('gulp-notify');
var stylus = require('gulp-stylus');
var autoprefixer = require('gulp-autoprefixer');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var buffer = require('vinyl-buffer');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var historyApiFallback = require('connect-history-api-fallback')
/*
Styles Task
*/
gulp.task('styles',function() {
// move over fonts
gulp.src('css/fonts/**.*')
.pipe(gulp.dest('build/css/fonts'))
// Compiles CSS
gulp.src('css/style.styl')
.pipe(stylus())
.pipe(autoprefixer())
.pipe(gulp.dest('./build/css/'))
.pipe(reload({stream:true}))
});
/*
Images
*/
gulp.task('images',function(){
gulp.src('css/images/**')
.pipe(gulp.dest('./build/css/images'))
});
/*
Browser Sync
*/
gulp.task('browser-sync', function() {
browserSync({
// we need to disable clicks and forms for when we test multiple rooms
server : {},
middleware : [ historyApiFallback() ],
ghostMode: false
});
});
function handleErrors() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'Compile Error',
message: '<%= error.message %>'
}).apply(this, args);
this.emit('end'); // Keep gulp from hanging on this task
}
function buildScript(file, watch) {
var props = {
entries: ['./scripts/' + file],
debug : true,
cache: {},
packageCache: {},
transform: [babelify.configure({stage : 0 })]
};
// watchify() if watch requested, otherwise run browserify() once
var bundler = watch ? watchify(browserify(props)) : browserify(props);
function rebundle() {
var stream = bundler.bundle();
return stream
.on('error', handleErrors)
.pipe(source(file))
.pipe(gulp.dest('./build/'))
// If you also want to uglify it
// .pipe(buffer())
// .pipe(uglify())
// .pipe(rename('app.min.js'))
// .pipe(gulp.dest('./build'))
.pipe(reload({stream:true}))
}
// listen for an update and run rebundle
bundler.on('update', function() {
rebundle();
gutil.log('Rebundle...');
});
// run it once the first time buildScript is called
return rebundle();
}
gulp.task('scripts', function() {
return buildScript('main.js', false); // this will run once because we set watch to false
});
// run 'scripts' task first, then watch for future changes
gulp.task('default', ['images','styles','scripts','browser-sync'], function() {
gulp.watch('css/**/*', ['styles']); // gulp watch for stylus changes
return buildScript('main.js', true); // browserify watch for JS changes
});
答案 0 :(得分:0)
这是因为错误捕获并不能告诉应用程序继续。为了捕获错误并阻止它们停止进程,请使用gulp-plumber
等插件。
通过在构建过程中添加以下一个管道来使用gulp-plumber
首先,需要gulp-plumber
插件:
var plumber = require('gulp-plumber');
然后,将此代码添加到您的gulp任务中:(理想情况下,在您的.src()
函数之后)
.pipe(plumber({
errorHandler: function (err) {
console.log(err);
this.emit('end');
}
}))
答案 1 :(得分:0)
创建回调
gulp.task('styles',function(cb) {
// move over fonts
gulp.src('css/fonts/**.*')
.pipe(gulp.dest('build/css/fonts'))
// Compiles CSS
gulp.src('css/style.styl')
.pipe(stylus())
.pipe(autoprefixer())
.pipe(gulp.dest('./build/css/'), cb());
});
这使得流知道任务何时完成