我想通过以下方式过滤 gulp.pipe() 中的 .pug 文件:
如果文件名包含特定文本(例如“部分”),则将忽略该文本,否则将进一步通过管道传送并保存。我正在使用 gulp-ignore 插件。 忽略关键字存储在数组中:
var ignoreArr = [
"index",
"base",
"elem",
"temp",
"footer",
"header",
"layout",
"section"
];
我使用其他地方找到的解决方案来获取管道中的文件名
var es, log, logFile;
es = require('event-stream');
log = require('gulp-util').log;
logFile = function(es) {
return es.map(function(file, cb) {
log(file.path);
return cb();
});
};
我在将其与 gulp 任务集成时遇到问题
gulp.task("gen", function() {
return gulp.src('./src/*.pug')
.pipe(gulpIgnore.exclude(new RegExp(ignoreArr.join("|")).test(logFile(es)) == false))
.pipe(
pug({
locals: {
message: 'test',
}
})
)
.pipe(gulp.dest('./dist/'));
});
最佳答案
这是另一个更简单的解决方案:
const gulp = require("gulp");
const filter = require('gulp-filter');
const path = require('path');
var ignoreArr = [
"index",
"base",
"elem",
"temp",
"footer",
"header",
"layout",
"section"
];
gulp.task('default', function () {
// return true if want the file in the stream
const myFilter = filter(function (file) {
let baseName = path.basename(file.path, '.pug');
// is the test function (!String.includes) satisfied for every element of the ignoreArr
return ignoreArr.every(ignoreEntry => !baseName.includes(ignoreEntry));
});
return gulp.src(['./src/*.pug'])
.pipe(myFilter)
// .pipe(pug())
.pipe(gulp.dest('dist'));
});
关于javascript - 如何根据包含特定文本的文件名过滤 gulp.pipe() 中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49560198/