简体   繁体   English

Gulp通知全局成功功能

[英]Gulp Notify global success function

I'm looking to have gulp-notify success function that is called which I can reuse. 我正在寻找可以重复使用的gulp-notify成功函数。 I'm using the same format for all of my tasks and would like to clean it up. 我对所有任务都使用相同的格式,并希望对其进行清理。 Here's an example of what I'm doing right now: 这是我现在正在做的一个例子:

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()
            }
        }))
});

I'm re-using the same notify function with multiple tasks. 我将同一通知功能用于多个任务。 I've tried doing something like this, but each attempt throws an error. 我已经尝试过执行类似的操作,但是每次尝试都会引发错误。 This particular error is with plumber - Can't Pipe to Undefined 这个特定的错误与水管工有关- 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}));
});

Any thoughts on how to accomplish this are appreciated! 任何关于如何实现这一目标的想法都将受到赞赏!

EDIT After qballers solution, only issue is my gulp-size plugin returning undefined for file sizes: 编辑 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}));
});

Not sure if this the solution you are gunning for but you can just use object literals to save you code duplications. 不确定这是否是您想要的解决方案,但是您可以仅使用对象文字来节省代码重复。

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))
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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