Javascript 如何编写使用其他gulp插件的gulp插件?
我有一个用于预编译车把模板的任务,该任务包括以下内容:Javascript 如何编写使用其他gulp插件的gulp插件?,javascript,gulp,Javascript,Gulp,我有一个用于预编译车把模板的任务,该任务包括以下内容: gulp.src(['templates/*.hbs']) .pipe(handlebars()) .pipe(declare({ namespace: 'Template.templates', noRedeclare: true })) .pipe(concat('compiled.js')) .pipe(header('Template = {};\nTemplate.r
gulp.src(['templates/*.hbs'])
.pipe(handlebars())
.pipe(declare({
namespace: 'Template.templates',
noRedeclare: true
}))
.pipe(concat('compiled.js'))
.pipe(header('Template = {};\nTemplate.render = function(templateName, context) { return Handlebars.template(Template.templates[templateName])(context) };\n'))
.pipe(gulp.dest('templates'));
我想创建一个gulp插件来包装此功能,使其更易于使用,如下所示:
gulp.src(['templates/*.hbs'])
.pipe(handlebars2())
.dest('templates')
或选择:
gulp.src(['templates/*.hbs'])
.pipe(handlebars2({
filename: 'compiled.js',
namespace: 'Template'
}))
.dest('templates')
编写gulp插件的文档和我看过的示例源代码只展示了streams的用法,我不知道如何将其应用到利用我自己内部的其他gulp插件
我如何编写一个gulp插件来实现上述handlebars2
的功能?
即使有人只是给我指出了正确的方向,我也可以解决它,并将其作为其他人的答案发布。谢谢 gulp插件的作用是返回一个流,该流将使用另一个流中的数据:
readable.pipe(writable);
当您试图制作一个“吞咽”插件时,您无法直接访问正在读取的流。您只能访问它传递给您的数据(例如,乙烯基文件)
这样,你就不能用传统的方式进行管道连接。您试图做的是重用部分管道
要轻松实现这一目标,请尝试使用lazypipe
:
var lazypipe = require('');
var handlebars2 = lazypipe()
.pipe(handlebars)
.pipe(declare,{
namespace: 'Template.templates',
noRedeclare: true
})
.pipe(concat,'compiled.js')
.pipe(header,'Template = {};\nTemplate.render = function(templateName, context) { return Handlebars.template(Template.templates[templateName])(context) };\n');
记住不要在这里调用流创建函数,lazypipe会为您调用它们
希望我能帮上忙。由于gulp插件只处理流,所以您可以使用
流组合器
:
var combine = require('stream-combiner');
function coffeePipe() {
return combine(
coffeescript(),
coffeelint.reporter('fail').on('error', function(){
gutil.beep();
gulp.run('lint');
});
};
//usage:
gulp.src().pipe(coffeePipe());
我发现《懒人管》作者的评论揭示了:
老实说,当我创建Lazyppe时,我真希望我刚刚想到了后一种解决方案。其实是一样的,只是稍微详细一点,解决了同样的问题。不过懒人管很受欢迎,所以我很高兴它被使用了
我喜欢流合并器解决方案。要不是你的回答,我是不会找到的。谢谢@雷恩,就我个人而言,我不使用懒惰的管道,我后来提出了一个我更喜欢的解决方案。不管怎样,我很高兴它帮助了你。