Skip to content
This repository was archived by the owner on Jul 18, 2026. It is now read-only.
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
35 changes: 29 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
3 changes: 2 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('gulp-dedupe', function() {
'file2.txt', 'Contents2',
'file3.txt', 'Contents3',
'file4.txt', 'Contents4',
'test/file1.txt', 'Contents1'
]
);

Expand Down Expand Up @@ -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);
Expand Down