Luismahou / grunt-hashres

Hashes your js and css files and rename the <script> and <link> declarations that refer to them in your html/php/etc files.
MIT License
115 stars 41 forks source link

Take file path into account when substituting #39

Open ebello opened 10 years ago

ebello commented 10 years ago

I have a use case similar to the test, in that I have two files of the same name in different directories that both need to be substituted in my file. In the current version of hashres it only takes the filename into account when substituting, so in the destination file both instances would be replaced with the same hash.

The easiest way I found to take path into account is to find the common path that all files in the src have and remove that when considering the substitution. The test file illustrates this. The files to be hashed are:

./temp/helper/subfolders-same-filename/scripts/myscripts1.js
./temp/helper/subfolders-same-filename/scripts/sub/myscripts1.js
./temp/helper/subfolders-same-filename/styles/mystyles1.css
./temp/helper/subfolders-same-filename/styles/sub/mystyles1.css

In the destination file though, the files are referenced relatively:

scripts/myscripts1.js
scripts/sub/myscripts1.js
styles/mystyles1.css
styles/sub/mystyles1.css

So, it would not suffice to simply substitute by file path + file name. This code introduces a findCommonPath method that will end up removing ./temp/helper/subfolders-same-filename/ above in order to substitute as expected.

This is a breaking change, however, as the following src list may break for substitutions, depending on how the destination file is referencing the source files:

// this may not work as expected
files: [{
  src : grunt.file.expand([
    './temp/helper/subfolders-same-filename/scripts/**/*.js',
    './another-location/helper/subfolders-same-filename/styles/**/*.css']),
  dest: './temp/helper/subfolders-same-filename/index.html'
}],

Although, my guess is that in practice this functionality change wouldn't affect most implementations. An alternative is to perhaps add a flag in options to take path substitutions into account.

dk1 commented 10 years ago

Good stuff! I ran in to this bug myself on the weekend

boycce commented 9 years ago

Error: Warning: Invalid regular expression: /build\js\1.libs\boot\-libs\.[0-9a-f]{8}\.js/: Unterminated character class Use OS: Windows

options: {
  encoding: 'utf8',
  fileNameFormat: '${name}.${hash}.${ext}',
  renameFiles: true
},
main: {
  src: [
    'build/js/*.js',
    'build/js/1.libs/*.js',
    'build/css/*.css',
    'build/imgs/*.jpg',
    'build/imgs/*.png',
  ],
  dest: [
    'build/tree.json',
    'build/css/*.css'
  ],
}