Search code examples
gulpgulp-uglify

gulp-uglify cannot change the name


I'm using gulp-uglify to minify my code, and there's a thing I would like to change .

I have the next gulpfile.js . When I do gulp calendar-uglify I got the compile inside /compile/ directory and with the name calendar.min.js. If I change the name inside uglify and I run again the command, gulp generates the name before again.

I mean, it seems that uglify cannot compile the file with the name I wrote. Gulp-uglify allways takes the filename of the concat file.

What can I do to change that?

TY,

Jaster

gulpfile.js

// Dependencies
var   gulp      = require('gulp'),
      concat    = require('gulp-concat'),
      uglify    = require('gulp-uglify');

// Private callbacks

/**
 * Calendar js concat
 * @private
 **/
var _calendarConcat = function(){
  return gulp.src(['./src/olympics/Calendar.js', './src/entry/calendar.js'])
    .pipe(concat('calendar.min.js'))
    .pipe(gulp.dest('./dist'));
};

/**
 * Calendar js uglify
 * @private
 **/
var _calendarUglify = function(){
  return gulp.src('./dist/calendar.min.js')
    .pipe(uglify('calendar.min.js', {
      mangle: true,
      output: {
        beautify: true
      }
    }))
    .pipe(gulp.dest('./compile/'));
};

// Task def

gulp.task('calendar-concat', _calendarConcat);
gulp.task('calendar-uglify', _calendarUglify);

Solution

  • The gulp-uglify documentation doesn't explicitly state the method signature for uglify() anywhere, but you can look at the source code to figure it out. It's essentially this:

    uglify([opts, [uglify]])
    

    Where

    So there's no way specify a different file name using uglify().

    There's no need for that anyway. Gulp plugins are supposed to do one thing and one thing well. That way you can combine them into more complex pipelines.

    In your case you're looking for the gulp-rename plugin which let's you rename a file:

    var rename = require('gulp-rename');
    
    var _calendarUglify = function(){
      return gulp.src('./dist/calendar.min.js')
        .pipe(uglify({
          mangle: true,
          output: {
            beautify: true
          }
        }))
        // renames the file to calendar2.min.js
        .pipe(rename({basename:'calendar2.min'})) 
        .pipe(gulp.dest('./compile/'));
    };