Skip to content

Commit

Permalink
Add shallowCompare option
Browse files Browse the repository at this point in the history
passing {shallowCompare: 2} would match a manifest entry:
	{/var/www/css/style.css: css/style-12345.css}
to a file reference:
	href="../css/style.css"
  • Loading branch information
madbarron committed Apr 7, 2015
1 parent 24ed932 commit ed29282
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ Type: `Stream` (e.g., `gulp.src()`)
Read JSON manifests written out by `rev`. Allows replacing filenames that were
`rev`ed prior to the current task.

#### options.shallowCompare
Type: `int`

Only match manifest paths using the rightmost `shallowCompare` levels.
e.g. `shallowCompare: 2` would match `../css/unicorn.css` to `/var/www/css/unicorn.css`

## Contributors

- Chad Jablonski
Expand Down
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function plugin(options) {
var manifest = JSON.parse(file.contents.toString());
Object.keys(manifest).forEach(function (srcFile) {
renames.push({
unreved: canonicalizeUri(srcFile),
reved: options.prefix + canonicalizeUri(manifest[srcFile])
unreved: canonicalizeUri(shallowify(srcFile)),
reved: options.prefix + canonicalizeUri(shallowify(manifest[srcFile]))
});
});
});
Expand Down Expand Up @@ -99,6 +99,19 @@ function plugin(options) {
return canonicalizeUri(newPath);
}

// Only look at the last few parts of a path
function shallowify(filePath) {
if (options.shallowCompare) {

var separator = utils.getPathSeparator(filePath);

return filePath.split(separator)
.slice(-options.shallowCompare)
.join(separator);
}
return filePath;
}

function canonicalizeUri(filePath) {
if (path.sep !== '/' && options.canonicalUris) {
filePath = filePath.split(path.sep).join('/');
Expand Down
53 changes: 53 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,59 @@ describe('manifest option', function () {

stream.end();
});

it('should only check top shallowCompare levels', function (cb) {
var manifest = es.readArray([
new gutil.File({
path: '/project/rev-manifest.json',
contents: new Buffer(JSON.stringify({
'/var/www/project/css/style.css': '/css/style-12345.css'
}))
})
]);

var stream = revReplace({manifest: manifest, shallowCompare: 2});

var replacedCSSFilePattern = /"\/css\/style-12345\.css"/;
stream.on('data', function(file) {
var contents = file.contents.toString();

var extension = path.extname(file.path);
if (extension === '.html') {
assert(
replacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should be replaced'
);
}
});
stream.on('end', function() {
cb();
});

stream.write(new gutil.File({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));

stream.end();
});
});

describe('utils.getPathSeparator', function() {
it('should return \\ for backslashy paths', function() {
assert('\\' === utils.getPathSeparator('c:\\wamp\\www\\css\\style.css'),
'Backslash path should be detected');
});

it('should return / for foreslashy paths', function() {
assert('/' === utils.getPathSeparator('/css/style.css'),
'forwardslash paths should be detected');
});

it('should return a reasonable default if separator cannot be determined', function() {
assert('/' === utils.getPathSeparator('style.css'),
'Default value should be returned');
});
});

describe('utils.byLongestUnreved', function() {
Expand Down
11 changes: 10 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ function byLongestUnreved(a, b) {
return b.unreved.length - a.unreved.length;
}

function getPathSeparator(filePath) {
if ((filePath.match(/\\/g) || []).length > (filePath.match(/\//g) || []).length) {
return '\\';
}

return '/';
}

module.exports = {
byLongestUnreved: byLongestUnreved
byLongestUnreved: byLongestUnreved,
getPathSeparator: getPathSeparator
};

0 comments on commit ed29282

Please sign in to comment.