简体   繁体   English

如何获取基本文件名并将其作为变量传递给gulp中的管道?

[英]How do I get the base filename and pass that as a variable down the pipe in gulp?

I'm trying to grab the base name of each html file that passes through a stream. 我正在尝试获取通过流传递的每个html文件的基本名称。 The code here obviously doesn't work because _base is no longer in scope. 显然,这里的代码不起作用,因为_base不在范围内。 It seems like there should be a really easy way to do this, or maybe there's something built into gulp-ejs that I don't know about. 似乎应该有一种非常简单的方法来执行此操作,或者可能是gulp-ejs中内置了一些我不知道的东西。 Thoughts? 有什么想法吗?

gulp.task('html', function () {
    return gulp.src('app/*.html')
    .pipe(tap(function(file, t){
        var _base = path.basename(file.path, '.html');
    })
    .pipe($.ejs(require('./app/'+_base+'.json')))
});

Can't you just declare _base above the return statement? 您不能只在return语句上方声明_base吗?

gulp.task('html', function () {
    var _base;
    return gulp.src('app/*.html')
    .pipe(tap(function(file, t){
        _base = path.basename(file.path, '.html');
    })
    .pipe($.ejs(require('./app/'+_base+'.json')))
});

You cannot access the filename like that because you are out of the scope of the running stream. 您无法访问那样的文件名,因为您超出了运行流的范围。 What you need to do is use the filename in your tap or use through2. 您需要做的是使用水龙头中的文件名或使用through2。 What are you trying to achieve specifically? 您要具体实现什么?

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

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