1

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 :

  • ./node_modules/bootstrap-sass/assets/javascripts/bootstrap.js to ./dist/js/bootstrap.3.3.7.js
  • ./node_modules/bootstrap-sass/assets/javascripts/bootstrap.min.js to ./dist/js/bootstrap.3.3.7.min.js

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:

  • bootstrap.3.3.7.js (correct)
  • bootstrap.min.3.3.7.js (incorrect, min is before the version number).

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

Steve B
  • 36,818
  • 21
  • 101
  • 174

1 Answers1

2

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;
      }
   }))
Mark
  • 143,421
  • 24
  • 428
  • 436