[英]Grouping files by ignoring file extension in gulp
Assuming the following list of files假设有以下文件列表
file1.csv
file2.csv
file2.js
a standard gulp task may look like this标准的 gulp 任务可能如下所示
gulp.task( 'exampleTask', function() {
return gulp.src([ '*.json', '*.csv', '*.js' ])
.pipe( gulpModule() )
.pipe( gulp.dest( '/dest/' ) );
});
Using that setup each of the three files will get piped through gulpModule()
.使用该设置,三个文件中的每一个都将通过gulpModule()
管道传输。
My requirement, however, is slightly different: If the file extension is not .js
and the same filename exists just with a .js
extension, then do not pipe the current file along.但是,我的要求略有不同:如果文件扩展名不是.js
并且存在相同的文件名,但只有.js
扩展名,则不要通过管道传输当前文件。
So in the above example, only the following files would get piped through gulpModule()
:所以在上面的例子中,只有以下文件会通过gulpModule()
管道传输:
file1.csv
file2.js
Any idea how to achieve this?知道如何实现这一目标吗?
You should be able to do this with a simple plugin, as demonstrated below.您应该可以使用一个简单的插件来做到这一点,如下所示。
this.push()
如果文件满足参数,则与this.push()
一起传递var through = require('through2');
var path = require("path");
gulp.task( 'exampleTask', function() {
return gulp.src([ '*.json', '*.csv', '*.js' ])
.pipe(exclude())
.pipe( gulpModule() )
.pipe( gulp.dest( '/dest/' ) );
});
var exclude = function() {
return through.obj(function (file, enc, callback) {
var ext = path.extname(file.path),
filePath = file.path.substring(0,file.path.length-ext.length);
if (ext!=".js" && !fs.existsSync(filePath + ".js") )
{
//push it, otherwise ignore it.
this.push(file);
}
return callback();
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.