Gulp注入转换不起作用 [英] Gulp-inject transform doesn't work

查看:123
本文介绍了Gulp注入转换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've tried to setup gulp-inject to inject dependencies into index.html. Everything works fine except transform function. I need to replace part of filepath in the following way: /frontend/src/ --> /static/ I've tried to do it like this (copy-pasted from somewhere):

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

执行完后,控制台中没有任何东西(标准gulp输出除外)并在结果文件中出现未转换的文件路径。看起来像我的自定义转换只是不运行,而默认的转换工作。

After executing, I have nothing (except standard gulp output) in the console, and un-transformed filepath appears in result file. Looks like my custom transform just doesn't run, and the default transform works instead.

推荐答案

以下工作对我来说甚至( / ** / *。js 而不是 / *。js ):

The following is working for me even with multiple levels (/**/*.js instead of /*.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注入转换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆