繁体   English   中英

通过忽略 gulp 中的文件扩展名对文件进行分组

[英]Grouping files by ignoring file extension in gulp

假设有以下文件列表

file1.csv
file2.csv
file2.js

标准的 gulp 任务可能如下所示

gulp.task( 'exampleTask', function() {
  return gulp.src([ '*.json', '*.csv', '*.js' ])
    .pipe( gulpModule() )
    .pipe( gulp.dest( '/dest/' ) );
});

使用该设置,三个文件中的每一个都将通过gulpModule()管道传输。

但是,我的要求略有不同:如果文件扩展名不是.js并且存在相同的文件名,但只有.js扩展名,则不要通过管道传输当前文件。

所以在上面的例子中,只有以下文件会通过gulpModule()管道传输:

file1.csv
file2.js

知道如何实现这一目标吗?

您应该可以使用一个简单的插件来做到这一点,如下所示。

  1. 在插件中,检查您的文件名并运行您的业务规则。
  2. 如果文件满足参数,则与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.

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