gulp-rev: Merge: true not working for rev.manifest()

I use gulp-rev 4 times, once for js, css, templates and for the concatenated and minified JS. The merge option is set to true. However the manifests are not merged, but overwritten. How do I get the rev.manifest() to merge? Note - I do understand that the gulp.dest() causes the file to be overwritten. But how else do you merge?

    // Include gulp
var gulp = require('gulp'); 

// Include Our Plugins
var jshint = require('gulp-jshint');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var rev = require('gulp-rev');
var del = require('del');
var confirm = require('gulp-confirm');

//gulp runs tasks async, but that causes issues for us.

gulp.task('clean', function () {
    return del([
        'dist/**/*' // clean out this directory
    ]);
});

// Lint Task
gulp.task('lint', ['clean'], function() {
    //return gulp.src('js/**/*.js')
    //    .pipe(jshint())
    //    .pipe(jshint.reporter('default'));
    });

// Compile Our Sass
gulp.task('sass', ['lint'], function() {
    //return gulp.src('sass/*.scss')
    //    .pipe(sass())
    //    .pipe(gulp.dest('css'));
    });

gulp.task('rev-js', ['sass'], function () {
    return gulp.src(['js/**/*.js'])
        //.pipe(gulp.dest('dist/js')) // copy files to dist
        .pipe(rev()) // revision them
        .pipe(gulp.dest('dist/js')) // copy the revision files to dist
        .pipe(rev.manifest("rev-manifest.json", {
            merge: true // merge with the existing manifest (if one exists)
        }))
        .pipe(gulp.dest('dist'))
        .pipe(confirm({
            // Static text.
            question: 'Completed rev JS. Continue?',
            input: '_key:y'
        }));
});

gulp.task('rev-css', ['rev-js'], function () {
    return gulp.src(['css/*.css'])
        //.pipe(gulp.dest('dist/css')) // copy files to dist
        .pipe(rev()) // revision them
        .pipe(gulp.dest('dist/css')) // copy the revision files to dist
        .pipe(rev.manifest("rev-manifest.json", {
            merge: true // merge with the existing manifest (if one exists)
        }))
        .pipe(gulp.dest('dist'))
        .pipe(confirm({
            // Static text.
            question: 'Completed rev CSS. Continue?',
            input: '_key:y'
        }));
});

gulp.task('rev-templates', ['rev-css'], function () {
    return gulp.src(['templates/**/*.html'])
        //.pipe(gulp.dest('dist/templates')) // copy files to dist
        .pipe(rev()) // revision them
        .pipe(gulp.dest('dist/templates')) // copy the revision files to dist
        .pipe(rev.manifest("rev-manifest.json", {
            merge: true // merge with the existing manifest (if one exists)
        }))
        .pipe(gulp.dest('dist'));
});

// Concatenate & Minify JS
gulp.task('scripts', ['rev-templates'], function() {
    gulp.src('js/**/*.js')
        .pipe(concat('all.js'))
        .pipe(gulp.dest('dist'))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist'))
        .pipe(rev())
        .pipe(gulp.dest('dist'))
        .pipe(rev.manifest("rev-manifest.json", {
            merge: true // merge with the existing manifest (if one exists)
        }))
        .pipe(gulp.dest('dist'));
    });

// Watch Files For Changes
gulp.task('watch', ['scripts'], function() {
    //gulp.watch('js/*.js', ['lint', 'scripts']);
    //gulp.watch('scss/*.scss', ['sass']);
    });

// Default Task
gulp.task('default', ['watch']);

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 31 (3 by maintainers)

Most upvoted comments

You indeed HAVE to pass the path of the manifest as parameter before the options if you want the merge to work, here is the example corrected :

var gulp = require('gulp');
var rev = require('gulp-rev');

gulp.task('default', function () {
    // by default, gulp would pick `assets/css` as the base,
    // so we need to set it explicitly:
    return gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
        .pipe(gulp.dest('build/assets'))
        .pipe(rev())
        .pipe(gulp.dest('build/assets'))
        .pipe(rev.manifest('build/assets/rev-manifest.json', {
            base: 'build/assets',
            merge: true // merge with the existing manifest (if one exists)
        }))
        .pipe(gulp.dest('build/assets'));
});

I use

.pipe($.rev.manifest('dist/rev-manifest.json', {base: process.cwd()+'/dist', merge: true}))
.pipe(gulp.dest('dist'));

@bensampaio try to change the path of the rev-manifest in this line : .pipe(rev.manifest('rev-manifest.json', { merge: true, base: buildFolder })) to the same as the base path : .pipe(rev.manifest(buildFolder + '/rev-manifest.json', { merge: true, base: buildFolder }))

Merging is handled right here.

Everyone with this issue: your problem is likely that gulp tasks run in parallel (and async), and later tasks are overwriting earlier ones without merging their changes together. See #115 for a couple of things to try to fix your task definitions.

I agree with @goooseman ,

.pipe(rev.manifest('dist/rev-manifest.json', {base: process.cwd()+'/dist', merge: true}))
.pipe(gulp.dest('dist'));

it working!!

I also bumped to this problem. To solve it, first I dump each files to different manifest json file. (Ex: a.json, b.json and etc). After all tasks (which has its own manifest file) finished, I use a task to merge all that files into 1 manifest file. Here is the example code:

gulp.task('css', function() {
  gulp.src(['css/**/*.css'])
    .pipe(concat())  // gulp-concat
    .pipe(rev())  // gulp-rev
    .pipe(gulp.dest('dist/css'))
    .pipe(rev.manifest('tmp/manifest/css-manifest.json'))
    .pipe(gulp.dest('.'));

gulp.task('js', function() {
  gulp.src(['js/**/*.js'])
    .pipe(concat())
    .pipe(rev())
    .pipe(gulp.dest('dist/js'))
    .pipe(rev.manifest('tmp/manifest/js-manifest.json'))
    .pipe(gulp.dest('.'));
});

gulp.task('manifest', function() {
  gulp.src(['tmp/manifest/*.json'])
    .pipe(extend('rev-manifest.json')) // gulp-extend
    .pipe(gulp.dest('.');
});

gulp.task('build', ['css', 'js']);

// Run the tasks synchronously with gulp-shell
gulp.task('default', shell.task([
  'gulp build',
  'gulp manifest'
]);

In my opinion, the task take some times to write file. When the task finished, the file is not completely written, yet.

Currently this method works for me. But I don’t know wether this is a good way. Any suggestions is really appreciated.

@OliverJAsh I just spent 30 mins investigating the same issue, and found the same thing. I just changed base: 'dist' to cwd: 'dist' and everything started working. I’ll send a PR to change the docs.

edit: I also had to provide an absolute url to the file like @goooseman suggested. i’ll add that to the docs too.

vinyl-file doesn’t pick up the base option we pass into it: https://github.com/sindresorhus/vinyl-file/blob/9bdbc6d9211d7d896e2e3c88afa8beb530d873b7/index.js#L19

Is that a bug in vinyl-file?

If I pass in a cwd option instead of base, it works:

.pipe(rev.manifest({
    cwd: './public',
    merge: true
}))

Everyone with this issue: rev.manifest can’t tell where your existing file is if you’re writing it to another directory via gulp.dest. It doesn’t know the future, it doesn’t know that one line later in your code you’re going to be redirecting it to another folder. It has no idea where to look unless you tell it. So pass a base option if you’re writing your manifest somewhere else via gulp.dest 😃

@bensampaio you need to supply a base for your rev.manifest call:

.pipe(rev.manifest('rev-manifest.json', { merge: true, base: buildFolder }))

@mtpultz @LoicUV Because you’ve set the gulp.src {base} property, the plugin is looking for an existing manifest file inside public/ and not public/build.

You may try:

  1. Changing the base value on your gulp.src to outputFolder
  2. Adding in a {path: 'public/build/rev-manifest.json'} option along with your merge

As seen in the code, the plugin looks for a manifest solely based on opts.path. If one is not found, then merge: true becomes irrelevant.