-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (42 loc) · 1.73 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
const loaderUtils = require('loader-utils');
function validateInputAsArray(input) {
return input instanceof Array ? input.join('|') : [input].join('|');
}
function requireRegexp(moduleName) {
const moduleString = validateInputAsArray(moduleName);
return new RegExp(`.*?require\\(\\s*['"](\\./|(\\.\\./)*)?(${moduleString}|debug)['"]\\s*\\).*`, 'g');
}
function importRegexp(moduleName) {
const moduleString = validateInputAsArray(moduleName);
return new RegExp(`import\\s{?\\s*.+\\s*}?\\sfrom\\s['"](\\./|(\\.\\./)*)?(${moduleString}|debug)['"].*`, 'g');
}
function methodInvocationRegexp(methodName) {
const methodString = validateInputAsArray(methodName);
return new RegExp(`\\b(?:${methodString}|debug)\\(.*\\);?`, 'g');
}
function methodDefinitionRegexp(methodName) {
const methodString = validateInputAsArray(methodName);
return new RegExp(`\\b((?:var|const|let|,)\\s){0,1}(?:${methodString}|debug)\\s?=\\s?.*(,|;)?`, 'g');
}
function replaceWithRegexp(source, regexp) {
return source.replace(regexp, '');
}
function replaceWithRegexps(source, regexps) {
return regexps.reduce((prev, regexp) => replaceWithRegexp(prev, regexp), source);
}
exports = function loader(source) {
const options = loaderUtils.getOptions(this) || {};
const methodName = options.methodName || ['debug'];
const moduleName = options.moduleName || [''];
return replaceWithRegexps(source, [
requireRegexp(moduleName),
importRegexp(moduleName),
methodInvocationRegexp(methodName),
methodDefinitionRegexp(methodName),
]);
};
exports.methodInvocationRegexp = methodInvocationRegexp;
exports.methodDefinitionRegexp = methodDefinitionRegexp;
exports.requireRegexp = requireRegexp;
exports.importRegexp = importRegexp;
module.exports = exports;