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

Take file path into account when substituting #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ebello
Copy link

@ebello ebello commented May 12, 2014

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
Copy link

dk1 commented Nov 3, 2014

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

@boycce
Copy link

boycce commented Sep 13, 2015

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'
  ],
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants