简体   繁体   English

如何在gulp中运行任务之前运行函数?

[英]How to run function before run task in gulp?

guys how to run another function in task before run main task? 伙计们如何在运行主任务之前在任务中运行另一个功能? AS u can see in function have stream, and i know about syntax (gulp.task('task', ['tast'], func... ) But i dont want to see 999 tasks in list, thats why im trying to use functions for tasks 正如你可以在函数中看到的流一样,并且我知道语法(gulp.task('task',['tast'],func ...)但我不想在列表中看到999个任务,这就是为什么我试图使用函数执行任务

function cssBuild() {
    var dt = gulp
        .src('app/html-dev/styl/framework/style.styl')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(stylus({ use: nib(), 'include css': true, import: ['nib'], compress: false }))
        .pipe(gulp.dest('app/cache/css'))
        .pipe(browserSync.stream());
    return dt;
}


function htmlBuild() {
    var dt = gulp
        .src('app/html-dev/**/*.pug')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(pug({
            pretty: true
        }))
        .pipe(gulp.dest('app/cache'))
        .pipe(browserSync.stream());
    return dt;
}   

 gulp.task('build', function() {
        var removeDist = del.sync('app/dist');

        cssBuild();
        htmlBuild();
        jsBuild();


        return merge (

            gulp
                .src('app/cache/css/*.css')
                .pipe(cssnano())
                .pipe(gulp.dest('app/dist/css')),

            gulp
                .src(['app/html-dev/img/**/*', '!app/html-dev/img/empty.jpg'])
                .pipe(gulp.dest('app/dist/img')),
            gulp
                .src(["app/html-dev/fonts/**/*", '!app/html-dev/fonts/empty.woff'])
                .pipe(gulp.dest('app/dist/fonts')),

            gulp
                .src("app/cache/js/**/*")
                .pipe(gulp.dest('app/dist/js')),

            gulp
                .src("app/cache/*.html")
                .pipe(gulp.dest('app/dist'))
        );

    });

The problem you are probably running into is that the tasks aren't necessarily synchronous, so you need to treat them appropriately (ie, wait for them to complete). 您可能遇到的问题是这些任务不一定是同步的,因此您需要适当地对待它们(即,等待它们完成)。

It's probably better to use gulp as it is intended and to have several separate tasks (or ditch gulp entirely and just script something normally). 最好使用gulp并有几个单独的任务(或完全放弃gulp并正常编写某些内容)。 Trying to half use gulp is only going to cause you headaches. 尝试半gulp只会让您头痛。

If you are worried about having one massive gulp task list, what you can do instead is group tasks together and then call them. 如果你担心有一个巨大的gulp任务列表,你可以做什么,而不是为工作组一起,然后给他们打电话。

For example: 例如:

gulp.task('html', () => { /* do something */ });
gulp.task('css', () => { /* do something */ });
gulp.task('js', () => { /* do something */ });

gulp.task('clean', () => { /* do something */ });

// Group the build ones into one call
gulp.task('build', ['html', 'css', 'js']);

// Group the default into one
gulp.task('default', ['clean', 'build']);

By grouping them together, you can keep them nice and organized without having to have 100 things in one task. 通过将它们分组在一起,可以使它们保持良好的组织性,而不必在一项任务中包含100件事。 This also lets the benefits of gulp show. 这也让我们大吃一惊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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