Search code examples
javascriptnode.jsgulpuglifyjsgulp-uglify

Getting example in gulp-uglify to work


I have never used gulp. I am trying out gulp-uglify example.

https://www.npmjs.com/package/gulp-uglify

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

gulp.task('compress', function() {
  return gulp.src('lib/*.js')
    .pipe(uglify())
    .pipe(gulp.dest('dist'));
});

The example does not work. Where is gulp.task declared? What does the code inside gulp.task do? A file dist will be created after compressing all .js files in lib folder? How can the code example be modified to work properly?


Solution

  • Working configuration

    Gulpfile:

    "use strict";
    
    var gulp = require('gulp');
    var uglify = require('gulp-uglify');
    
    gulp.task('scripts', function() {
        gulp.src('./lib/*.js')
            .pipe(uglify())
            .pipe(gulp.dest('./dist/'));
    });
    

    package.json (run npm install to verify that all dependencies correctly loaded):

    {
        "dependencies": {
            "gulp": "~3.9.0",
            "gulp-uglify": "~1.5.1"
        }
    }