diff --git a/.gitignore b/.gitignore index 404d2ea..1ff5fbc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules npm-debug.log tmp .DS_STORE +.idea diff --git a/README.md b/README.md index e07a511..767eac3 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ grunt.initConfig({ defaults. Extra details are below. { algorithm: 'md5', // Algoirthm used for hashing files + alias: '[]', // Look for a physical path instead of using the alias baseDir: './', // The base directory for all assets cdnPath: false, // You're own CDN path deleteOriginals: false, // Delete the original file after hashing @@ -105,6 +106,22 @@ Default value: `'md5'` `algorithm` is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are `'sha1'`, `'md5'`, `'sha256'`, `'sha512'` +#### options.alias +Type: `Array` + +Default value: `'[]'` + +You can override this at the file level, e.g: + +``` +options: { + rename: false, + alias: [{ + 'path-to-search': 'new-path' + }], +}, +``` + #### options.baseDir Type: `String` @@ -303,6 +320,9 @@ Remote URLs for CSS, JavaScript, and images are ignored by cacheBust. This assum ### Change Log +**v0.5.0** - 2015-06-23 +* Added new configuration to enable server aliases. + **v0.4.13** - 2015-02-27 * Fixes issue with deleting the original files when referenced in more than one source file. * Fixed issue with hashe in the url of assets when referenced in CSS diff --git a/package.json b/package.json index bf9cd7e..45cd013 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-cache-bust", "description": "Bust static assets from the cache using content hashing", - "version": "0.4.13", + "version": "0.5.0", "author": "Ben Holland ", "repository": { "type": "git", diff --git a/tasks/cachebust.js b/tasks/cachebust.js index 9b8a857..441c82d 100644 --- a/tasks/cachebust.js +++ b/tasks/cachebust.js @@ -156,15 +156,18 @@ module.exports = function(grunt) { filename = utils.removeHashInUrl(filename); reference = utils.removeHashInUrl(reference); - if (!grunt.file.exists(filename)) { - grunt.log.warn('Static asset "' + filename + '" skipped because it wasn\'t found, original reference=' + reference); + var physicalFilename = utils.getPhysicalPath(filename, opts.alias); + var physicalReference = utils.getPhysicalPath(reference, opts.alias); + + if (!grunt.file.exists(physicalFilename)) { + grunt.log.warn('Static asset "' + physicalFilename + '" skipped because it wasn\'t found, original reference=' + physicalReference); return false; } // Cater for special `?#iefix` in font face declarations - this isn't pretty reference = reference.replace('?#', '#'); - newFilename = reference.split('?')[0] + '?' + utils.generateFileHash(grunt.file.read(filename)); + newFilename = reference.split('?')[0] + '?' + utils.generateFileHash(grunt.file.read(physicalFilename)); newReference = newFilename; markup = markup.replace(new RegExp(utils.regexEscape(reference), 'g'), newFilename); } diff --git a/tasks/lib/utils.js b/tasks/lib/utils.js index e058416..215445e 100644 --- a/tasks/lib/utils.js +++ b/tasks/lib/utils.js @@ -60,6 +60,43 @@ module.exports = function(opts) { return str.replace(findHash, function(match, hash, extension) { return extension; }); + }, + + /** + * The current filename can be obtained using some server aliases. + * If a filename part matches one of them, then the function returns the physical filename replacing all aliases + * with the real path. + * + * @param {string} filename + * @param {array} alias It should match following structure: + * [ + * { + * 'path-to-search': 'new-path' + * }, + * { + * 'other-path-to-search': 'other-path' + * },... + * ] + * @return {string} physicalFilename + */ + getPhysicalPath: function(filename, alias) { + if (typeof filename !== "string") { + throw 'Filename MUST be a string'; + } + + var physicalFilename = filename; + + if (alias && typeof alias === "object") { + alias.forEach(function(alias) { + for (var key in alias) { + if (filename.indexOf(key) > -1) { + physicalFilename = physicalFilename.replace(key, alias[key]); + } + } + }); + } + + return physicalFilename; } }; diff --git a/tests/utils/utils_test.js b/tests/utils/utils_test.js index 26e3d48..91bdd7d 100644 --- a/tests/utils/utils_test.js +++ b/tests/utils/utils_test.js @@ -22,6 +22,27 @@ module.exports = { test.equal(utils.removeHashInUrl('/assets/scripts.js#4567#678'), '/assets/scripts.js'); test.done(); - } + }, + + getPhysicalPath: function(test) { + test.expect(6); + + test.throws(function() { + utils.getPhysicalPath(); + }); + test.throws(function() { + utils.getPhysicalPath(123); + }); + test.doesNotThrow(function() { + utils.getPhysicalPath('/assets/style.css', true); + }); + test.doesNotThrow(function() { + utils.getPhysicalPath('/assets/style.css'); + }); + test.equal(utils.getPhysicalPath('/assets/styles.css', true), '/assets/styles.css'); + test.equal(utils.getPhysicalPath('/assets/styles.css', [{ 'assets': 'test' }]), '/test/styles.css'); + + test.done(); + } };