22

When i use gulp-uglify with browserify i get a error

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error
    at new JS_Parse_Error (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:189:18)
    at js_error (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:197:11)
    at croak (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:656:9)
    at token_error (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:664:9)
    at expect_token (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:677:9)
    at expect (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:680:36)
    at /home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1222:13
    at /home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:703:24
    at expr_atom (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1152:35)
    at maybe_unary (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1327:19)
    at expr_ops (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1362:24)
    at maybe_conditional (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1367:20)
    at maybe_assign (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1391:20)
    at expression (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1410:20)

this is my scripts task

gulp.task('scripts', function() {

    var bundler = browserify({
        entries: ['./src/scripts/main.js'],
        debug: debug
    }).transform(stringify()); // the error persist even without this transformation

    bundler
        .bundle()
        .on('error', handleErrors)
        .pipe(source(getBundleName() + '.js'))
        .pipe(jshint())
        .pipe(jshint.reporter('default', { verbose: true }))
        .pipe(jshint.reporter('fail'))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(uglify())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./web/js'));
});
rkmax
  • 17,633
  • 23
  • 91
  • 176
  • If you found a solution to your problem, please either A) Accept the answer that solved it (possibly adding a comment saying how), or B) Post your own answer showing how you solved it. (Which is absolutely fine on SO, provided it really is distinct from the existing answers. SO will let you accept your own answer after 48 hours.) – T.J. Crowder Mar 18 '16 at 13:07

4 Answers4

71

uglify will parse the script content before minifying it. I suspect that one of the browserify source maps are being included in the stream down to uglify. Anyway to find the problem you can use gulp-util's log method to handle uglify's exceptions. Example:

...
var gulpUtil = require('gulp-util');

gulp.task('scripts', function() {
    ...
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(uglify().on('error', gulpUtil.log)) // notice the error event here
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./web/js'));
});

If you still have problems fixing the issue, post the details after incorporating the error log.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Marcos Abreu
  • 2,832
  • 22
  • 18
  • 1
    not sure why it took me so long to find this... gulp-util helped me to pinpoint the error right away.. Thanks for posting this! – hanzolo Jan 30 '16 at 01:25
  • 5
    FWIW you can also do `.on('error', console.log)` probably not as concise as the above but I don't want another dependency, yet... – segFault Feb 16 '17 at 00:11
  • 2
    Unreal! Thanks! I had declared 2 variables using "let" ... I've been switching between Swift3 and JS :S – Chris Allinson Mar 07 '17 at 02:44
6

I encountered this same issue with gulp-concat-sourcemap and gulp-uglify. I solved it by ignoring map files with gulp-ignore:

gulp.task("uglify-src", function() {
    gulp.src([ "src/js/**/*.js" ])
    .pipe(concat("app.js"))
    .pipe(ignore.exclude([ "**/*.map" ]))
    .pipe(uglify())
    .pipe(gulp.dest("dist/js"));
});
Francesc Castells
  • 2,692
  • 21
  • 25
Dan O
  • 6,022
  • 2
  • 32
  • 50
0

It could be a simple error in your source JavaScript file. Try disabling uglify by commenting it out in your gulpfile and see if your browser console spots the real issue.

gulp.task('minified', function () {
    return gulp.src(paths.concatScripts)
        .pipe(sourcemaps.init())       
        //.pipe(uglify())
        .pipe(sourcemaps.write('.', { addComment: false }))        
        .pipe(gulp.dest(publishUrl));
}); 
James Lawruk
  • 30,112
  • 19
  • 130
  • 137
0

Errors are not propagated by Node.js pipe. This article mentions @Marcos Abreu's uglify().on approach in addition to describing the use of pump instead of pipe.

https://github.com/terinjokes/gulp-uglify/blob/master/docs/why-use-pump/README.md#why-use-pump

Ken Lin
  • 1,819
  • 21
  • 21