GulpUglifyError:无法使用Browserify缩小JavaScript错误

时间:2017-04-12 08:08:36

标签: gulp browserify gulp-uglify

我正在将我的脚本与Browserify + Babel捆绑在一起,如下所示:

function buildJs() {
    let bopts = {
        paths: [
            `${SRC}/js`,
            './config'
        ],
        debug: !isProduction
    };
    let opts = Object.assign({}, watchify.args, bopts);
    let b = watchify(persistify(opts));

    b.add(`${SRC}/js/index.js`)
    .on('update', bundle)
    .on('log', gutil.log)
    .external(vendors)
    .transform(babelify, { 
        presets: ["es2015", "react"],
        plugins: [
            "syntax-async-functions",
            "transform-regenerator",
            "transform-class-properties",
            "transform-decorators-legacy",
            "transform-object-rest-spread",
            "transform-react-jsx-source",
            staticFs
        ]
    })
    .transform(browserifyCss, { global: true });

    function bundle() {
        let stream = b.bundle()
        .on('error', swallowError)
        .on('end', () => {
            gutil.log(`Building JS:bundle done.`);
        })
        .pipe(source('bundle.js'))
        .pipe(streamify(uglify()));

        return stream.pipe(gulp.dest(`${DIST}/js`));
    }

    return bundle();
}

这只是浏览器 - > babelify - > browserify-css - > uglify - > gulp.dest。 但如果我执行此任务,它将失败:

[17:00:30] Using gulpfile ~/ctc-web/gulpfile.js
[17:00:30] Starting 'build'...
[17:00:46] 1368516 bytes written (16.43 seconds)
[17:00:46] Building JS:bundle done.

events.js:160
      throw er; // Unhandled 'error' event
      ^
GulpUglifyError: unable to minify JavaScript
    at createError (/home/devbfex/ctc-web/node_modules/gulp-uglify/lib/create-error.js:6:14)
    at wrapper (/home/devbfex/ctc-web/node_modules/lodash/_createHybrid.js:87:15)
    at trycatch (/home/devbfex/ctc-web/node_modules/gulp-uglify/minifier.js:26:12)
    at DestroyableTransform.minify [as _transform] (/home/devbfex/ctc-web/node_modules/gulp-uglify/minifier.js:79:19)
    at DestroyableTransform.Transform._read (/home/devbfex/ctc-web/node_modules/readable-stream/lib/_stream_transform.js:159:10)
    at DestroyableTransform.Transform._write (/home/devbfex/ctc-web/node_modules/readable-stream/lib/_stream_transform.js:147:83)
    at doWrite (/home/devbfex/ctc-web/node_modules/readable-stream/lib/_stream_writable.js:338:64)
    at writeOrBuffer (/home/devbfex/ctc-web/node_modules/readable-stream/lib/_stream_writable.js:327:5)
    at DestroyableTransform.Writable.write (/home/devbfex/ctc-web/node_modules/readable-stream/lib/_stream_writable.js:264:11)
    at Transform.ondata (_stream_readable.js:555:20)

只是跳过uglify的作品,但我确实需要它。

奇怪的是,在结束事件之后发生了错误。我尝试使用乙烯基缓冲液,但同样的错误发生。

我找不到任何解决方案,每次我的尝试都会失败并显示相同的错误消息。

我错过了什么?有没有我错过的东西?

1 个答案:

答案 0 :(得分:0)

尝试将let替换为var,看看会发生什么。