diff --git a/Gruntfile.js b/Gruntfile.js index 2d84573..18f02c8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -76,6 +76,13 @@ module.exports = function (grunt) { cwd: 'test/fixtures', src: ['*.js', '*.css'], dest: 'test/tmp/withSourceMaps' + }, + usingQueryParams: { + options: { + useQueryParam: true + }, + src: ['test/fixtures/not-modified.png'], + dest: 'test/tmp/dest' } }, simplemocha: { diff --git a/README.md b/README.md index 0cd3004..b1ab442 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,13 @@ Default: `8` The number of characters of the file hash to prefix the file name with. +#### options.useQueryParam + +Type: `boolean` +Default: `false` + +Whether or not to use a query parameter (`?rev=XXXXXXX`) instead of actually renaming the files. This way, the files will retain their filename, but a query parameter containing the file hash can be attached and used in later references. The link to the file, including the query parameter can be found in the `grunt.filerev.summary` object and, e.g., be used in subsequent tasks like usemin. + ### Destination It will overwrite the `src` files if you don't specify a `dest`: diff --git a/tasks/filerev.js b/tasks/filerev.js index 28e570a..9619706 100644 --- a/tasks/filerev.js +++ b/tasks/filerev.js @@ -12,7 +12,8 @@ module.exports = function (grunt) { var filerev = grunt.filerev || {summary: {}}; var options = this.options({ algorithm: 'md5', - length: 8 + length: 8, + useQueryParam: false }); eachAsync(this.files, function (el, i, next) { @@ -50,14 +51,19 @@ module.exports = function (grunt) { var ext = path.extname(file); var newName; - if (typeof options.process === 'function') { - newName = options.process(path.basename(file, ext), suffix, ext.slice(1)); - } else { - if (options.process) { - grunt.log.error('options.process must be a function; ignoring'); - } + if (!options.useQueryParam) { + if (typeof options.process === 'function') { + newName = options.process(path.basename(file, ext), suffix, ext.slice(1)); + } else { + if (options.process) { + grunt.log.error('options.process must be a function; ignoring'); + } - newName = [path.basename(file, ext), suffix, ext.slice(1)].join('.'); + newName = [path.basename(file, ext), suffix, ext.slice(1)].join('.'); + } + } else { + // no renaming but the hash can be used to bust caches by appending it as a query parameter + newName = [path.basename(file, ext), ext.slice(1)].join('.'); } var resultPath; @@ -102,6 +108,10 @@ module.exports = function (grunt) { } filerev.summary[path.normalize(file)] = path.join(dirname, newName); + // if using the useQueryParam method, append the queryString + if (options.useQueryParam) { + filerev.summary[path.normalize(file)] += '?rev=' + suffix; + } grunt.verbose.writeln(chalk.green('✔ ') + file + chalk.gray(' changed to ') + newName); if (sourceMap) { diff --git a/test/fixtures/not-modified.png b/test/fixtures/not-modified.png new file mode 100644 index 0000000..f0a907d Binary files /dev/null and b/test/fixtures/not-modified.png differ diff --git a/test/test.js b/test/test.js index 8e97ae8..96834e5 100644 --- a/test/test.js +++ b/test/test.js @@ -12,7 +12,8 @@ var hashes = { 'test/fixtures/styles.css.map' : 'test/tmp/withSourceMaps/styles.a6aa2292.css.map', 'test/fixtures/more-styles.css' : 'test/tmp/withSourceMaps/more-styles.dce8e0e5.css', 'test/fixtures/inline.js' : 'test/tmp/withSourceMaps/inline.8b435ef2.js', - 'test/fixtures/another.png' : 'test/tmp/another-processed-92279d3f.png' + 'test/fixtures/another.png' : 'test/tmp/another-processed-92279d3f.png', + 'test/fixtures/not-modified.png' : 'test/tmp/not-modified.png' }; it('should revision files based on content', function () { @@ -98,3 +99,10 @@ it('should allow a filename processing function', function () { var revisioned = fs.statSync(hashes[file]).size; assert(revisioned === original); }); + +it('should not rename files with the useQueryParam option', function () { + var file = 'test/fixtures/not-modified.png'; + var original = fs.statSync(file).size; + var revisioned = fs.statSync(hashes[file]).size; + assert(revisioned === original); +});