diff --git a/README.md b/README.md index a928ae1..af359eb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 7d22e68..1783a3a 100644 --- a/index.js +++ b/index.js @@ -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])) }); }); }); @@ -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('/'); diff --git a/test.js b/test.js index 06761ab..3c59550 100644 --- a/test.js +++ b/test.js @@ -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() { diff --git a/utils.js b/utils.js index 50ae6da..86da06b 100644 --- a/utils.js +++ b/utils.js @@ -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 };