5

I use gulp-inject to add files to load to index.html. To force browser to reload js-scripts, I want to add a query string to the filename that is injected into index.html.

I suspect this is possible with gulp-inject's transform function. But reading the docs I do not understand how it could be solved. Do you?

EricC
  • 5,720
  • 13
  • 52
  • 71

2 Answers2

7

Ok, found out how to do it.

Add a transform function like this:

  transform: function (filepath) {
    arguments[0] = filepath + '?v=' + pjson.version;
    return inject.transform.apply(inject.transform, arguments);
  }

FYI, I update version number using gulp-bump and I get the package info for my app (that include version number) like this: var pjson = require('./package.json');

EricC
  • 5,720
  • 13
  • 52
  • 71
4

based on EricC answer, full code will look something like that:

gulp.task('inject', function() {
    return gulp.src(paths.html, {cwd: bases.application})
        .pipe(inject(gulp.src([
            bases.build + 'css/styles.min.css',
            bases.build + 'scripts/app.min.js'
        ]), {
            transform: function (filepath) {
                arguments[0] = filepath + '?v=' + app_version;
                return inject.transform.apply(inject.transform, arguments);
            }
        }
    ))
    .pipe(gulp.dest(bases.build));
});
strongBAD
  • 331
  • 2
  • 11