简体   繁体   中英

Gulp + Growl + Browser-sync notification

I use gulp and growl. I create this gulpfile.js :

var gulp = require("gulp");
var less = require("gulp-less");
var notifier = require('node-notifier');
var notify = require("gulp-notify");
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;

gulp.task('less', function() {
 var trete;
 console.log(trete);
 gulp.src('css/main.less')
 .pipe(less())
 .on('error', function(err){
    trete = err.message;
    notifier.notify({
        'title': 'My notification',
        'message': trete
    });
    return false;
})
.pipe(notify("Всё заебись!"))
.pipe(gulp.dest('css/'))
});

gulp.task('bs-reload', function () {
browserSync.reload();
});
gulp.task('browser-sync', function() {
browserSync.init(['css/*.css', 'js/*.js'], {
    server: {
        baseDir: './'
    }
});
});

gulp.task('watch', function() {
gulp.watch('css/**', ['less']);
gulp.run('less');
gulp.watch(['*.html'], ['bs-reload']);
});

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

When I run gulp, growl shows two notification and browser-sync twice reload page. Maybe I do something wrong?

You run 'less' twice in the 'default' task. Its the first dependent task, will run, then it runs again in the 'watch' task because of gulp.run.

Remove gulp.run in 'watch' task.

Add 'less' as dependent task for 'watch' task instead.

You probably want to stream your styles instead of full page reload so pipe less through this at the end.

.pipe(gulp.dest(cssPath))
.pipe(browserSync.stream());

Your task do not return their streams, they will never finish due to that. Tasks should always return a stream or take in a done-callback and call that if they are async.

var gulp = require("gulp");
var less = require("gulp-less");
var notifier = require('node-notifier');
var notify = require("gulp-notify");
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;

gulp.task('less', function() {
    return gulp.src('css/main.less')
        .pipe(less())
            .on('error', function(err){
                notifier.notify({
                    'title': 'My notification',
                    'message': err.message
                });
                return false;
           })
       .pipe(notify("Всё заебись!"))
       .pipe(gulp.dest('css/'))
       .pipe(browserSync.stream());
});

gulp.task('browser-sync', ['less'], function() {
    browserSync.init({
        server: {
            baseDir: './'
        }
    });
});

gulp.task('watch', ['less'], function() {
    gulp.watch('css/**', 'less');
    gulp.watch('*.html').on('change', reload);
});

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

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