简体   繁体   中英

Gulp task apply uglify if file in pipe is javascript

I want to check if the file in the pipe is .js or not (it could be .map , .html , ...). And if so, uglifying it before copying it in the correct path.

ʕ •́؈•̀) I've try something like this (which not working):

gulpfile.js

gulp.src(current + '/**/*', {base: current})
.pipe($.tap(function (file) {
    if (path.extname(file.path) === '.js') {
        return gulp.src(file.path)
        .pipe($.uglify());
    }
}))
.pipe(gulp.dest(destination + '/' + name));

But for now, the uglify seems to do nothing...

Is anyone have a clue on how to do this ? (╥﹏╥)

If you're open to using plugins there is one called gulp-filter that does what you're asking for. https://www.npmjs.com/package/gulp-filter

It would probably look something like this

var gulp = require('gulp');
var gulpFilter = require('gulp-filter');

gulp.task('default', function () {
    // create filter instance inside task function
    var jsfilter = gulpFilter('**/*.js', {restore: true});
    return gulp.src(current + '/**/*', {base: current})
        // filter a subset of the files 
        .pipe(jsFilter)
        // run them through a plugin 
        .pipe($.uglify())
        // bring back the previously filtered out files (optional) 
        .pipe(jsFilter.restore)
        .pipe(gulp.dest(destination + '/' + name));
});

try using gulp-filter

something like

var filter = require('gulp-filter');
var jsFilter = filter('**/*.js');

gulp.src('*/*')
.pipe(jsFilter)
.pipe(uglify)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM