Enclose JS files content with a "try ... catch" statement.
It allows to easily catch errors on development.
Install gulp-trycatch-closure
using NPM, as a development dependency:
npm install --save-dev gulp-trycatch-closure
var gulp = require('gulp'),
concat = require('gulp-concat'),
trycatch = require('gulp-trycatch-closure');
gulp.task('build', function() {
// Get all JS files
return gulp.src(['src/js/*.js'])
// Enclose their content with a try ... catch statement
.pipe(trycatch())
// Concat
.pipe(concat('build.js'))
// Send to destination
.pipe(gulp.dest('dist/'));
});
var gulp = require('gulp'),
concat = require('gulp-concat'),
ifif = require('gulp-if'),
trycatch = require('gulp-trycatch-closure'),
// Check if we're in dev env
isDev = process.NODE_ENV !== 'production';
gulp.task('build', function() {
return gulp.src(['src/js/*.js'])
.pipe(ifif(isDev, trycatch()))
.pipe(concat('build.js'))
.pipe(gulp.dest('dist/'));
});
You can pass to the trycatch
function an object of options. Here are the available options with their default value:
logger
- Way to log the error, default isconsole.warn(e);
try {
/* file content */
} catch (e) {
console.warn(e);
}