简体   繁体   English

如何编写使用其他gulp插件的gulp插件?

[英]How do I write a gulp plugin that uses other gulp plugins?

I have a gulp task for precompiling handlebars templates that consists of the following: 我有一个gulp任务用于预编译把手模板,包括以下内容:

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

I want to create a gulp plugin that wraps this functionality to make it easier to use, like this: 我想创建一个包含此功能的gulp插件,使其更易于使用,如下所示:

gulp.src(['templates/*.hbs'])
  .pipe(handlebars2())
  .dest('templates')

Or with options: 或者有选项:

gulp.src(['templates/*.hbs'])
  .pipe(handlebars2({
    filename: 'compiled.js',
    namespace: 'Template'
  }))
  .dest('templates')

The documentation for writing gulp plugins and the examples source code I've looked at only show the usage of streams, and I'm not sure how to apply that to leveraging other gulp plugins inside my own. 编写gulp插件的文档和我看过的示例源代码只显示了stream的用法,我不知道如何应用它来利用我自己的其他gulp插件。

How do I write a gulp plugin to accomplish the functionality of handlebars2 above? 如何编写gulp插件来完成上面的handlebars2的功能?

Even if someone just points me in the right direction, I can solve it and post the solution as an answer for others. 即使有人指出我正确的方向,我也可以解决它,并将解决方案作为其他人的答案。 Thanks! 谢谢!

What a gulp plugin does is to return a stream, which will consume data from another stream: gulp插件的作用是返回一个流,它将使用另一个流中的数据:

readable.pipe(writable);

As you are trying to make a gulp plugin, you don't have access directly to the stream you are reading from. 当您尝试制作gulp插件时,您无法直接访问正在读取的流。 You can only access the data it passes to you (the vinyl files, for example). 您只能访问它传递给您的数据(例如,乙烯基文件)。

This way you cannot pipe to it in a conventional way. 这样你就无法以传统的方式管道。 What you are trying to do is to reuse a partial pipeline. 您要做的是重用部分管道。

To achive such thing easily, try using lazypipe : 要轻松实现这一目标,请尝试使用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');

Remember not to call stream creation functions here, lazypipe will call them for you. 记住不要在这里调用流创建函数,lazypipe会为你调用它们。

Hope I've helped. 希望我帮了。

Since gulp plugins just process streams, you can use stream-combiner : 由于gulp插件只是处理流,你可以使用stream-combiner

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

https://github.com/OverZealous/lazypipe/issues/4#issuecomment-36785601 https://github.com/OverZealous/lazypipe/issues/4#issuecomment-36785601

I found this comment from the author of lazypipe revealing: 我发现lazypipe的作者发表了这条评论:

Honestly, when I created lazypipe, I wish I had just thought of this latter solution. 老实说,当我创建lazypipe时,我希望我刚刚想到了后一种解决方案。 It's really the same thing, only slightly more verbose, and solves the same issue. 它真的是一样的,只是稍微冗长一点,并解决了同样的问题。 lazypipe is pretty popular, though, so I'm glad it's being used! lazypipe很受欢迎,所以我很高兴它被使用了!

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

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