Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
miladmeidanshahi opened this issue Oct 2, 2017 · 2 comments
Open
Labels

Comments

@miladmeidanshahi
Copy link

miladmeidanshahi commented Oct 2, 2017

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

@zhouzi zhouzi added the question label Oct 2, 2017
@prime89
Copy link

prime89 commented Dec 4, 2017

The merge option does not work on multi-process

@Saturate
Copy link

Saturate commented May 2, 2018

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants