Search code examples
javascriptgulpunminify

How can I uglyfy selected JS?


I'm using generator-gulp-webapp, but I want to just selected Javascript files that build in minified form. How can I edit gulpfile.js ? Here is the html task. I think I should edit this task.

gulp.task('html', ['views','styles'], function () {
  var assets = $.useref.assets({searchPath: ['.tmp', 'app', '.']});

  return gulp.src(['app/*.html', '.tmp/*.html'])
   .pipe(assets)
   .pipe($.if('*.js', $.uglify()))
   .pipe($.if('*.css', $.csso()))
   .pipe(assets.restore())
   .pipe($.useref())
   .pipe(gulp.dest('dist'));
});

Solution

  • if you only want to uglify js files here is a simple task :

    var gulp = require('gulp');
    var concat = require('gulp-concat');
    var uglify = require('gulp-uglify');
    
    gulp.task('js', function() {
        return gulp.src('app/js/*.js') //get all js
          pipe(concat('main.js')) //concat all js in a single file
          pipe(uglify()) //minify js
          pipe(gulp.dest('dist')); //output js in dist folder
    
    gulp.task('watch', function() {
        gulp.watch('app/js/*', ['js']);
    });