Search code examples
javascriptnode.jsgulpgulp-uglify

Gulp-Uglify Parse Error when used with Reactify and Babelify


Here is how I'm doing this to uglify the JavaScript library:

var babelify = require("babelify");
var browserify = require('browserify');
var reactify = require('reactify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');

browserify(componentPath + "app.jsx")
    .transform(reactify)
    .transform(babelify)
    .bundle()
    .pipe(source('app.js'))
    .pipe(buffer())
    .pipe(uglify())
    .pipe(gulp.dest(buildPath));

This used to work, until I recently updated all my libraries to the latest (meaning I don't know if it is gulp-uglify that is the problem or reactify or even babelify. But this is what I get:

events.js:85
throw er; // Unhandled 'error' event
^
Error
at new JS_Parse_Error (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:1526:18)
    at js_error (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:1534:11)
        at croak (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2025:9)
            at token_error (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2033:9)
                at unexpected (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2039:9)
                    at expr_atom (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2534:13)
                        at maybe_unary (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2708:19)
                            at expr_ops (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2743:24)
                                at maybe_conditional (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2748:20)
                                    at maybe_assign (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2772:20)

I ran uglify().on('error', gulpUtil.log) and this is the output:

{ [Error: /Path/To/My/Application/app.js: Unexpected token: punc ())]
message: '/Path/To/My/Application/app.js: Unexpected token: punc ())',
        fileName: '/Path/To/My/Application/app.js',
        lineNumber: 41,
        stack: 'Error\n    at new JS_Parse_Error (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:1526:18)\n    at js_error (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:1534:11)\n    at croak (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2025:9)\n    at token_error (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2033:9)\n    at unexpected (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2039:9)\n    at expr_atom (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2534:13)\n    at maybe_unary (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2708:19)\n    at expr_ops (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2743:24)\n    at maybe_conditional (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2748:20)\n    at maybe_assign (eval at <anonymous> (/Path/To/My/Application/node_modules/gulp-uglify/node_modules/uglify-js/tools/node.js:24:4), <anonymous>:2772:20)',
        showStack: false,
        showProperties: true,
        plugin: 'gulp-uglify' }

Help is really appreciated


Solution

  • You have already made a very good start. According to the debug log, it seems comments in original code was not stripped expectedly. You should checkout /path/to/my/application/app.js at line 41, to see what is there.

    If there is a comment, I'd suggest you to check your remove comment plugin. In my case, the most recent gulp-strip-comments is the root cause. I switched it back to an old version, everything works fine again.