【问题标题】:How do I get the base filename and pass that as a variable down the pipe in gulp?如何获取基本文件名并将其作为变量传递到 gulp 的管道中?
【发布时间】:2015-08-27 13:32:48
【问题描述】:

我正在尝试获取通过流的每个 html 文件的基本名称。这里的代码显然不起作用,因为_base 不再在范围内。似乎应该有一个非常简单的方法来做到这一点,或者也许 gulp-ejs 中内置了一些我不知道的东西。想法?

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

【问题讨论】:

    标签: gulp ejs gulp-tap


    【解决方案1】:

    你不能在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')))
    });
    

    【讨论】:

      【解决方案2】:

      您无法访问这样的文件名,因为您超出了正在运行的流的范围。您需要做的是使用您的水龙头中的文件名或使用 through2。您具体想达到什么目标?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-14
        • 2022-12-01
        • 2019-04-11
        • 1970-01-01
        • 2011-08-01
        • 2022-01-15
        • 1970-01-01
        • 2022-07-29
        相关资源
        最近更新 更多