gulpgulp-rename

gulp rename consider .min.js as the extension


In a gulp task, I want to include a javascript file and inserting version within the file name.

Basically, in my task, I want to copy :

I defined this task:

var gulp = require('gulp');
var rename = require('gulp-rename');
var bsInfo = require('./node_modules/bootstrap-sass/package.json');

...

gulp.task('assets:js', function () {
    return gulp.src(['./node_modules/bootstrap-sass/assets/javascripts/bootstrap.js', './node_modules/bootstrap-sass/assets/javascripts/bootstrap.min.js'])
        .pipe(rename(function (path) { path.basename += "." + bsInfo.version; }))
        .pipe(gulp.dest('./dist/js'));
});

However, the generated file names are:

How can I write my task to output the correct file name ?


Solution

  • Try this for your rename pipe:

    .pipe(rename(function (file) {
    
          if (file.basename.endsWith(".min")) {
            file.basename = file.basename.slice(0, -4) + "." + bsInfo.version + ".min";
          }
          else {
            file.basename += "." + bsInfo.version;
          }
       }))