1
votes

I am trying to set up a serve task that will do the following:

  1. Call a watch task to watch for any source changes. ( the watch task calls a build task that builds the app into a "build" folder )
  2. Start the app using Nodemon ( gulp-nodemon )
  3. If a source change happens - rebuild the app and restart nodemon

So far I've written the following tasks:

gulp.task('build', gulp.series(['lint'], () => {
    del.sync(['./build/**/*.*']);
    const tsCompile = gulp.src('./src/**/*.ts')
        .pipe(gulpSourcemaps.init())
        .pipe(project());

    return tsCompile.js.pipe(gulpSourcemaps.write({
        sourceRoot: file => path.relative(path.join(file.cwd, file.path), file.base)
    }))
    .pipe(gulp.dest('./build/'));
}));

gulp.task('watch', gulp.series(['build'], () => {
    gulp.watch('./src/**/*.ts', gulp.series(['build']));
}));

gulp.task('serve', gulp.series(['watch'], () => {
    return gulpNodemon({
        script: './build/index.js',
        watch: './build/'
    });
}));

The current behavior of the tasks is:

  1. I start the application by typing gulp serve
  2. The watch tasks is called and it works as expected
  3. Nodemon does not start and the process is stuck at the watch task. If I change any of the source code, the watch task will be called.

Basically, Nodemon does not start and only the watch task is working.

I am not able to figure out why the following behavior happens and I want to ask if anyone knows what the problem could be?

1

1 Answers

1
votes

Use are running your watch task in series.

However, the watch task does not end so nodemon never starts.

Try using gulp.parallel() instead of gulp.series():

gulp.task('serve', gulp.parallel('watch', () => {
  return gulpNodemon({
    script: './build/index.js',
    watch: './build/'
  });
}));

Hopefully this should solve your problem.