Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gulp-inject: How to add query string to injected filename

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?

like image 489
EricC Avatar asked Aug 27 '15 06:08

EricC


2 Answers

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');

like image 60
EricC Avatar answered Jan 03 '23 11:01

EricC


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));
});
like image 32
strongBAD Avatar answered Jan 03 '23 12:01

strongBAD