Gulp-inject变换不起作用

时间:2015-12-27 16:16:14

标签: javascript gulp gulp-inject

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

/static/

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

2 个答案:

答案 0 :(得分:6)

即使有多个级别(/**/*.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('./'));
});

答案 1 :(得分:1)

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

为了确保它跨平台工作(Windows,Linux),使用path.normalize

检查 - https://github.com/pra85/gulp-inject-example

上的示例代码