diff --git a/Gruntfile.js b/Gruntfile.js index 0986aa2..f2aaa4a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -57,6 +57,13 @@ module.exports = function (grunt) { cwd: 'test/fixtures', src: ['*.js'], dest: 'test/tmp/withSourceMaps' + }, + withBasenameOption: { + options: { + basename: true + }, + src: ['test/tmp/another.png'], + dest: 'test/tmp/relative' } }, simplemocha: { @@ -79,15 +86,32 @@ module.exports = function (grunt) { 'jshint', 'clean', 'copy', - 'filerev', + 'filerev:compile', + 'filerev:withConfig', + 'filerev:withDest', + 'filerev:withExpand', + 'filerev:withSummaryAttributeName', + 'filerev:withSourceMaps', 'checkSummary', + 'clearSummary', + 'filerev:withBasenameOption', + 'checkBasenameSummary', 'simplemocha', 'clean' ]); + grunt.registerTask('clearSummary', 'clear filerev summary', function () { + grunt.filerev.summary = {}; + }); + grunt.registerTask('checkSummary', 'Check that summary attribute is correctly created', function () { var src = path.normalize('test/fixtures/file.png'); var expected = path.normalize('test/tmp/file.26365248.png'); assert.equal(grunt.filerev.summary[src], expected); }); + + grunt.registerTask('checkBasenameSummary', 'Check that summary attribute is correctly created', function () { + assert.equal(Object.keys(grunt.filerev.summary)[0], 'another.png'); + assert.equal(grunt.filerev.summary['another.png'], 'another.92279d3f.png'); + }); }; diff --git a/README.md b/README.md index 0cd3004..81ec947 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,21 @@ The task will ensure that any source map for `.css` or `.js` file is revisioned For example, `js/main.js` revisioned to `js/main.9d713a59.js` will also have `js/main.js.map` revisioned to the same hash `js/main.9d713a59.js.map`. +#### basename + +Type: `boolean` +Default: `false` + +This option is useful when you are only interested about having a map of the transformation of the filename, which means the summary will only output the basename. + +For example you would have the following map, without taking into account which directories original and revved files would be. +``` +{ + 'img1.png': 'img1.59bcc3ad.png', + 'img2.png': 'img2.060b1aa6.png' +} +``` + ## License [BSD license](http://opensource.org/licenses/bsd-license.php) and copyright Google diff --git a/tasks/filerev.js b/tasks/filerev.js index 0197ab8..3778373 100644 --- a/tasks/filerev.js +++ b/tasks/filerev.js @@ -9,7 +9,8 @@ module.exports = function (grunt) { grunt.registerMultiTask('filerev', 'File revisioning based on content hashing', function () { var options = this.options({ algorithm: 'md5', - length: 8 + length: 8, + basename: false }); var target = this.target; var filerev = grunt.filerev || {summary: {}}; @@ -74,8 +75,13 @@ module.exports = function (grunt) { } } - filerev.summary[path.normalize(file)] = path.join(dirname, newName); - grunt.verbose.writeln(chalk.green('✔ ') + file + chalk.gray(' changed to ') + newName); + if (options.basename) { + filerev.summary[path.basename(file)] = newName; + } else { + filerev.summary[path.normalize(file)] = path.join(dirname, newName); + } + grunt.log.writeln(chalk.green('✔ ') + file + chalk.gray(' changed to ') + newName); + if (sourceMap) { filerev.summary[path.normalize(file + '.map')] = path.join(dirname, newName + '.map'); grunt.verbose.writeln(chalk.green('✔ ') + file + '.map' + chalk.gray(' changed to ') + newName + '.map'); diff --git a/test/test.js b/test/test.js index 1692408..7f942fd 100644 --- a/test/test.js +++ b/test/test.js @@ -51,4 +51,3 @@ it('should revision .js file ok without any .map', function () { var revisioned = fs.statSync(hashes[file]).size; assert(revisioned === original); }); -