Gulp-注入转换不起作用

Gulp-inject transform doesn't work

我尝试设置 gulp-inject 以将依赖项注入 index.html。除了转换功能外,一切正常。我需要按以下方式替换部分文件路径: /frontend/src/ --> /static/ 我试过这样做(从某处复制粘贴):

transform : function ( filePath, file, i, length ) {
                var newPath = filePath.replace('/frontend/src', '');
                console.log('inject script = '+ newPath);
                return '<script src="/static/' + newPath  + '"></script>';
            }

执行后,控制台中没有任何内容(标准 gulp 输出除外),结果文件中出现未转换的文件路径。看起来我的自定义转换没有 运行,默认转换起作用了。

即使在多个级别(/**/*.js 而不是 /*.js),以下内容对我也有效:

gulp.task('inject', function() {
    gulp.src('./test.html')
        .pipe(inject(
            gulp.src(['./Content/js/*.js'], {read: false }),
            {
                transform: function (filePath, file, i, length) {
                    var newPath = filePath.replace('/Content/js/', '');
                    console.log('inject script = '+ newPath);
                    return '<script src="/static/' + newPath  + '"></script>';
                }
            })
        )
        .pipe(gulp.dest('./'));
});

gulp-inject 插件的转换功能按预期工作。 gulp任务的配置如下-

gulp.src(path.normalize('./app/index.html'))
    .pipe(inject(
        gulp.src([path.normalize('./frontend/src/*.js')], {read: false}), {
            transform : function ( filePath, file, i, length ) {
               var newPath = filePath.replace(path.normalize('/frontend/src'), '');
               console.log('inject script = '+ newPath);
               return '<script src="/static' + newPath  + '"></script>';
            }
        }
    ))
    .pipe(gulp.dest('./build'));

为确保其正常工作 cross-platform(Windows、Linux),使用 path.normalize

查看示例代码 - https://github.com/pra85/gulp-inject-example