-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (33 loc) · 1.2 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Dougal Graham @dougalg
*/
var loaderUtils = require("loader-utils");
var SourceNode = require("source-map").SourceNode;
var SourceMapConsumer = require("source-map").SourceMapConsumer;
var HEADER = "\n/*** WRAPPING FROM logger-loader ***/\n";
module.exports = function(content, sourceMap) {
if(this.cacheable) this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var loggerModule = JSON.stringify(query.module);
var loggerModuleName = JSON.stringify(query.importName) || 'wpLogError';
if (loggerModule) {
var prefix = "try {\n";
var postfix = "\n} catch (e) {\n\tvar "+ loggerModuleName +" = require("+ loggerModule +");\n\t"+ loggerModuleName +"(e);\n}";
if(sourceMap) {
var currentRequest = loaderUtils.getCurrentRequest(this);
var node = SourceNode.fromStringWithSourceMap(content, new SourceMapConsumer(sourceMap));
node.prepend(prefix);
node.add(postfix);
var result = node.toStringWithSourceMap({
file: currentRequest
});
this.callback(null, result.code, result.map.toJSON());
return;
}
return prefix + content + postfix;
}
else {
return content;
}
}