index.js
function(opts) {
	var opts = opts || {};
	var shouldHash = typeof opts.hash != 'undefined' ? opts.hash : defaults.hash;
	var prefix = _.flatten([defaults.prefix]);
	var shouldPrefix = typeof opts.shouldPrefix != 'undefined' ? opts.shouldPrefix : true;
	return through.obj(function(file, enc, cb) {
		var originalPath = file.path;

		// Get hash of contents
		var hash = md5(opts, file.contents.toString());

		// Construct new filename
		var ext = path.extname(file.path);
		var basePath = path.basename(file.path, ext);
		var filename = typeof hash !== 'undefined' ? basePath + '-' + hash + ext : basePath + ext;
		file.path = path.join(path.dirname(file.path), filename);

		// Add to manifest
		var base = path.join(file.cwd, defaults.src);
		var key = originalPath.replace(base, '');

		// @TODO: Instead of this it could use "glob" module to regex delete files
		// Check for existing value and whether cleanup is set
		var existing = manifest[key];
		if (existing && existing.src && defaults.cleanup) {
			// Delete the file
			fs.unlink(path.join(file.cwd, defaults.dest, existing.src));
		} else if (defaults.cleanup && shouldHash) {
			// Check if cleanup and hash enabled then we can remove any non hashed version from dest directory
			var nonHashPath = path.join(path.dirname(originalPath), basePath + ext).replace(base, '');
			var absPath = path.join(file.cwd, defaults.dest, nonHashPath);
			fs.exists(absPath, function(exists) {
				if (!exists) return;
				fs.unlink(absPath);
			});
		}

		var filePrefix = shouldPrefix ? prefix[index % prefix.length] : '';

		// Finally add new value to manifest
		var src = file.path.replace(base, '');
		manifest[key] = {
			index: index++,
			src: src,
			dest: filePrefix + src
		};

		// Write manifest file
		writeManifest();

		// Return and continue
		this.push(file);
		cb();
	});
}
function(opts) {
	var opts = opts || {};
	var shouldHash = typeof opts.hash != 'undefined' ? opts.hash : defaults.hash;
	var prefix = _.flatten([defaults.prefix]);
	var shouldPrefix = typeof opts.shouldPrefix != 'undefined' ? opts.shouldPrefix : true;
	return through.obj(function(file, enc, cb) {
		var originalPath = file.path;

		// Get hash of contents
		var hash = md5(opts, file.contents.toString());

		// Construct new filename
		var ext = path.extname(file.path);
		var basePath = path.basename(file.path, ext);
		var filename = typeof hash !== 'undefined' ? basePath + '-' + hash + ext : basePath + ext;
		file.path = path.join(path.dirname(file.path), filename);

		// Add to manifest
		var base = path.join(file.cwd, defaults.src);
		var key = originalPath.replace(base, '');

		// @TODO: Instead of this it could use "glob" module to regex delete files
		// Check for existing value and whether cleanup is set
		var existing = manifest[key];
		if (existing && existing.src && defaults.cleanup) {
			// Delete the file
			fs.unlink(path.join(file.cwd, defaults.dest, existing.src));
		} else if (defaults.cleanup && shouldHash) {
			// Check if cleanup and hash enabled then we can remove any non hashed version from dest directory
			var nonHashPath = path.join(path.dirname(originalPath), basePath + ext).replace(base, '');
			var absPath = path.join(file.cwd, defaults.dest, nonHashPath);
			fs.exists(absPath, function(exists) {
				if (!exists) return;
				fs.unlink(absPath);
			});
		}

		var filePrefix = shouldPrefix ? prefix[index % prefix.length] : '';

		// Finally add new value to manifest
		var src = file.path.replace(base, '');
		manifest[key] = {
			index: index++,
			src: src,
			dest: filePrefix + src
		};

		// Write manifest file
		writeManifest();

		// Return and continue
		this.push(file);
		cb();
	});
}
function ( opts ) { var opts = opts || { } ; var shouldHash = typeof opts . hash != 'undefined' ? opts . hash : defaults . hash ; var prefix = _ . flatten ( [ defaults . prefix ] ) ; var shouldPrefix = typeof opts . shouldPrefix != 'undefined' ? opts . shouldPrefix : true ; return through . obj ( function ( file , enc , cb ) { var originalPath = file . path ; // Get hash of contents var hash = md5 ( opts , file . contents . toString ( ) ) ; // Construct new filename var ext = path . extname ( file . path ) ; var basePath = path . basename ( file . path , ext ) ; var filename = typeof hash !== 'undefined' ? basePath + '-' + hash + ext : basePath + ext ; file . path = path . join ( path . dirname ( file . path ) , filename ) ; // Add to manifest var base = path . join ( file . cwd , defaults . src ) ; var key = originalPath . replace ( base , '' ) ; // @TODO: Instead of this it could use "glob" module to regex delete files // Check for existing value and whether cleanup is set var existing = manifest [ key ] ; if ( existing && existing . src && defaults . cleanup ) { // Delete the file fs . unlink ( path . join ( file . cwd , defaults . dest , existing . src ) ) ; } else if ( defaults . cleanup && shouldHash ) { // Check if cleanup and hash enabled then we can remove any non hashed version from dest directory var nonHashPath = path . join ( path . dirname ( originalPath ) , basePath + ext ) . replace ( base , '' ) ; var absPath = path . join ( file . cwd , defaults . dest , nonHashPath ) ; fs . exists ( absPath , function ( exists ) { if ( ! exists ) return ; fs . unlink ( absPath ) ; } ) ; } var filePrefix = shouldPrefix ? prefix [ index % prefix . length ] : '' ; // Finally add new value to manifest var src = file . path . replace ( base , '' ) ; manifest [ key ] = { index : index ++ , src : src , dest : filePrefix + src } ; // Write manifest file writeManifest ( ) ; // Return and continue this . push ( file ) ; cb ( ) ; } ) ; }
Gets md5 from file contents and writes new filename with hash included to destinations
Gets md5 from file contents and writes new filename with hash included to destinations