Uglify throws Parse error

北战南征 提交于 2019-11-28 09:03:10
Dan O

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"));
});
Marcos Abreu

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.

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));
}); 

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!