1

When running the gulp default task, one of the build tasks does not complete. And it seems to depend on the order in which they are called. Here's the script.

var gulp = require('gulp');
var del = require('del');
var ts = require('gulp-typescript');
var concat = require('gulp-concat');

var tsProject = ts.createProject('tsconfig.json');

gulp.task('clean', function () {
    return del(['dist']);
});

gulp.task('buildCommon', function () {
    return gulp.src('src/common/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(concat('common.min.js'))
        .pipe(gulp.dest('dist/server'))
        .pipe(gulp.dest('dist/client'));
});

gulp.task('buildServer', function () {
    return gulp.src('src/server/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(concat('server.min.js'))
        .pipe(gulp.dest('dist/server'));
});

gulp.task('build', ['clean'], function () {
    gulp.start('buildCommon');
    gulp.start('buildServer');
});

Running results in the following.

c:\development\git\fsts>gulp build
[06:31:19] Using gulpfile c:\development\git\fsts\gulpfile.js
[06:31:19] Starting 'clean'...
[06:31:19] Finished 'clean' after 10 ms
[06:31:19] Starting 'build'...
[06:31:19] Starting 'buildCommon'...
[06:31:19] Starting 'buildServer'...
[06:31:19] Finished 'build' after 15 ms
[06:31:20] Finished 'buildServer' after 1.18 s

You should notice that buildCommon does not complete.

If I run, buildCommon or buildServer tasks individually, they work.

c:\development\git\fsts>gulp buildServer
[06:34:00] Using gulpfile c:\development\git\fsts\gulpfile.js
[06:34:00] Starting 'buildServer'...
[06:34:01] Finished 'buildServer' after 783 ms

c:\development\git\fsts>gulp buildCommon
[06:34:31] Using gulpfile c:\development\git\fsts\gulpfile.js
[06:34:31] Starting 'buildCommon'...
[06:34:31] Finished 'buildCommon' after 761 ms

Here's the other weird part. If I change the order of the start calls, then the buildServer doesn't complete.

gulp.task('build', ['clean'], function () {
    gulp.start('buildServer');
    gulp.start('buildCommon');
});

c:\development\git\fsts>gulp build
[06:39:20] Using gulpfile c:\development\git\fsts\gulpfile.js
[06:39:20] Starting 'clean'...
[06:39:20] Finished 'clean' after 9.86 ms
[06:39:20] Starting 'build'...
[06:39:20] Starting 'buildServer'...
[06:39:20] Starting 'buildCommon'...
[06:39:20] Finished 'build' after 16 ms
[06:39:21] Finished 'buildCommon' after 1.18 s

Any ideas why the two separate tasks with their own streams won't let one of them finish?

Update: tried to use merge-stream and a single task and it still has the same behavior.

Update! My next move was to try and merge the streams and just use one task but I am getting a similar result.

var gulp = require('gulp');
var del = require('del');
var ts = require('gulp-typescript');
var concat = require('gulp-concat');
var merge = require('merge-stream');

var tsProject = ts.createProject('tsconfig.json');

gulp.task('clean', function () {
    return del(['dist']);
});

gulp.task('build', ['clean'], function () {

    var serverStream = gulp.src('src/server/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(concat('server.js'))
        .pipe(gulp.dest('dist/server'));

    var commonStream = gulp.src('src/common/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(concat('common.js'))
        .pipe(gulp.dest('dist/server'))
        .pipe(gulp.dest('dist/client'));

    var merged = merge(commonStream, serverStream);
    return merged;
});

gulp.task('default', ['build']);

With the result looking like the build task doesn't complete.

c:\development\git\fsts>gulp
[06:58:00] Using gulpfile c:\development\git\fsts\gulpfile.js
[06:58:00] Starting 'clean'...
[06:58:00] Finished 'clean' after 9.9 ms
[06:58:00] Starting 'build'...
Delta Echo
  • 33
  • 1
  • 5
  • There are two things here that look like potential problems to me: you have two separate gulp.dest in the commonStream (not sure if that is illegal but I've never seen it before), and two streams writing to the same directory (dist/server). Maybe try a clientStream and a serverStream, and have them each write once to their own directory? – Jason Kennaly Jan 09 '16 at 13:24
  • The individual tasks work when called independently. It's not that. – Delta Echo Jan 09 '16 at 13:45
  • It's not what? That they are creating a problem when the two streams write to the same directory? How can you tell? – Jason Kennaly Jan 09 '16 at 13:50
  • Gulp allows you to pipe to two different destinations. If I run the task buildCommon independently, the file is written to both destinations. Also, if I run the build task calling buildCommon last, it correctly writes the file to both dest folders. http://stackoverflow.com/questions/21951497/how-to-save-a-stream-into-multiple-destinations-with-gulp-js – Delta Echo Jan 09 '16 at 13:54

1 Answers1

1

As per the link https://github.com/gulpjs/gulp/issues/426, gulp.start is not really recommended for use in build files and it can lead to complicated build files.

I would suggest run-sequence to sequence your tasks one after and I tested to see that it works fine. The problem you are facing is actually with the typescript compiler not able to run two instances in parallel, and having contentions on intermediate output files that it creates.

var gulp = require('gulp');
var del = require('del');
var ts = require('gulp-typescript');
var concat = require('gulp-concat');
var runsequence = require('run-sequence');

var tsProject = ts.createProject('tsconfig.json');

gulp.task('clean', function () {
    return del(['dist']);
});

gulp.task('buildCommon', function () {
    return gulp.src('src/common/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(concat('common.min.js'))
        .pipe(gulp.dest('dist/server'))
        .pipe(gulp.dest('dist/client'));
});

gulp.task('buildServer', function () {
    return gulp.src('src/server/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(concat('server.min.js'))
        .pipe(gulp.dest('dist/server'));
});

gulp.task('build', ['clean'], function (done) {
     runsequence('buildCommon', 'buildServer', done);    
});