gulp-include与gulp-uglify不起作用

san*_*a-p 2 javascript gulp gulp-uglify

我有一个gulp.task 'scripts'使用gulp-uglify.然后我也补充gulp-import说它没用.如果我这样做只会uglify起作用.如果我这样做只会import起作用.但如果我同时做两件事都行不通.这是功能:

gulp.task('scripts', function(){
    gulp.src([
            'testgulp/**/*.js', '!testgulp/**/_*.js', '!testgulp/**/*.min.js'
        ])
        .pipe(include()) //if I comment only this line, uglify works
            .on('error', console.log)
        .pipe(uglify()) //if I comment only this line, include works
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest(function(file) { return file.base; }));

});
Run Code Online (Sandbox Code Playgroud)

然后在终端上我尝试这两个时都有这个响应:

/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/map-stream/index.js:103
        throw err
        ^
GulpUglifyError: unable to minify JavaScript
    at createError (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/lib/create-error.js:6:14)
    at wrapper (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/node_modules/lodash/_createHybrid.js:87:15)
    at trycatch (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/minifier.js:26:12)
    at DestroyableTransform.minify [as _transform] (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/minifier.js:76:19)
    at DestroyableTransform.Transform._read (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:159:10)
    at DestroyableTransform.Transform._write (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:147:83)
    at doWrite (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:313:64)
    at writeOrBuffer (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:302:5)
    at DestroyableTransform.Writable.write (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:241:11)
    at Stream.ondata (stream.js:31:26)
Run Code Online (Sandbox Code Playgroud)

我正在离开考拉应用程序,附加/前置功能非常棒,所以我想对Gulp做同样的事情.我尝试了gulp-include和gulp-import,两者都有同样的问题.我做错了什么?谢谢 :)

-EDIT- 我还尝试gulp -sync和硬编码minifyit任务的超时(),但不解决问题...

san*_*a-p 5

好的,我想我发现了问题.首先,我需要安装gulp-util来更好地找出错误是什么.

gulp.task('scripts', function(){
    gulp.src([
            'assets/scripts/**/*.js',
            '!assets/scripts/**/_*.js',
            '!assets/scripts/**/*.min.js'
        ])
        .pipe(include())
            .on('error', console.log)
        .pipe(uglify().on('error', gutil.log)) //gulp-util
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest(function(file) { return file.base; }));
});
Run Code Online (Sandbox Code Playgroud)

然后是ES5简写uglify的一个错误.我不得不安装gulp-babel,最后的代码:

gulp.task('scripts', function(){
    gulp.src([
            'assets/scripts/**/*.js',
            '!assets/scripts/**/_*.js',
            '!assets/scripts/**/*.min.js'
        ])
        .pipe(include())
            .on('error', console.log)
        .pipe(babel({
            presets: ['es2015'], //this fixed a shorthand javascript error
            compact: true
        }))
        .pipe(uglify().on('error', gutil.log)) // gulp-util
        .pipe(rename({ suffix: '.min' }))
});
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题.希望能帮助别人:)