I am developing a Gulpfile
. Can it be made to restart as soon as it changes? I am developing it in CoffeeScript. Can Gulp
watch Gulpfile.coffee
in order to restart when changes are saved?
Gulpfile explained A gulpfile is a file in your project directory titled gulpfile. js (or capitalized as Gulpfile. js , like Makefile), that automatically loads when you run the gulp command.
As mentioned in my earlier post Gulp js interview questions, Gulp tasks are asynchronous. Which means that we are not sure about sequence of gulp tasks execution while chaining multiple tasks. And if you have used Grunt then it's a tricky situation to handle for you as Grunt tasks are synchronous by design.
Advertisements. The Watch method is used to monitor your source files. When any changes to the source file is made, the watch will run an appropriate task. You can use the 'default' task to watch for changes to HTML, CSS, and JavaScript files.
Run a Gulp Task in Visual Studio CodeType "Run Task" and select it, which will bring up a list of tasks configured in Gulp. Choose the Gulp Task you want to run! Most of your Gulp Tasks will probably be automated using gulp watch, but manual Gulp Tasks can easily be run within Visual Studio Code.
I created gulper that is gulp.js cli wrapper to restart gulp on gulpfile change.
You can simply replace gulp with gulper.
$ gulper <task-name>
You can create a task
that will gulp.watch
for gulpfile.js
and simply spawn
another gulp child_process.
var gulp = require('gulp'), argv = require('yargs').argv, // for args parsing spawn = require('child_process').spawn; gulp.task('log', function() { console.log('CSSs has been changed'); }); gulp.task('watching-task', function() { gulp.watch('*.css', ['log']); }); gulp.task('auto-reload', function() { var p; gulp.watch('gulpfile.js', spawnChildren); spawnChildren(); function spawnChildren(e) { // kill previous spawned process if(p) { p.kill(); } // `spawn` a child `gulp` process linked to the parent `stdio` p = spawn('gulp', [argv.task], {stdio: 'inherit'}); } });
I used yargs
in order to accept the 'main task' to run once we need to restart. So in order to run this, you would call:
gulp auto-reload --task watching-task
And to test, call either touch gulpfile.js
or touch a.css
to see the logs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With