Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp-include with gulp-uglify doesn't work

I have a gulp.task 'scripts' that uses gulp-uglify. Then I also added gulp-import and it didn't work. If i do only uglify it works. If I do only import it works. But if i do both at the same time it doesn't work. Here is the function:

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

});

And then on terminal I have this response when try to do both:

/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)

I'm moving away from Koala App and the append/prepend feature was awesome, so i would like to do the same with Gulp. I tried gulp-include and gulp-import, both has the same problem. What i'm doing wrong? Thank you :)

-EDIT- i also tried gulp-sync and hardcode a timeout() on minifyit task, but doesn't fix the problem...

like image 399
sandrina-p Avatar asked Jun 04 '26 20:06

sandrina-p


2 Answers

Okay, I think I found the problem. First of all I needed to install gulp-util to find out better what was the error.

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

Then it was something with ES5 shorthand uglify that trows an error. I had to install gulp-babel as well, and the final code:

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

And that fixed my problem. Hope to help someone else :)

like image 133
sandrina-p Avatar answered Jun 07 '26 09:06

sandrina-p


You should use pump module.

   var pump = require('pump');

   gulp.task('scripts', function(){
       pump([
          gulp.src([...]),
          ugilfy(),
          rename({ suffix: '.min' }),
          gulp.dest(...)
       ]);
   });

And you can see why use pump

like image 38
huwence Avatar answered Jun 07 '26 10:06

huwence



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!