Gulp通知全局成功功能

Gulp Notify global success function

本文关键字:功能 成功 全局 通知 Gulp      更新时间:2023-09-26

我希望有gulp-notify成功函数被调用,我可以重用。我对所有的任务都使用相同的格式,我想把它清理一下。下面是我现在正在做的一个例子:

gulp.task('build-css', function() {
    const s = gsize();
    return gulp.src('src/css/main.css')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(cssmin())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist/css'))
        .pipe(s)
        .pipe(notify({
            title: function () {
                return '<%= file.relative %> - ' + s.prettySize;
            },
            onLast: true,
            subtitle: "Successfully Compiled",
            message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
            templateOptions: {
                hour: new Date().getHours(),
                minute: new Date().getMinutes(),
                second: new Date().getSeconds()
            }
        }))
});

我在多个任务中重用相同的通知函数。我试过这样做,但每次尝试都会抛出一个错误。这个特殊的错误是与水管工- Can't Pipe to Undefined

var onSuccess = function () {
    const s = gsize();
    notify({
        title: function () {
            return '<%= file.relative %> - ' + s.prettySize;
        },
        onLast: true,
        subtitle: "Successfully Compiled",
        message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
        templateOptions: {
            hour: new Date().getHours(),
            minute: new Date().getMinutes(),
            second: new Date().getSeconds()
        }
    })
};
...
gulp.task('build-css', function() {
    const s = gsize();
    return gulp.src('src/css/main.css')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(autoprefixer({
            browsers: ['last 6 versions'],
            cascade: false
        }))
        .pipe(cssmin())
        .pipe(rename({suffix: '.min'}))
        .pipe(s)
        .pipe(onSuccess())
        .pipe(gulp.dest('dist/css'))
        .pipe(reload({stream: true}));
});

任何关于如何实现这一点的想法都是赞赏的!

EDIT在qballers解决方案之后,唯一的问题是我的gulp-size插件返回未定义的文件大小:

const s = gsize();
// Success Message
var notifyGeneric = {
    title: function () {
        return '<%= file.relative %> - ' + s.prettySize;
    },
    onLast: true,
    subtitle: "Successfully Compiled",
    message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
    templateOptions: {
        hour: date.getHours(),
        minute: date.getMinutes(),
        second: date.getSeconds()
    }
};
...
gulp.task('build-css', function() {
    const s = gsize();
    return gulp.src(srcCssPath + 'main.css')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(autoprefixer({
            browsers: ['last 6 versions'],
            cascade: false
        }))
        .pipe(cssmin())
        .pipe(rename({suffix: '.min'}))
        .pipe(s)
        .pipe(notify(notifyGeneric))
        .pipe(gulp.dest(cssPath))
        .pipe(reload({stream: true}));
});

不确定这是否是您想要的解决方案,但您可以使用对象字面量来节省代码重复。

var notifyGeneric = {
            title: function () {
                return '<%= file.relative %> - ' + this.s.prettySize;
            },
            onLast: true,
            subtitle: "Successfully Compiled",
            message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
            templateOptions: {
                hour: new Date().getHours(),
                minute: new Date().getMinutes(),
                second: new Date().getSeconds()
            },
            s: {}
        };
gulp.task('build-css', function() {
    notifyGeneric.s = gsize();
    return gulp.src('src/css/main.css')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(cssmin())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist/css'))
        .pipe(notifyGeneric.s)
        .pipe(notify(notifyGeneric))
});