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

Add basename option #60

Open
wants to merge 3 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
26 changes: 25 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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');
});
};
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions tasks/filerev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}};
Expand Down Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ it('should revision .js file ok without any .map', function () {
var revisioned = fs.statSync(hashes[file]).size;
assert(revisioned === original);
});