-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLogLoader.js
More file actions
31 lines (26 loc) · 973 Bytes
/
LogLoader.js
File metadata and controls
31 lines (26 loc) · 973 Bytes
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
module.exports = function (source, map) {
const separated = source.split('\n');
const fileName = getFileNameByPath(this.currentRequest);
for (let i = 0; i < separated.length; i++) {
const line = separated[i];
const match = line.match(/(\.log\(|\.info\(|\.debug\(|\.warn\(|\.error\()/g);
if (match) {
match.forEach(item => {
separated[i] = separated[i].replace(item, `${item}\'${fileName}:${i + 1}\ ' + `);
});
}
}
const toSource = separated.join('\n');
this.callback(null, toSource, map);
};
const getFileNameByPath = (path) => {
if (!path || path.length < 1) {
return path
}
const normalizedPath = path.replace(/\\/gi, '/');
const lastIndex = normalizedPath.lastIndexOf('/');
if (lastIndex > -1 && lastIndex + 1 < normalizedPath.length) {
return normalizedPath.substring(lastIndex + 1, normalizedPath.length)
}
return path;
};