吞噬水龙头进入无限循环
我试图获取当前的文件名在globstream内,以便我可以将它传递给玉来正确命名我的模板。 通常它看起来像:
gulp.task('templatesjs', function() { gulp.src('src/templates/*.jade') .pipe(jade({ client: true, name:"filenamehere" })) .pipe(gulp.dest('build/templates')) });
我在网上find的唯一答案是通过使用gulp-tap来提取stream中的文件名:
gulp.task('templatesjs', function() { gulp.src('src/templates/*.jade') .pipe(tap(function(file, t) { var filename = file.relative.split('.')[0]; return t.through(jade, [{client:true, name:filename}]); })) .pipe(gulp.dest('build/templates')) });
但是由于某种原因,这个进入了一个无限循环。 如果glob匹配index.jade,例如第一个迭代的file.relative被设置为index.jade,但是它会在file.relative被设置为index.js的无限循环中被捕获,并且jade编译器崩溃试图编译index.js文件。 我知道这是因为,如果我省略客户端:true选项,file.relative是在第一次迭代index.jade和所有迭代后的index.html。 这当然不会崩溃,但会陷入一个无限循环,编译index.html到index.html。
我的解决scheme是检查文件的扩展名,只通过玉pipe道,如果我在一个包含玉文件的迭代。 这解决了无限循环,但它是sl and,它复制一个玉文件到我的build立目录(不是一个巨大的交易)。 它也只对第一个文件起作用,对所有文件起作用后都得到第一个文件的名字。 我知道我做错了什么,但我不知道是什么。
gulp.task('templatesjs', function() { gulp.src('src/templates/*.jade') .pipe(tap(function(file, t) { sourcefile = file.relative; filename = sourcefile.split('.')[0] if(sourcefile.split('.')[sourcefile.split('.').length-1]!='js') return t.through(jade, [{client:true, name:filename}]); else return t; })) .pipe(gulp.dest('build/templates')) });
随着through
你复制stream,有效地创build一个stream的stream,因此无限循环。
这应该工作:
gulp.task('templatesjs', function() { gulp.src('src/templates/*.jade') .pipe(tap(function(file, t) { var filename = file.relative.split('.')[0]; return gulp.src(file.path) .pipe(jade({client:true, name:filename})) .pipe(gulp.dest('build/templates')); })) });
所以我还是不太了解乙烯stream或者点击,但是至less我已经使用这个工具了:
https://www.npmjs.com/package/gulp-jade-jst-concat
在挣扎了4个小时之后,在3分钟内完成了工作
即使在修改示例之后,我也无法使其他模块( https://www.npmjs.com/package/gulp-tree-concat )正常工作。 希望这有助于他们的客户端gulp玉模块。