Gulp通知全局成功功能
Gulp Notify global success function
我希望有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))
});
相关文章:
- Jquery表单插件未显示成功功能
- 成功:功能(数据);不使用命名函数
- 只有在前一个功能成功完成的情况下,我才能正确地启动此功能
- JQuery AJAX没有't获得成功回调功能
- 谷歌登录成功功能在我阻止第三方cookie时没有任何作用
- 成功提交 Web 表单后,Javascript 重置功能将不起作用
- 骨干.js:Save的成功功能根本不起作用
- 如何触发一个新的 X-Editable 从当前功能的成功函数中打开
- ajax内部访问类功能成功
- 如何检查PHP邮件功能是否成功,然后运行一个javascript代码
- Extjs 4上传成功功能没有反应
- 将显示功能应用于ajax成功创建的元素
- 包括成功/失败jQuery / Ajax功能
- 列表框上的EventListener在物品掉落后不再工作[所有javascript功能在成功掉落后停止]
- AJAX成功服务器端功能
- Div不显示更改功能,但在ajax成功
- 只有在功能成功完成后才卸载页面
- 成功后启动下一个功能
- 为什么我不能用这个.贴出成功功能
- JQuery Mobile db成功事件不触发功能