Node.js 在一个gulp任务中跳过二进制文件

Node.js 在一个gulp任务中跳过二进制文件,node.js,gulp,binaryfiles,gulp-if,Node.js,Gulp,Binaryfiles,Gulp If,我试图使用和来跳过HTML任务中的二进制文件,但是我遇到了很多麻烦 我有一个任务: // html task, converts includes & variables in HTML gulp.task("html", function () { "use strict"; // development HTML directory var htmlDirectory = dev; // production HTML directory (if

我试图使用和来跳过HTML任务中的二进制文件,但是我遇到了很多麻烦

我有一个任务:

// html task, converts includes & variables in HTML
gulp.task("html", function () {
    "use strict";

    // development HTML directory
    var htmlDirectory = dev;

    // production HTML directory (if --dist is passed)
    if (argv.dist) htmlDirectory = dist;

    // clean directory if --dist is passed
    if (argv.dist) del([htmlDirectory + "/**/*", "!" + htmlDirectory + "{/assets,/assets/**}"]);

    // process HTML
    return gulp.src([src + "/**/*", "!" + src + "{/assets,/assets/**}"])
        // prevent breaking on error
        .pipe(plumber({errorHandler: onError}))
        // check if source is newer than destination
        .pipe(gulpif(!argv.dist, newer({dest: htmlDirectory, extra: [src + "{/partials,/partials/**}"]})))
        // check if a file is a binary
        .pipe(gulpif(isBinary(), function () { /* somehow skip? */ } ))
        // replace variables
        .pipe(fileinclude({
            prefix: "@@",
            basepath: "@file",
            context: {
                name: name,
                description: description,
                version: version,
                repository: repository,
                license: license,
            }
        }))
        // replace FontAwesome placeholders
        .pipe(replace(/(?:<icon:)([A-Za-z0-9\-\_]+)[^>]*(?:>)/g, "<i class='fa fa-$1' aria-hidden='true'><\/i>"))
        // output to the compiled directory
        .pipe(gulp.dest(htmlDirectory))
        // reload the files
        .pipe(browserSync.reload({stream: true}))
        // notify that the task is complete, if not part of default or watch
        .pipe(gulpif(gulp.seq.indexOf("html") > gulp.seq.indexOf("default"), notify({title: "Success!", message: "HTML task complete!", onLast: true})))
        // push the task to the ranTasks array
        .on("data", function() {
            if (ranTasks.indexOf("html") < 0) ranTasks.push("html");
        });
});

我不知道如何告诉Gulp跳过该文件并继续其余的任务。我觉得我错过了一些简单的东西。

经过大量的研究、实验和gulp is binary开发人员的帮助,我发现了这一点。我的任务如下:

// html task, copies binaries, converts includes & variables in HTML
gulp.task("html", function () {
    "use strict";

    // development HTML directory
    var htmlDirectory = dev;

    // production HTML directory (if --dist is passed)
    if (argv.dist) htmlDirectory = dist;

    // clean directory if --dist is passed
    if (argv.dist) del([htmlDirectory + "/**/*", "!" + htmlDirectory + "{/assets,/assets/**}"]);

    // copy binaries
    var binaries = gulp.src([src + "/**/*", "!" + src + "{/assets,/assets/**}"])
        // prevent breaking on error
        .pipe(plumber({errorHandler: onError}))
        // check if source is newer than destination
        .pipe(gulpif(!argv.dist, newer({dest: htmlDirectory, extra: [src + "/**/*", "!" + src + "{/assets,/assets/**}"]})))
        // check if a file is a binary
        .pipe(isBinary())
        // skip the file if it's not a binary
        .pipe(through.obj(function(file, enc, next) {
            if (!file.isBinary()) {
                next();
                return;
            }

            next(null, file);
        }))
        // output to the compiled directory
        .pipe(gulp.dest(htmlDirectory));

    // process HTML
    var html = gulp.src([src + "/**/*", "!" + src + "{/assets,/assets/**}"])
        // prevent breaking on error
        .pipe(plumber({errorHandler: onError}))
        // check if source is newer than destination
        .pipe(gulpif(!argv.dist, newer({dest: htmlDirectory, extra: [src + "/**/*", "!" + src + "{/assets,/assets/**}"]})))
        // check if a file is a binary
        .pipe(isBinary())
        // skip the file if it's a binary
        .pipe(through.obj(function(file, enc, next) {
            if (file.isBinary()) {
                next();
                return;
            }

            next(null, file);
        }))
        // replace variables
        .pipe(fileinclude({
            prefix: "@@",
            basepath: "@file",
            context: {
                name: name,
                description: description,
                version: version,
                repository: repository,
                license: license,
            }
        }))
        // replace icon placeholders
        .pipe(replace(/(?:<icon:)([A-Za-z0-9\-\_][^:>]+)(?:\:([A-Za-z0-9\-\_\ ][^:>]*))?(?:>)/g, "<i class='icon'><svg class='icon_svg $2' aria-hidden='true'><use xlink:href='#$1' \/><\/svg></i>"))
        // output to the compiled directory
        .pipe(gulp.dest(htmlDirectory));

    // merge both steams back in to one
    return merge(binaries, html)
        // prevent breaking on error
        .pipe(plumber({errorHandler: onError}))
        // reload the files
        .pipe(browserSync.reload({stream: true}))
        // notify that the task is complete, if not part of default or watch
        .pipe(gulpif(gulp.seq.indexOf("html") > gulp.seq.indexOf("default"), notify({title: "Success!", message: "HTML task complete!", onLast: true})))
        // push the task to the ranTasks array
        .on("data", function() {
            if (ranTasks.indexOf("html") < 0) ranTasks.push("html");
        });
});
//html任务,复制二进制文件,转换html中的包含和变量
任务(“html”,函数(){
“严格使用”;
//开发HTML目录
var htmlDirectory=dev;
//生产HTML目录(如果传递了--dist)
如果(argv.dist)htmlDirectory=dist;
//如果传递了--dist,则清除目录
if(argv.dist)del([htmlDirectory+“/****”,“!”+htmlDirectory+“{/assets,/assets/***}]”);
//复制二进制文件
var binaries=gulp.src([src+“/****”,“!”+src+“{/assets,/assets/***}]”)
//防止因错误而中断
.管道(管道工({errorHandler:onError}))
//检查源是否比目标更新
.pipe(gulpif(!argv.dist,较新的({dest:htmlDirectory,extra:[src+“/****”,“!”+src+“{/assets,/assets/***}]})))
//检查文件是否为二进制文件
.pipe(isBinary())
//如果文件不是二进制文件,请跳过该文件
.pipe(通过.obj)(函数(文件、enc、下一个){
如果(!file.isBinary()){
next();
返回;
}
下一步(空,文件);
}))
//输出到已编译目录
.管道(吞咽目的地(htmlDirectory));
//处理HTML
var html=gulp.src([src+“/****”,“!”+src+“{/assets,/assets/***}]”)
//防止因错误而中断
.管道(管道工({errorHandler:onError}))
//检查源是否比目标更新
.pipe(gulpif(!argv.dist,较新的({dest:htmlDirectory,extra:[src+“/****”,“!”+src+“{/assets,/assets/***}]})))
//检查文件是否为二进制文件
.pipe(isBinary())
//如果是二进制文件,请跳过该文件
.pipe(通过.obj)(函数(文件、enc、下一个){
if(file.isBinary()){
next();
返回;
}
下一步(空,文件);
}))
//替换变量
.管道(文件包括({
前缀:“@@”,
基本路径:“@file”,
背景:{
姓名:姓名,,
描述:描述,
版本:版本,,
存储库:存储库,
许可证:许可证,
}
}))
//替换图标占位符
.管道(替换(/(?:)+)(?:\:([A-Za-z0-9\-\\\\\\][^:>]*))(?:>)/g,“”)
//输出到已编译目录
.管道(吞咽目的地(htmlDirectory));
//将两个蒸汽合并回一个
返回合并(二进制文件、html)
//防止因错误而中断
.管道(管道工({errorHandler:onError}))
//重新加载文件
.pipe(browserSync.reload({stream:true}))
//通知任务已完成(如果不是默认或监视的一部分)
.pipe(gulpif(gulp.seq.indexOf(“html”)>gulp.seq.indexOf(“默认”),通知({title:“Success!”,消息:“html任务完成!”,onLast:true}))
//将任务推送到任务阵列
.on(“数据”,函数(){
如果(ranTasks.indexOf(“html”)<0)ranTasks.push(“html”);
});
});
完整的gulpfile可在此处找到:

// html task, copies binaries, converts includes & variables in HTML
gulp.task("html", function () {
    "use strict";

    // development HTML directory
    var htmlDirectory = dev;

    // production HTML directory (if --dist is passed)
    if (argv.dist) htmlDirectory = dist;

    // clean directory if --dist is passed
    if (argv.dist) del([htmlDirectory + "/**/*", "!" + htmlDirectory + "{/assets,/assets/**}"]);

    // copy binaries
    var binaries = gulp.src([src + "/**/*", "!" + src + "{/assets,/assets/**}"])
        // prevent breaking on error
        .pipe(plumber({errorHandler: onError}))
        // check if source is newer than destination
        .pipe(gulpif(!argv.dist, newer({dest: htmlDirectory, extra: [src + "/**/*", "!" + src + "{/assets,/assets/**}"]})))
        // check if a file is a binary
        .pipe(isBinary())
        // skip the file if it's not a binary
        .pipe(through.obj(function(file, enc, next) {
            if (!file.isBinary()) {
                next();
                return;
            }

            next(null, file);
        }))
        // output to the compiled directory
        .pipe(gulp.dest(htmlDirectory));

    // process HTML
    var html = gulp.src([src + "/**/*", "!" + src + "{/assets,/assets/**}"])
        // prevent breaking on error
        .pipe(plumber({errorHandler: onError}))
        // check if source is newer than destination
        .pipe(gulpif(!argv.dist, newer({dest: htmlDirectory, extra: [src + "/**/*", "!" + src + "{/assets,/assets/**}"]})))
        // check if a file is a binary
        .pipe(isBinary())
        // skip the file if it's a binary
        .pipe(through.obj(function(file, enc, next) {
            if (file.isBinary()) {
                next();
                return;
            }

            next(null, file);
        }))
        // replace variables
        .pipe(fileinclude({
            prefix: "@@",
            basepath: "@file",
            context: {
                name: name,
                description: description,
                version: version,
                repository: repository,
                license: license,
            }
        }))
        // replace icon placeholders
        .pipe(replace(/(?:<icon:)([A-Za-z0-9\-\_][^:>]+)(?:\:([A-Za-z0-9\-\_\ ][^:>]*))?(?:>)/g, "<i class='icon'><svg class='icon_svg $2' aria-hidden='true'><use xlink:href='#$1' \/><\/svg></i>"))
        // output to the compiled directory
        .pipe(gulp.dest(htmlDirectory));

    // merge both steams back in to one
    return merge(binaries, html)
        // prevent breaking on error
        .pipe(plumber({errorHandler: onError}))
        // reload the files
        .pipe(browserSync.reload({stream: true}))
        // notify that the task is complete, if not part of default or watch
        .pipe(gulpif(gulp.seq.indexOf("html") > gulp.seq.indexOf("default"), notify({title: "Success!", message: "HTML task complete!", onLast: true})))
        // push the task to the ranTasks array
        .on("data", function() {
            if (ranTasks.indexOf("html") < 0) ranTasks.push("html");
        });
});