diff --git a/index.js b/index.js index 6bab15c..df83830 100644 --- a/index.js +++ b/index.js @@ -10,10 +10,12 @@ var gutil = require('gulp-util'); var PluginError = gutil.PluginError; var path = require('path'); var defaults = require('lodash.defaults'); - +var md5 = require('md5'); module.exports = function(options) { - var filesMap = {}; + + var filesMap = {}, + filesHashes = {}; options = defaults(options || {}, { error: false, // Throw an error in case of duplicate. @@ -22,16 +24,32 @@ module.exports = function(options) { }); function bufferContents(file) { + if (file.isNull()) { return; } if (file.isStream()) { return this.emit('error', new PluginError('gulp-dedupe', 'Streaming not supported')); } var fullpath = path.resolve(file.path), + hash = md5(file._contents), + dupeType = null, + h, f; - if ((f = filesMap[fullpath])) { + if ((f = filesMap[fullpath]) || (h = filesHashes[hash])) { + + // fall back to hash lookup + if (!f && h) { + dupeType = 'hash'; + f = h; + } + else { + dupeType = 'path'; + } + if (options.error) { - this.emit('error', new PluginError('gulp-dedupe', 'Duplicate `' + file.path + '`')); - } else if (options.same && file.contents.toString() !== f.contents.toString()) { + this.emit('error', new PluginError('gulp-dedupe', 'Duplicate `' + file.path + '` has same ' + dupeType + ' as `' + f.path + '`')); + } + + else if (options.same && file.contents.toString() !== f.contents.toString()) { var errorDiff = []; if (options.diff) { @@ -53,10 +71,15 @@ module.exports = function(options) { this.emit('error', new PluginError('gulp-dedupe', 'Duplicate file `' + file.path + '` with different contents' + errorDiff)); } + return; - } else { + + } + else { filesMap[fullpath] = file; + filesHashes[hash] = file; } + this.emit('data', file); } diff --git a/package.json b/package.json index 746f54f..96583d1 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "through": "~2.3.6", "lodash.defaults": "~2.4.1", "colors": "~1.0.2", - "diff": "~1.0.8" + "diff": "~1.0.8", + "md5": "~2.2.1" }, "devDependencies": { "mocha": "*", diff --git a/test/main.js b/test/main.js index 7d6c14e..ed9a9a7 100644 --- a/test/main.js +++ b/test/main.js @@ -28,7 +28,6 @@ describe('gulp-dedupe', function() { 'file2.txt', 'Contents2', 'file3.txt', 'Contents3', 'file4.txt', 'Contents4', - 'test/file1.txt', 'Contents1' ] ); @@ -82,8 +81,10 @@ describe('gulp-dedupe', function() { it('should dedupe files', function(done) { stream.on('data', function (file) { + var expectedFilename = results.shift(), expectedHead = results.shift(); + should.exist(file); should.exist(file.relative); should.exist(file.contents);