gulp-uglify: Uglify throws Parse error

this is the 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'));
});
```js

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

Following this helped:

  1. Install Gulp-util
  2. Import the Gulp-util by using the following statement: var gutil = require('gulp-util');
  3. Finally, when you are uglifying the code, attach the error handler like this: .pipe(uglify().on('error', gutil.log))

I was able to debug it. It was a syntax error in one of the minified files I was including.

In my case (probably others too), this was due to me accidentally writing es6 on an old project that still uses gulp šŸ˜… .

@eqbalsajadi this is exactly the issue you were facing, template literals are not natively supported.

If you wish to use features such as that I’d suggest you use babelify to transform js before uglifying. My fix was to add the babilify transform (https://github.com/babel/babelify)

  var appBundler = browserify({
        entries: [options.src], // Only need initial file, browserify finds the rest
        transform: [
            [reactify, {}], [babelify, { presets: ["es2015", "react"] }],
        ],
        debug: options.development,
        cache: {},
        packageCache: {},
        fullPaths: options.development && !options.hot,
        plugin: options.development && options.hot ? [lrload] : [],
    });

@sunnee5150 you are also running concat before uglify which is providing to be a disadvantage for you in this instance, since now your syntax error is in your concatenated file making it harder to debug (this is also a problem w sourcemaps, since the sourcemap will link to the concat version, but this is another point).

try and run uglify before concatenating and the error message will lead to you to the un-concatenated file and line number, and i think it’ll make it easier for you.

for example:

gulp.src(src.js)
        .pipe(uglify().on('error', gutil.log))
        .pipe(concat(name.js))
        .pipe(gulp.dest(dest.js));

@terinjokes ah thank you, i will look into it!

i figure out that uglify parser do not recognize character (`) that is useful for multiline html inline code https://github.com/terinjokes/gulp-uglify/issues/155

You can use gulp.dest multiple times, so you can save the intermediate ā€œapp.min.jsā€ somewhere. In the following example, I save it to the ā€œtmp/distā€ folder.

gulp.task('bundle-app', ['templates'], function() {
        return gulp.src(['src/app.js', 'src/**/*.js'])
            .pipe(sourcemaps.init())
                .pipe(concat('app.min.js'))
                .pipe(gulp.dest('./tmp/dist')) // output an intermediate copy of app.min.js
                .pipe(ignore.exclude(['**/*.map']))
                .pipe(uglify().on('error', gulpUtil.log))
            .pipe(sourcemaps.write('./'))
            .pipe(gulp.dest('./public/dist'));
    });