-
Notifications
You must be signed in to change notification settings - Fork 0
/
.pnpmfile.cjs
57 lines (50 loc) · 1.63 KB
/
.pnpmfile.cjs
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
const DEPENDENCY_TYPES = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies'];
const REMOVE_DEPENDENCIES = ['playwright', 'safaridriver', 'webdriverio'];
function readPackage(pkg, _) {
if (process.env.NODE_ENV === 'production') {
const searchResults = searchAllDependencies(pkg);
if (searchResults.hasDep) {
searchResults.results.forEach((pkgDeps, depType) => {
pkgDeps.forEach(([depName, _]) => (pkg[depType] = removeDepFromPackage(pkg, depName, depType)));
});
}
}
return pkg;
}
function searchAllDependencies(pkg, depName) {
const pkgDepTypeMap = {
dependencies: pkg.dependencies,
devDependencies: pkg.devDependencies,
optionalDependencies: pkg.optionalDependencies,
peerDependencies: pkg.peerDependencies,
};
let hasDep = false;
const results = new Map();
DEPENDENCY_TYPES.forEach((type) => {
REMOVE_DEPENDENCIES.forEach((depName) => {
const resultsForDepType = searchDependencies(depName, pkgDepTypeMap[type], type);
if (resultsForDepType.length > 0) {
hasDep = true;
results.set(type, resultsForDepType);
}
});
});
return { name: pkg.name, depName, hasDep, results };
}
function searchDependencies(searchDep, dependencies, type) {
return Object.keys(dependencies)
.filter((name) => name.includes(searchDep))
.map((name) => [name, type]);
}
function removeDepFromPackage(pkg, depName, depType) {
const dependencies = JSON.parse(JSON.stringify(pkg[depType]));
if (dependencies[depName]) {
delete dependencies[depName];
}
return dependencies;
}
module.exports = {
hooks: {
readPackage,
},
};