gulp Minify JS using gulp-minify


Example

First, Install gulp and gulp-minify to project directory locally

npm install --save-dev gulp gulp-minify

Then add following min-js task to your gulpfile.js

var gulp = require('gulp');
var minify = require('gulp-minify');

gulp.task('min-js', function() {
    return gulp.src('lib/*.js')
        .pipe(minify({
            ext: {
                min: '.min.js'
            },
            ignoreFiles: ['-min.js']
        }))
        .pipe(gulp.dest('lib'))
});

gulp.task('watch', function(){
  gulp.watch('lib/*.js', ['min-js']); 
  // Other watchers
});

gulp.task('default', ['min-js', 'watch']);

This task find all js files in lib directory, minfy it and save to lib directory with .min.js suffix. For example, after minify lib/app.js file will be created a lib/app.min.js file

Besides running as a dependency for the 'default' gulp task, this task can be run manually by typing the following command:

gulp min-js