如何管理到Gulp的另一项任务?

sri*_*igi 19 gulp gulp-watch

我试着干我的gulpfile.在那里,我有一些我不熟悉的代码重复.如何做得更好?

gulp.task('scripts', function() {
  return gulp.src('src/scripts/**/*.coffee')
    .pipe(coffeelint())
    .pipe(coffeelint.reporter())
    .pipe(coffee())
    .pipe(gulp.dest('dist/scripts/'))
    .pipe(gulp.src('src/index.html'))  // this
    .pipe(includeSource())             // needs
    .pipe(gulp.dest('dist/'))          // DRY
});

gulp.task('index', function() {
  return gulp.src('src/index.html')
    .pipe(includeSource())
    .pipe(gulp.dest('dist/'))
});
Run Code Online (Sandbox Code Playgroud)

我得到了index一个单独的任务,因为我需要注意src/index.htmllivereload.但我也在关注我的.coffee消息来源,当他们改变时,我也需要更新src/index.html.

怎么管道index进去scripts

Ste*_*acy 25

gulp 使您能够根据参数订购一系列任务.

例:

gulp.task('second', ['first'], function() {
   // this occurs after 'first' finishes
});
Run Code Online (Sandbox Code Playgroud)

尝试以下代码,您将运行任务'index'来运行这两个任务:

gulp.task('scripts', function() {
  return gulp.src('src/scripts/**/*.coffee')
    .pipe(coffeelint())
    .pipe(coffeelint.reporter())
    .pipe(coffee())
    .pipe(gulp.dest('dist/scripts/'));
});

gulp.task('index', ['scripts'], function() {
  return gulp.src('src/index.html')
    .pipe(includeSource())
    .pipe(gulp.dest('dist/'))
});
Run Code Online (Sandbox Code Playgroud)

index现在,任务需要scripts在它运行代码内部之前完成.

  • 我更喜欢使用`run-sequence`来编排我的任务,所以一般来说这个解决方案是正确的 - 将重复的工作分成自己的gulp.task. (2认同)