Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Gulp be restarted upon each Gulpfile change?

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?

like image 485
coool Avatar asked Apr 05 '14 20:04

coool


People also ask

What is gulp Gulpfile?

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.

Is gulp synchronous?

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.

What is gulp watch command?

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.

How do I run Gulpfile in Visual Studio?

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.


2 Answers

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> 
like image 31
anatoo Avatar answered Sep 22 '22 08:09

anatoo


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.

like image 154
Caio Cunha Avatar answered Sep 23 '22 08:09

Caio Cunha