跳过 gulp 任务中的二进制文件



我正在尝试使用gulp-if和gulp-is-binary来跳过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");
        });
});

这是我遇到问题的行:

.pipe(gulpif(isBinary(), function () { /* somehow skip? */ } ))

我不知道如何告诉 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");
        });
});

完整的gulpfile可以在这里找到:

https://github.com/JacobDB/new-site/blob/2d510e33863d25a99de4fe350bf9a181aefa3761/gulpfile.js

相关内容

  • 没有找到相关文章