简体   繁体   中英

Gulp not working

I'm trying to run this following gulp that I found on a jekkyl template. I'm running on node 4 in a Windows 8.1 and i'm not understanding why every time i get the:

events.js:141
      throw er; //Unhandled 'error' event
      ^

I got the impression that is something related to windows, because when I ran on linux i didn't have problems.

This is the gulp:

gulp.task('jekyll-build', function (done) {
    browserSync.notify(messages.jekyllBuild);
    return cp.spawn('jekyll', ['build'], {stdio: 'inherit'})
        .on('close', done);
});

gulp.task('jekyll-rebuild', ['jekyll-build'], function () {
    browserSync.reload();
});

gulp.task('browser-sync', ['jekyll-build'], function() {
    browserSync({
        server: {
            baseDir: '_site'
        }
    });
});

gulp.task('stylus', function(){
        gulp.src('src/styl/main.styl')
        .pipe(plumber())
        .pipe(stylus({
            use:[koutoSwiss(), prefixer(), jeet(),rupture()],
            compress: true
        }))
        .pipe(gulp.dest('_site/assets/css/'))
        .pipe(browserSync.reload({stream:true}))
        .pipe(gulp.dest('assets/css'))
});

gulp.task('js', function(){
    return gulp.src('src/js/**/*.js')
        .pipe(plumber())
        .pipe(concat('main.js'))
        .pipe(uglify())
        .pipe(gulp.dest('assets/js/'))
});

gulp.task('imagemin', function() {
    return gulp.src('src/img/**/*.{jpg,png,gif}')
        .pipe(plumber())
        .pipe(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))
        .pipe(gulp.dest('assets/img/'));
});

gulp.task('watch', function () {
    gulp.watch('src/styl/**/*.styl', ['stylus']);
    gulp.watch('src/js/**/*.js', ['js']);
    gulp.watch('src/img/**/*.{jpg,png,gif}', ['imagemin']);
    gulp.watch(['*.html', '_includes/*.html', '_layouts/*.html', '_posts/*'], ['jekyll-rebuild']);
});

gulp.task('default', ['js', 'stylus', 'browser-sync', 'watch']);

Does anybody know why and could help me understanding?

Basically the main problem is in the following line:

gulp.task('default', ['js', 'stylus', 'browser-sync', 'watch']);

The task in the array occur concurrently. If scripts and styles can usually be processed concurrently, browserSync could hardly serve a folder that is maybe not yet created. You have to rework the build process to add some synchronization here.

I see also another problem in:

// stylus task
.pipe(gulp.dest('_site/assets/css/'))
.pipe(browserSync.reload({stream:true}))
.pipe(gulp.dest('assets/css'))

or

.pipe(gulp.dest('assets/js/'))

or

.pipe(gulp.dest('assets/img/'));

You serve the _site folder, but sometimes you copy to _site/assets/ , sometimes to assets/ . Also, you should remove the last gulp.dest in the stylus task.

I'm glad that you tried to help. Fortunatelly I discovered that the problem is the way that Windows run scripts. I just added a ".bat" like below:

gulp.task('jekyll-build', function (done) {
    browserSync.notify(messages.jekyllBuild);
    return cp.spawn('jekyll.bat', ['build'], {stdio: 'inherit'})
        .on('close', done);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM