sindresorhus / gulp-rev

Static asset revisioning by appending content hash to filenames: `unicorn.css` → `unicorn-d41d8cd98f.css`
MIT License
1.54k stars 217 forks source link

merge all rev-manifest.json assets to one rev-manifest.json #231

Open miladmeidanshahi opened 7 years ago

miladmeidanshahi commented 7 years ago

Description

Hi, how can i merge all assets to one rev-manifest.json for example css files js files to one rev-manifest.json and rev-del work correctly in this situation.

here is asset directory

static/dist/css/*.css
static/dist/js/*.js

rev-manifest.json files for all assets

static/dist/rev-manifest.json

{
  "test.js": "test-b159db5fed.min.js",
  "test.css": "test-b159db5fed.min.css",
  ...
}

and finally i think the gulp task must be something like this

for javascript files

        .pipe(rev())
        .pipe(gulp.dest(statics + 'dist/js'))
        .pipe(rev.manifest({ merge: true }))
        .pipe(revDel({ dest: statics + 'dist/js' }))
        // hear save the rev-manifest.json to static/dist/rev-manifest.json not separate for each folder 
        .pipe(gulp.dest(statics + 'dist'))

for css files

        .pipe(rev())
        .pipe(gulp.dest(statics + 'dist/css'))
        .pipe(rev.manifest({ merge: true }))
        .pipe(revDel({ dest: statics + 'dist/css' }))
        // hear save the rev-manifest.json to static/dist/rev-manifest.json not separate for each folder 
        .pipe(gulp.dest(statics + 'dist'))

the important step is revdel should work fine

prime89 commented 6 years ago

The merge option does not work on multi-process

Saturate commented 6 years ago

You can write another gulp task to merge all the manifests after all parallel tasks are done. Something like this should do:

import fs from 'fs';
import log from 'fancy-log';
import chalk from 'chalk';

const options = {
    finalManifest: './dist/asset-manifest.json'
};

function readManifest(manifestPath) {
    return new Promise((resolve, reject) => {
        fs.readFile(manifestPath, 'utf8', (readErr, data) => {
            let jsonData;

            if (readErr) {
                console.error(readErr);
                return reject(readErr);
            }

            try {
                jsonData = JSON.parse(data);
            } catch (parseError) {
                console.error('Parse Error: ', manifestPath, data, parseError);
                return reject(parseError);
            }

            resolve(jsonData);

            return data;
        });
    });
}

function mergeManifest(done) {
    return Promise.all([
        readManifest('./dist/css/rev-manifest.json'),
        readManifest('./dist/js/rev-manifest.json')
    ]).then((values) => {
        const mergedManifest = Object.assign(...values);

        log(chalk.cyan('Writing merged assest manifest') + chalk.grey(` (${options.finalManifest})`));

        fs.writeFile(options.finalManifest, JSON.stringify(mergedManifest, null, '\t'), (err) => {
            if (err) throw err;
            done();
        });
    }).catch((err) => {
        console.error(err);
        throw err;
    });
}

export default mergeManifest;