Skip to content

Commit

Permalink
Fix issue #643: yeoman build not updating revved image references in css
Browse files Browse the repository at this point in the history
  • Loading branch information
roparz committed Dec 3, 2012
1 parent ca0f0da commit e862a58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 6 additions & 5 deletions cli/tasks/usemin.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;
});
Expand All @@ -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(/^\/$/)) {
Expand All @@ -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 ) {
Expand Down
13 changes: 11 additions & 2 deletions cli/test/test-usemin.js
Expand Up @@ -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
Expand Down Expand Up @@ -48,14 +48,23 @@ 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);
grunt.log.muted = true;
grunt.file.mkdir('images');
grunt.file.mkdir('css');
});

it('should skip external file', function() {
grunt.file.write('images/23012.foo.png', "foo");
var content = '<img src="//css/main.css">';
Expand Down

0 comments on commit e862a58

Please sign in to comment.