diff --git a/lib/revvedfinder.js b/lib/revvedfinder.js index 29e33e1..730c843 100644 --- a/lib/revvedfinder.js +++ b/lib/revvedfinder.js @@ -9,7 +9,7 @@ var _ = require('lodash'); // - a hash mapping files with their revved versions // - a function that will return a list of file matching a given pattern (for example grunt.file.expand) // -var RevvedFinder = module.exports = function (locator) { +var RevvedFinder = module.exports = function (locator, baseURL) { if (_.isFunction(locator)) { debug('using function locator'); this.expandfn = locator; @@ -17,6 +17,7 @@ var RevvedFinder = module.exports = function (locator) { debug('using file locator %s', locator); this.mapping = locator; } + this.baseURL = baseURL || ''; }; var regexpQuote = function (str) { @@ -196,10 +197,12 @@ RevvedFinder.prototype.find = function find(ofile, searchDirs) { // filename = [dirname, filename].join('/'); // } - if (absolute) { + if (absolute && this.baseURL) { + filepath = prefix.substring(1) + filepath; // ignore the first slash + } else if (absolute) { filepath = prefix + filepath; } debug('Let\'s return %s', filepath); - return filepath; + return this.baseURL + filepath; }; diff --git a/tasks/usemin.js b/tasks/usemin.js index 9a4c6ca..1c2644c 100644 --- a/tasks/usemin.js +++ b/tasks/usemin.js @@ -122,7 +122,8 @@ module.exports = function (grunt) { // var locator = options.revmap ? grunt.file.readJSON(options.revmap) : function (p) { return grunt.file.expand({filter: 'isFile'}, p); }; var locator = getLocator(grunt, options); - var revvedfinder = new RevvedFinder(locator); + var baseURL = options.baseURL || ''; + var revvedfinder = new RevvedFinder(locator, baseURL); var handler = new FileProcessor(type, patterns, revvedfinder, function (msg) { grunt.verbose.writeln(msg); }, blockReplacements); diff --git a/test/test-revvedfinder.js b/test/test-revvedfinder.js index 5977b88..9e5fdc9 100644 --- a/test/test-revvedfinder.js +++ b/test/test-revvedfinder.js @@ -223,7 +223,13 @@ describe('RevvedFinder', function () { var file = rf.find('../../images/misc/test.png', ['temp/foo', 'dist/bar']); assert.equal(file, '../../images/misc/test.34546.png'); }); - + it('should return the corresponding file prepended with a base URL', function () { + var rf = new RevvedFinder(helpers.normalize({ + 'dist/images/misc/test.png': 'dist/images/misc/test.34546.png' + }), '/static/'); + var file = rf.find('images/misc/test.png', ['temp', 'dist']); + assert.equal(file, '/static/images/misc/test.34546.png'); + }); }); describe('absolute paths', function () { it('should return the corresponding file', function () { @@ -233,8 +239,14 @@ describe('RevvedFinder', function () { var file = rf.find('/images/misc/test.png', ['temp', 'dist']); assert.equal(file, '/images/misc/test.34546.png'); }); + it('should return the corresponding file prepended with a base URL', function () { + var rf = new RevvedFinder(helpers.normalize({ + 'dist/images/misc/test.png': 'dist/images/misc/test.34546.png' + }), '/static/'); + var file = rf.find('/images/misc/test.png', ['temp', 'dist']); + assert.equal(file, '/static/images/misc/test.34546.png'); + }); }); }); - }); });