Uglify 抛出解析错误

Uglify throws Parse error

当我将 gulp-uglify 与 browserify 结合使用时,出现错误

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)

这是我的脚本任务

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

uglify 将在缩小之前解析脚本内容。我怀疑 browserify 源映射之一被包含在向下到 uglify 的流中。无论如何要找到问题你可以使用 gulp-util's log 方法来处理 uglify 的异常。示例:

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

如果您在解决问题时仍然遇到问题,post合并错误日志后的详细信息。

我在 gulp-concat-sourcemapgulp-uglify 中遇到了同样的问题。我通过忽略带有 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"));
});

这可能是源 JavaScript 文件中的一个简单错误。尝试通过在你的 gulpfile 中注释掉它来禁用 uglify,看看你的浏览器控制台是否发现了真正的问题。

gulp.task('minified', function () {
    return gulp.src(paths.concatScripts)
        .pipe(sourcemaps.init())       
        //.pipe(uglify())
        .pipe(sourcemaps.write('.', { addComment: false }))        
        .pipe(gulp.dest(publishUrl));
}); 

错误不会通过 Node.js 管道传播。除了描述使用 pump 而不是 pipe.

之外,本文还提到了@Marcos Abreu 的 uglify().on 方法

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