Luismahou/grunt-hashres — pull request #10 by AdamWagner (2013-05-31 07:19:02+00:00)
Title: Match cache-busted filename on subsequent builds

This is my first pull request, so bear with me :-)

# The issue
Replacing the orignal filenames with the hashed name worked fine the first time the task was run. On subsequent runs however, the regex would match the original filename as a substring of the hashed name, and replace that substring with the hashed name again. The result looked like this after 3 runs:

4a2e4963.4a2e4963.4a2e4963.script.min.js

... which broke the reference to the hashed file.

# The fix
I fixed the issue by first checking the contents of the destination file for a match of the hashed filename using:

RegExp('[a-z0-9]{8}.' + name, "g");

If that match is found, it is used during the replace. Otherwise, the original match on

name

is used.