[英]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
),以下内容对我/*.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'));
为了确保它跨平台工作(Windows,Linux),使用path.normalize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.