-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
201 lines (160 loc) · 4.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var path = require("path");
var findRoot = require("find-root");
var chalk = require("chalk");
var _ = require("lodash");
var semver = require("semver");
const defaults = {
verbose: false,
showHelp: true,
emitError: false,
exclude: null,
strict: true
};
function DuplicatePackageCheckerPlugin(options) {
this.options = _.extend({}, defaults, options);
}
function cleanPath(path) {
return path.split(/[\/\\]node_modules[\/\\]/).join("/~/");
}
// Get closest package definition from path
function getClosestPackage(modulePath) {
let root;
let pkg;
// Catch findRoot or require errors
try {
root = findRoot(modulePath);
pkg = require(path.join(root, "package.json"));
} catch (e) {
return null;
}
// If the package.json does not have a name property, try again from
// one level higher.
// https://github.com/jsdnxx/find-root/issues/2
// https://github.com/date-fns/date-fns/issues/264#issuecomment-265128399
if (!pkg.name) {
return getClosestPackage(path.resolve(root, ".."));
}
return {
package: pkg,
path: root
};
}
DuplicatePackageCheckerPlugin.prototype.apply = function(compiler) {
let verbose = this.options.verbose;
let showHelp = this.options.showHelp;
let emitError = this.options.emitError;
let exclude = this.options.exclude;
let strict = this.options.strict;
compiler.hooks.emit.tapAsync("DuplicatePackageCheckerPlugin", function(
compilation,
callback
) {
let context = compilation.compiler.context;
let modules = {};
function cleanPathRelativeToContext(modulePath) {
let cleanedPath = cleanPath(modulePath);
// Make relative to compilation context
if (cleanedPath.indexOf(context) === 0) {
cleanedPath = "." + cleanedPath.replace(context, "");
}
return cleanedPath;
}
compilation.modules.forEach(module => {
if (!module.resource) {
return;
}
let pkg;
let packagePath;
let closestPackage = getClosestPackage(module.resource);
// Skip module if no closest package is found
if (!closestPackage) {
return;
}
pkg = closestPackage.package;
packagePath = closestPackage.path;
let modulePath = cleanPathRelativeToContext(packagePath);
let version = pkg.version;
modules[pkg.name] = modules[pkg.name] || [];
let isSeen = _.find(modules[pkg.name], module => {
return module.version === version;
});
if (!isSeen) {
let entry = { version, path: modulePath };
let issuer =
module.issuer && module.issuer.resource
? cleanPathRelativeToContext(module.issuer.resource)
: null;
entry.issuer = issuer;
modules[pkg.name].push(entry);
}
});
let duplicates = {};
for (let name in modules) {
const instances = modules[name];
if (instances.length <= 1) {
continue;
}
let filtered = instances;
if (!strict) {
filtered = [];
const groups = _.groupBy(instances, instance =>
semver.major(instance.version)
);
_.each(groups, group => {
if (group.length > 1) {
filtered = filtered.concat(group);
}
});
if (filtered.length <= 1) {
continue;
}
}
if (exclude) {
filtered = filtered.filter(instance => {
instance = Object.assign({ name }, instance);
return !exclude(instance);
});
if (filtered.length <= 1) {
continue;
}
}
duplicates[name] = filtered;
}
const duplicateCount = Object.keys(duplicates).length;
if (duplicateCount) {
let array = emitError ? compilation.errors : compilation.warnings;
let i = 0;
let sortedDuplicateKeys = Object.keys(duplicates).sort();
sortedDuplicateKeys.map(name => {
let instances = duplicates[name].sort(
(a, b) => (a.version < b.version ? -1 : 1)
);
let error =
name +
"\n" +
chalk.reset(" Multiple versions of ") +
chalk.green.bold(name) +
chalk.white(` found:\n`);
instances = instances.map(version => {
let str = `${chalk.green.bold(version.version)} ${chalk.white.bold(
version.path
)}`;
if (verbose && version.issuer) {
str += ` from ${chalk.white.bold(version.issuer)}`;
}
return str;
});
error += ` ${instances.join("\n ")}\n`;
// only on last warning
if (showHelp && ++i === duplicateCount) {
error += `\n${chalk.white.bold(
"Check how you can resolve duplicate packages: "
)}\nhttps://github.com/darrenscerri/duplicate-package-checker-webpack-plugin#resolving-duplicate-packages-in-your-bundle\n`;
}
array.push(new Error(error));
});
}
callback();
});
};
module.exports = DuplicatePackageCheckerPlugin;