forked from liady/webpack-node-externals
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
154 lines (144 loc) · 4.65 KB
/
utils.js
File metadata and controls
154 lines (144 loc) · 4.65 KB
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
var fs = require('fs');
var path = require('path');
var resolve = require('resolve');
exports.contains = function contains(arr, val) {
return arr && arr.indexOf(val) !== -1;
};
var atPrefix = new RegExp('^@', 'g');
exports.readDir = function readDir(dirName) {
if (!fs.existsSync(dirName)) {
return [];
}
try {
return fs
.readdirSync(dirName)
.map(function (module) {
if (atPrefix.test(module)) {
// reset regexp
atPrefix.lastIndex = 0;
try {
return fs
.readdirSync(path.join(dirName, module))
.map(function (scopedMod) {
return module + '/' + scopedMod;
});
} catch (e) {
return [module];
}
}
return module;
})
.reduce(function (prev, next) {
return prev.concat(next);
}, []);
} catch (e) {
return [];
}
};
exports.readFromPackageJson = function readFromPackageJson(options) {
if (typeof options !== 'object') {
options = {};
}
var includeInBundle = options.exclude || options.includeInBundle;
var excludeFromBundle = options.include || options.excludeFromBundle;
// read the file
var packageJson;
try {
var fileName = options.fileName || 'package.json';
var packageJsonString = fs.readFileSync(
path.resolve(process.cwd(), fileName),
'utf8'
);
packageJson = JSON.parse(packageJsonString);
} catch (e) {
return [];
}
// sections to search in package.json
var sections = [
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies',
];
if (excludeFromBundle) {
sections = [].concat(excludeFromBundle);
}
if (includeInBundle) {
sections = sections.filter(function (section) {
return [].concat(includeInBundle).indexOf(section) === -1;
});
}
// collect dependencies
var deps = {};
sections.forEach(function (section) {
Object.keys(packageJson[section] || {}).forEach(function (dep) {
deps[dep] = true;
});
});
return Object.keys(deps);
};
exports.containsPattern = function containsPattern(arr, val) {
return (
arr &&
arr.some(function (pattern) {
if (pattern instanceof RegExp) {
return pattern.test(val);
} else if (typeof pattern === 'function') {
return pattern(val);
} else {
return pattern == val;
}
})
);
};
exports.validateOptions = function (options) {
var results = [];
var mistakes = {
allowlist: ['allowslist', 'whitelist', 'allow'],
importType: ['import', 'importype', 'importtype'],
modulesDir: ['moduledir', 'moduledirs'],
modulesFromFile: ['modulesfile'],
includeAbsolutePaths: ['includeAbsolutesPaths'],
additionalModuleDirs: ['additionalModulesDirs', 'additionalModulesDir'],
};
var optionsKeys = Object.keys(options);
var optionsKeysLower = optionsKeys.map(function (optionName) {
return optionName && optionName.toLowerCase();
});
Object.keys(mistakes).forEach(function (correctTerm) {
if (options[correctTerm] === undefined) {
mistakes[correctTerm]
.concat(correctTerm.toLowerCase())
.forEach(function (mistake) {
var ind = optionsKeysLower.indexOf(mistake.toLowerCase());
if (ind > -1) {
results.push({
message: `Option '${optionsKeys[ind]}' is not supported. Did you mean '${correctTerm}'?`,
wrongTerm: optionsKeys[ind],
correctTerm: correctTerm,
});
}
});
}
});
return results;
};
exports.log = function (message) {
console.log(`[webpack-node-externals] : ${message}`);
};
exports.error = function (errors) {
throw new Error(
errors
.map(function (error) {
return `[webpack-node-externals] : ${error}`;
})
.join('\r\n')
);
};
exports.resolveRequest= function (req, issuer) {
var basedir =
issuer.endsWith(path.posix.sep) || issuer.endsWith(path.win32.sep)
? issuer
: path.dirname(issuer);
return resolve.sync(req, { basedir: basedir });
};