diff --git a/cli/tasks/usemin.js b/cli/tasks/usemin.js index db0e47f4..f8c73c3b 100644 --- a/cli/tasks/usemin.js +++ b/cli/tasks/usemin.js @@ -171,7 +171,7 @@ module.exports = function(grunt) { // actual replacement of revved assets if(!!grunt.task._helpers['usemin:post:' + name]) { - content = grunt.helper('usemin:post:' + name, content); + content = grunt.helper('usemin:post:' + name, content, p); } // write the new content to disk @@ -350,10 +350,10 @@ module.exports = function(grunt) { return content; }); - grunt.registerHelper('usemin:post:css', function(content) { + grunt.registerHelper('usemin:post:css', function(content, srcPath) { grunt.log.verbose.writeln('Update the CSS with background imgs, case there is some inline style'); - content = grunt.helper('replace', content, /url\(\s*['"]?([^'"\)]+)['"]?\s*\)/gm); + content = grunt.helper('replace', content, /url\(\s*['"]?([^'"\)]+)['"]?\s*\)/gm, srcPath); return content; }); @@ -363,7 +363,7 @@ module.exports = function(grunt) { // regexp should capture the assets relative filepath, it is then compared to // the list of files on the filesystem to guess the actual revision of a file // - grunt.registerHelper('replace', function(content, regexp) { + grunt.registerHelper('replace', function(content, regexp, srcPath) { return content.replace(regexp, function(match, src) { //do not touch external files or the root if ( src.match(/\/\//) || src.match(/^\/$/)) { @@ -377,12 +377,13 @@ module.exports = function(grunt) { var basename = path.basename(src); var dirname = path.dirname(src); + var normalizedDirname = path.normalize([path.dirname(srcPath), dirname].join('/')); // XXX files won't change, the filepath should filter the original list // of cached files (we need to treat the filename collision -- i.e. 2 files with same names // in different subdirectories) var filepaths = grunt.file.expand(path.join('**/*') + basename); - var filepath = filepaths.filter(function(f) { return dirname === path.dirname(f);})[0]; + var filepath = filepaths.filter(function(f) { return normalizedDirname === path.dirname(f); })[0]; // not a file in temp, skip it if ( !filepath ) { diff --git a/cli/test/test-usemin.js b/cli/test/test-usemin.js index d167d4f8..f073a62c 100644 --- a/cli/test/test-usemin.js +++ b/cli/test/test-usemin.js @@ -16,7 +16,7 @@ describe('usemin', function() { it("should take into account path", function() { usemin.call(grunt,grunt); - // Let's prepare our context: in index.html we do have references + // Let's prepare our context: in index.html we do have references // to images/test.png and images/misc/test.png. // Usemin's replace is supposed to change this by files // found on the filesystem matching the same path, but @@ -48,6 +48,15 @@ describe('usemin', function() { }); }); + describe('usemin:post:css', function() { + it("should replace img urls in css file", function() { + var content = 'url(../images/test.png)'; + var awaited = 'url(../images/23012.test.png)'; + var changed = grunt.helper('usemin:post:css', content, 'css/23012.main.css'); + assert.ok( changed == awaited ); + }); + }); + describe('usemin:post:html', function() { before(function() { usemin.call(grunt,grunt); @@ -55,7 +64,7 @@ describe('usemin', function() { grunt.file.mkdir('images'); grunt.file.mkdir('css'); }); - + it('should skip external file', function() { grunt.file.write('images/23012.foo.png', "foo"); var content = '';