Get “file basename” in gulp-replacer-task

陌路散爱 提交于 2019-12-12 03:33:39

问题


I'm manipulating a set of files and I am using gulp-replacer-task to replace the content of processed files with "strings" based on the basename or path of the file currently in the pipe-line.

How do i get at the file's properties currently in the pipe-line ?

gulp.task('svgbuild', function() {
    return gulp.src('./src/*.svg')
        .pipe(replace({
          patterns: [
            {
                match:       /STRING_TO_MATCH/g,
                replacement: function() {
                    // how to get the basename of the "file" in the stream;
                    var str = 'file.basename'              
                    // manipulate to get replacement string based on basename
                    var repl = str.toUpperCase()+'-inc'    
                    return repl;
                }
            }
            ]
        }))
});

回答1:


Somewhat later than I hoped, but it appears I found a solution for the problem using gulp-tap. This is what my gulpfile.js looks like:

var gulp = require('gulp'),
    path = require('path'),
    replace = require('gulp-replace-task'),
    tap = require('gulp-tap');

gulp.task('svgbuild', function () {
    return gulp.src('*.txt')
        .pipe(tap(function (file) {
            return gulp.src(file.path)
                .pipe(replace({
                    patterns: [
                        {
                            match: /foo/g,
                            replacement: function () {
                                var ext = path.extname(file.path),
                                    base = path.basename(file.path, ext);

                                return base.toUpperCase() + '-inc'
                            }
                        }
                    ]
                }))
            .pipe(gulp.dest('build'));
        }));
});



回答2:


I think you must look for the solution in Node.js. Maybe this helps: https://nodejs.org/api/path.html?



来源:https://stackoverflow.com/questions/31612192/get-file-basename-in-gulp-replacer-task

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!