2015-04-03 63 views
1

我在gulpfile.js以下任务:如何使多条管线门面在咕嘟咕嘟任务

gulp.task('css', function() { 
    return gulp.src('theme.scss') 
    .pipe(...) 
    .pipe(...) 
    .pipe(...) 
    .pipe(...) 
    .pipe(...) 
    .pipe(...) 
    .pipe(gulp.dest('dist')); 
}); 

,我想所有这些管道移动到一个模块中,以获得类似的东西:

gulp.task('css', function() { 
    return gulp.src('theme.scss') 
    .pipe(theme()) 
    .pipe(gulp.dest('dist')); 
}); 

这可能吗?

+0

你也许可以创建一个单独的功能,如包裹(),它返回this.pipe(..) .pipe(..),然后在调用时将你的上下文绑定到它。比如'pipe(wrapped()。bind(this))',或类似的东西。现在在我的手机上,但我可以尝试稍后再玩。 – 2015-04-03 20:09:19

回答

1

lazypipe做到了,我单独的模块以这种方式被实现:

module.exports = function() { 
    var pipes = lazypipe() 
    .pipe(...) 
    .pipe(...) 
    .pipe(...) 
    .pipe(...) 
    .pipe(...) 
    .pipe(...); 

    return pipes(); 
}; 
0

我可以想象的最简单的方法是这样的:

function theme(stream) { 
    return stream.pipe(less()) 
     .pipe(minify()) 
     .pipe(prefix()) 
} 

gulp.task('less', function() { 
    return theme(gulp.src('theme.less')) 
     .pipe(gulp.dest('public')); 
}) 

(完成与少,B/C吞掉,红宝石青菜,现在有一些新的接口)。 如果你想成为灵活,可以为您在主题功能要管之多:

function theme(stream) { 
    return stream.pipe(minify()) 
     .pipe(prefix()) 
} 

gulp.task('less', function() { 
    return theme(gulp.src('theme.less').pipe(less())) 
     .pipe(gulp.dest('public')); 
}) 

这不是漂亮,你可能会希望它是,但它的交易没有太多额外的包。