20

gulpfile を DRY しようとしています。そこには、私が慣れていないコードの小さな重複があります。どうすればこれを改善できますか?

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/'))
});

indexライブリロードを監視する必要があるため、別のタスクとして取得しましたsrc/index.html。しかし、ソースも監視して.coffeeおり、ソースが変更された場合は、更新する必要src/index.htmlもあります。

indexにパイプするにはどうすればよいscriptsですか?

4

2 に答える 2

25

gulp引数に基づいて一連のタスクを順序付けることができます。

例:

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

次のコードを試してください。タスク '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/'))
});

タスクは、関数内のコードを実行する前に終了するindex必要があります。scripts

于 2014-05-06T21:11:18.753 に答える
3

Orchestrator のソース、特に.start()実装を調べると、最後のパラメーターが関数の場合、それがコールバックとして扱われることがわかります。

私は自分のタスクのためにこのスニペットを書きました:

  gulp.task( 'task1', () => console.log(a) )
  gulp.task( 'task2', () => console.log(a) )
  gulp.task( 'task3', () => console.log(a) )
  gulp.task( 'task4', () => console.log(a) )
  gulp.task( 'task5', () => console.log(a) )

  function runSequential( tasks ) {
    if( !tasks || tasks.length <= 0 ) return;

    const task = tasks[0];
    gulp.start( task, () => {
        console.log( `${task} finished` );
        runSequential( tasks.slice(1) );
    } );
  }
  gulp.task( "run-all", () => runSequential([ "task1", "task2", "task3", "task4", "task5" ));
于 2017-05-24T22:22:34.347 に答える