diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/tasks/filecache.json b/tasks/filecache.json new file mode 100755 index 0000000..9e26dfe --- /dev/null +++ b/tasks/filecache.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tasks/hashresHelper.js b/tasks/hashresHelper.js old mode 100644 new mode 100755 index d65309e..200cedb --- a/tasks/hashresHelper.js +++ b/tasks/hashresHelper.js @@ -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, @@ -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)); } };