Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Add option to store revision as a query param instead of renaming the files #90

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
26 changes: 18 additions & 8 deletions tasks/filerev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Binary file added test/fixtures/not-modified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
});