How can I get error notifications while scrolling on a gulp browser?

I use a browser , so I can use npm modules in my interface and gulp to complete my build tasks. This works great:

var browserify = require('gulp-browserify'); gulp.task('js', ['clean'], function() { gulp .src('./public/js/src/index.js') .pipe(browserify({ insertGlobals : true, debug : ! gulp.env.production })) .pipe(gulp.dest('./public/js/dist')) }); 

However, if there is a syntax error in my JS, I would like to receive an error notification through an OS X notification. I saw this similar question and changed my code to add .on('error'...) after .browserify() :

 // Browserify/bundle the JS. gulp .src('./public/js/src/index.js') .pipe(browserify({ insertGlobals : true, debug : ! gulp.env.production }).on('error', function(err){ notify.onError({ message: "Error: <%= error.message %>", title: "Failed running browserify" } this.emit('end'); }) .pipe(gulp.dest('./public/js/dist')) 

However, this does not report a violation of my JS. Adding console.log () inside on('error',...) also not logged. I suspect because this question is not related to using the gulp pipeline.

How can I get error notifications while scrolling to gulp browning?

+5
source share
3 answers

Use gulp -notify . Note that you should also use the browserify module instead of the legacy gulp -browerify .

 var browserify = require('browserify'), brfs = require('brfs'), watchify = require('watchify'), notify = require("gulp-notify"); var bundler = watchify(browserify({ basedir: "./public/js/src" })); // Browserify our code gulp.task('js', ['clean'], function() { // Browserify/bundle the JS. return bundler.bundle() // log errors if they happen .on('error', function(err) { return notify().write(err); }) .pipe(source('index.js')) .pipe(gulp.dest('public/js/dist')); }); 

Here is an example of using browserify util in a gulp task.

Here is another example of using browserify and showing a notification when js has syntax errors.

+6
source

In my gulpfile, I just listen for uncaught exceptions and notify using node-notifier :

 var Notifier = require('node-notifier'); notifier = new Notifier(); process.on('uncaughtException', function(err) { notifier.notify({title: 'error', message: 'gulp process has crashed'}); setTimeout(function() { console.error(err); process.exit(1); }, 1000); }); 

I found that the notification required a setTimeout delay before exiting the process.

+3
source

I use gulp -plumber for all my error:

 gulp.task('browserify', function(){ return browserify('./assets/js/src/app.js') .pipe(plugins.plumber()) .pipe(gulp.dest('./assets/js')) .pipe(plugins.notify({message: "browserify complete"})) }); 

https://www.npmjs.com/package/gulp-plumber

You may also need to use gulp -streamify. I don't think browsers support streams out of the box. It's been a while since I tried the browser using gulp, so I'm not sure if my piece of code will work out of the box, sorry for that.

https://github.com/nfroidure/gulp-streamify

0
source

Source: https://habr.com/ru/post/1211535/


All Articles