Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GulpJS: How to rev images and then update their refs in css files?

Tags:

gulp

build

gulp.task('usemin', function () {

  return gulp.src(path.src + '*.html')
    .pipe(usemin({
      assetsDir: 'src',
      css: [ minifyCss(), 'concat', rev()],
      js: [uglify(), rev()],
      images: [rev()]
    }))
    .pipe(gulp.dest(path.dist));
});

It does not work on the images.

like image 327
Frank Fang Avatar asked Apr 20 '14 05:04

Frank Fang


People also ask

How do I minify JavaScript using Gulp?

js, first declare the dependencies as shown in the following code. var gulp = require('gulp'); var concat = require('gulp-concat'); var uglify = require('gulp-uglify'); var minify = require('gulp-minify-css'); Next, you need to create tasks for optimizing CSS and JavaScript as shown in the following code. gulp.

What is Gulp Rev?

Static asset revisioning with dependency considerations, appends content hash to each filename (eg. unicorn. css => unicorn.

What is Gulp SRC?

src() is given a glob to read from the file system and produces a Node stream. It locates all matching files and reads them into memory to pass through the stream. The stream produced by src() should be returned from a task to signal async completion, as mentioned in Creating Tasks.


1 Answers

The philosophy of gulp-rev-all is to me a good way to see asset revisioning. It's very well explained in their Readme that the hash should also take into account the reference(s) between revisioned files.

I've mocked up a little example that minify an image and a css file which use a background url property to see the revision of the new image path.

gulp.task('image', function () {
  return gulp.src('image.jpeg')
    .pipe(img({ progressive: false }))
    .pipe(gulp.dest('tmp'));
});

gulp.task('css', function () {
  return gulp.src('test.css')
    .pipe(css())
    .pipe(gulp.dest('tmp'));
});

gulp.task('rev', ['image', 'css'], function () {
  return gulp.src('tmp/**')
    .pipe(rev())
    .pipe(gulp.dest('dist'));
});

I've removed all the fancy stuff to be more clear, but you can see the whole example here.

like image 176
Preview Avatar answered Oct 05 '22 10:10

Preview