简体   繁体   English

Gulp.js任务,返回src?

[英]Gulp.js task, return on src?

I'm new to gulp and have been looking through example set-ups. 我是新手,并且一直在查看示例设置。 Some people have the following structure: 有些人有以下结构:

gulp.task("XXXX", function() {
    gulp.src("....

Other people have this: 其他人有这个:

gulp.task("XXXX", function() {
   return gulp.src("....

I'm wondering what difference the return on the src makes?? 我想知道src的回报有什么不同?

You return to indicate that the task is async. return以指示该任务是异步的。 gulp.src() returns a stream, so it's async. gulp.src()返回一个流,所以它是异步的。

Without it the task system wouldn't know when it finished. 没有它,任务系统就不会知道它什么时候结束。 Read the docs . 阅读文档

If you have dependent tasks you need to return the stream for the tasks to wait on their dependent tasks to complete before running themselves. 如果您有依赖任务,则需要返回任务流,以便在运行自己之前等待其相关任务完成。

eg 例如

// without return
gulp.task('task1', function() {
    gulp.src('src/coffee/*.coffee')
      /* eg compile coffeescript here */
     .pipe(gulp.dest('src'));
});

gulp.task('task2', ['task1'], function() {
    gulp.src('src/*.js')
      /* eg minfify js here */
     .pipe(gulp.dest('dest'));
});

in that example you'd expect task1 to complete ( eg compiling the coffeescript or whatever ) before task2 runs ... but unless we add return – like the example below – then they will run synchronously not asynchronously; 在这个例子,你会期望TASK1完成(如编译CoffeeScript的或其他)TASK2运行之前... ...但除非我们增加收益 -如下面的例子-那么他们将运行不同步异步; and the compiled coffeescript will not be minified because task2 will not have waited for task 1 to complete and so will not pick up on the compiled output of task1 . 和编译的CoffeeScript不会因为精缩TASK2将不会等待任务1完成,所以不会对TASK1的编译输出回暖。 So we should always return in these circumstances. 所以我们应该总是在这种情况下回归。

// with return
gulp.task('task1', function() {
    return gulp.src('**/*.coffee')
      /* your operations here */
     .pipe(gulp.dest('dest'));
});

gulp.task('task2', ['task1'], function() {
    return gulp.src('**/*.js')
      /* your operations here */
     .pipe(gulp.dest('dest'));
});

Edit: The recipe here explains it further. 编辑:这里的食谱进一步解释了它。 https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-tasks-in-series.md https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-tasks-in-series.md

I found this helpful, if you have multiple streams per task. 如果每个任务有多个流,我发现这很有用。 You need to combine/merge the multiple streams and return them. 您需要组合/合并多个流并返回它们。

var gulp = require('gulp');
var merge = require('gulp-merge');

gulp.task('test', function() {
    var bootstrap = gulp.src('bootstrap/js/*.js')
        .pipe(gulp.dest('public/bootstrap'));

    var jquery = gulp.src('jquery.cookie/jquery.cookie.js')
        .pipe(gulp.dest('public/jquery'));

    return merge(bootstrap, jquery);
});

The alternative, using Gulps task definition structure, would be: 使用Gulps任务定义结构的替代方案是:

var gulp = require('gulp');

gulp.task('bootstrap', function() {
    return gulp.src('bootstrap/js/*.js')
        .pipe(gulp.dest('public/bootstrap'));
});

gulp.task('jquery', function() {
    return gulp.src('jquery.cookie/jquery.cookie.js')
        .pipe(gulp.dest('public/jquery'));
});

gulp.task('test', ['bootstrap', 'jquery']);

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

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