Comparing version
# Changelog | ||
## 4.1.0 | ||
Thanks to PR #18 from @MEGApixel23: | ||
* New signature for hash.manifest: `hash.manifest(manifestPath, options)` | ||
* Old signature `hash.manifest(manifestPath, append, space)` is still supported | ||
* New options for `hash.manifest`: | ||
* `deleteOld` Deletes old hashed files mentioned in the manifest. This includes both files with different hashes and files that have been removed from the manifest. | ||
* `sourceDir` Used with `deleteOld`. Specifies the directory where to delete old hashed files from. | ||
## 4.0.1 | ||
@@ -4,0 +12,0 @@ * Update contributor and changelog info for npm |
48
index.js
@@ -26,3 +26,3 @@ var crypto = require('crypto'), | ||
var fileExt = path.extname(file.relative), | ||
fileName = path.basename(file.relative, fileExt); | ||
fileName = path.basename(file.relative, fileExt); | ||
@@ -70,6 +70,22 @@ var hasher = crypto.createHash(options.algorithm); | ||
exportObj.manifest = function(manifestPath, append, space) { | ||
append = (typeof append === 'undefined' ? true : append); | ||
var manifest = {}; | ||
exportObj.manifest = function(manifestPath, options) { | ||
var space = null; | ||
var append = false; | ||
var sourceDir = __dirname; | ||
var deleteOld = false; | ||
if (arguments.length === 2 && typeof options === 'object') { | ||
// New signature | ||
if (options.append != null) append = options.append; | ||
if (options.space != null) space = options.space; | ||
if (options.sourceDir) sourceDir = options.sourceDir; | ||
deleteOld = !!options.deleteOld; | ||
} else { | ||
// Old signature | ||
if (arguments[1] != null) append = arguments[1]; | ||
if (arguments[2] != null) space = arguments[2]; | ||
} | ||
var newManifest = {}; | ||
if (append && ! origManifestContents[manifestPath]) { | ||
@@ -84,2 +100,14 @@ try { | ||
function deleteOldFiles(oldFiles, newFiles, dirPath) { | ||
for (var prop in oldFiles) { | ||
if (newFiles.hasOwnProperty(prop) === false || oldFiles[prop] !== newFiles[prop]) { | ||
try { | ||
fs.unlinkSync(path.join(dirPath, oldFiles[prop])); | ||
} catch (e) { | ||
console.warn(e.message); | ||
} | ||
} | ||
} | ||
} | ||
return through2.obj( | ||
@@ -90,3 +118,3 @@ function(file, enc, cb) { | ||
var manifestDest = formatManifestPath(file.relative); | ||
manifest[manifestSrc] = manifestDest; | ||
newManifest[manifestSrc] = manifestDest; | ||
} | ||
@@ -98,3 +126,7 @@ | ||
function(cb) { | ||
var finish = function(data) { | ||
var finish = function (data) { | ||
if (deleteOld) { | ||
deleteOldFiles(origManifestContents[manifestPath], data, sourceDir); | ||
} | ||
origManifestContents[manifestPath] = data; | ||
@@ -112,7 +144,7 @@ | ||
appendQueue.then(new Promise(function(resolve) { | ||
finish(assign({}, origManifestContents[manifestPath], manifest)); | ||
finish(assign({}, origManifestContents[manifestPath], newManifest)); | ||
resolve(); | ||
})); | ||
} else { | ||
finish(manifest); | ||
finish(newManifest); | ||
} | ||
@@ -119,0 +151,0 @@ } |
{ | ||
"name": "gulp-hash", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Cachebust your assets by adding a hash to the filename", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -14,3 +14,6 @@ # gulp-hash [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] | ||
.pipe(gulp.dest('public/js')) // Write the renamed files | ||
.pipe(hash.manifest('assets.json')) // Switch to the manifest file | ||
.pipe(hash.manifest('assets.json', { | ||
deleteOld: true, | ||
sourceDir: __dirname + '/public/js' | ||
})) // Switch to the manifest file | ||
.pipe(gulp.dest('public')); // Write the manifest file | ||
@@ -34,2 +37,12 @@ ``` | ||
### hash.manifest(manifestPath, options) | ||
| Parameter | Default | Description | | ||
| --------- | ------- | ----------- | | ||
| manifestPath | (none) | The desired path to the manifest file | | ||
| options.append | true | Whether to merge the new manifest with an existing one's contents (same filename, doesn't have to exist before first run) | | ||
| options.space | null | [The space parameter for JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)| | ||
| options.deleteOld | false | If set to `true`, deletes old versions of hashed files | | ||
| options.sourceDir | __dirname | Used with `deleteOld`. Specifies where to search for old files to delete. | | ||
### hash.manifest(manifestPath, append, space) | ||
@@ -36,0 +49,0 @@ |
10108
29.09%6
20%125
28.87%59
28.26%