简体   繁体   中英

Nodemon restart run gulp tasks

I have the following code in my gulpfile

gulp.task('scripts', function () {
    gulp.src(paths.browserify)
        .pipe(browserify())
        .pipe(gulp.dest('./build/js'))
        .pipe(refresh(server));
});

gulp.task('lint', function () {
    gulp.src(paths.js)
        .pipe(jshint())
        .pipe(jshint.reporter(stylish));
});

gulp.task('nodemon', function () {
    nodemon({
        script: 'app.js'
    });
});

I need to run the scripts and lint tasks when Nodemon restarts.I have the following

gulp.task('nodemon', function () {
    nodemon({
        script: 'app.js'
    }).on('restart', function () {
        gulp.run(['scripts', 'lint']);
    });
});

Gulp.run() is now deprecated, so how would I achieve the above using gulp and best practices?

gulp-nodemon documentation states that you can do it directly, passing an array of tasks to execute:

nodemon({script: 'app.js'}).on('restart', ['scripts', 'lint']);

See doc here

UPDATE, as the author of gulp-nodemon uses run as well:

Idea #1, use functions:

var browserifier = function () {
  gulp.src(paths.browserify)
    .pipe(browserify())
    .pipe(gulp.dest('./build/js'))
    .pipe(refresh(server));
});

gulp.task('scripts', browserifier);

var linter = function () {
  gulp.src(paths.js)
    .pipe(jshint())
    .pipe(jshint.reporter(stylish));
});

gulp.task('lint', linter);

nodemon({script: 'app.js'}).on('restart', function(){
  linter();
  browserifier();
});

If you can, use Mangled Deutz's suggestion about using functions. That's the best, most guaranteed way to make sure this works now and going forward.

However, functions won't help if you need to run dependent tasks or a series of tasks. I wrote run-sequence to fix this. It doesn't rely on gulp.run , and it's able to run a bunch of tasks in order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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