简体   繁体   English

Gulp-inject变换不起作用

[英]Gulp-inject transform doesn't work

I've tried to setup gulp-inject to inject dependencies into index.html. 我试图设置gulp-inject将依赖注入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): 我需要以下列方式替换部分文件路径: /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>';
            }

After executing, I have nothing (except standard gulp output) in the console, and un-transformed filepath appears in result file. 执行后,我在控制台中没有任何内容(标准gulp输出除外),并且未转换的文件路径出现在结果文件中。 Looks like my custom transform just doesn't run, and the default transform works instead. 看起来我的自定义转换不会运行,而默认转换工作正常。

The following is working for me even with multiple levels ( /**/*.js instead of /*.js ): 即使有多个级别( /**/*.js而不是/*.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 plugin's transform function works as intended. gulp-inject插件的转换功能按预期工作。 The configuration of the gulp task is as follows - 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'));

To make sure it works cross-platform (Windows,Linux), path.normalize is used 为了确保它跨平台工作(Windows,Linux),使用path.normalize

Check the example code at - https://github.com/pra85/gulp-inject-example 请查看示例代码 - https://github.com/pra85/gulp-inject-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM