Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ return gulp.src(opt.distFolder + '**/*.js')
.pipe(gulp.dest(opt.distFolder));
```

#### 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 @@ -101,6 +101,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 @@ -331,6 +331,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
};