Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

When you hash an already hashed file, the file reference is not replaced in the dest #26

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified README.md
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions tasks/filecache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
17 changes: 15 additions & 2 deletions tasks/hashresHelper.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function preg_quote (str, delimiter) {
}

exports.hashAndSub = function(grunt, options) {

//We open the cache file, and then JSON decode it.
var fileCache = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'filecache.json')));

var src = options.src,
dest = options.dest,
encoding = options.encoding,
Expand Down Expand Up @@ -71,11 +73,22 @@ exports.hashAndSub = function(grunt, options) {
var destContents = fs.readFileSync(f, encoding);
for (var name in nameToHashedName) {
grunt.log.debug('Substituting ' + name + ' by ' + nameToHashedName[name]);
destContents = destContents.replace(new RegExp(preg_quote(name), "g"), nameToHashedName[name]);
if (fileCache[name]) {
//If the file was already hashed, and it has a wierd name like style.df23d23.css, then replace that name instead of the original name.
grunt.log.debug('File was already hashed');
destContents = destContents.replace(new RegExp(preg_quote(fileCache[name]), "g"), nameToHashedName[name]);
} else {
//If the file has not been hashed before
grunt.log.debug('File is new');
destContents = destContents.replace(new RegExp(preg_quote(name), "g"), nameToHashedName[name]);
}
fileCache[name] = nameToHashedName[name];
}
grunt.log.debug('Saving the updated contents of the outination file');
fs.writeFileSync(f, destContents, encoding);
});
});
//Save the cache file with the new mappings
fs.writeFileSync(path.resolve(__dirname, 'filecache.json'), JSON.stringify(fileCache));
}
};