-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathindex.js
95 lines (93 loc) · 3.02 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
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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var SourceMap = require("source-map");
var fs = require("fs");
var path = require("path");
var async = require("async");
var loaderUtils = require("loader-utils");
var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)",
// Matches /* ... */ comments
regex1 = new RegExp("/\\*"+baseRegex+"\\s*\\*/"),
// Matches // .... comments
regex2 = new RegExp("//"+baseRegex+"($|\n|\r\n?)"),
// Matches DataUrls
regexDataUrl = /data:[^;\n]+;base64,(.*)/;
module.exports = function(input, inputMap) {
this.cacheable && this.cacheable();
var resolve = this.resolve;
var addDependency = this.addDependency;
var emitWarning = this.emitWarning || function() {};
var match = input.match(regex1) || input.match(regex2);
if(match) {
var url = match[1];
var dataUrlMatch = regexDataUrl.exec(url);
var callback = this.async();
if(dataUrlMatch) {
processMap(JSON.parse((new Buffer(dataUrlMatch[1], "base64")).toString()), this.context, callback);
} else {
resolve(this.context, loaderUtils.urlToRequest(url), function(err, result) {
if(err) {
emitWarning("Cannot find SourceMap '" + url + "': " + err);
return untouched();
}
addDependency(result);
fs.readFile(result, "utf-8", function(err, content) {
if(err) {
emitWarning("Cannot open SourceMap '" + result + "': " + err);
return untouched();
}
processMap(JSON.parse(content), path.dirname(result), callback);
});
}.bind(this));
return;
}
} else {
var callback = this.callback;
return untouched();
}
function untouched() {
callback(null, input, inputMap);
}
function processMap(map, context, callback) {
if(!map.sourcesContent || map.sourcesContent.length < map.sources.length) {
var sourcePrefix = map.sourceRoot ? map.sourceRoot + "/" : "";
map.sources = map.sources.map(function(s) { return sourcePrefix + s; });
delete map.sourceRoot;
var missingSources = map.sourcesContent ? map.sources.slice(map.sourcesContent.length) : map.sources;
async.map(missingSources, function(source, callback) {
resolve(context, loaderUtils.urlToRequest(source), function(err, result) {
if(err) {
emitWarning("Cannot find source file '" + source + "': " + err);
return callback(null, null);
}
addDependency(result);
fs.readFile(result, "utf-8", function(err, content) {
if(err) {
emitWarning("Cannot open source file '" + result + "': " + err);
return callback(null, null);
}
callback(null, {
source: result,
content: content
});
});
});
}, function(err, info) {
map.sourcesContent = map.sourcesContent || [];
info.forEach(function(res) {
if(res) {
map.sources[map.sourcesContent.length] = res.source;
map.sourcesContent.push(res.content);
} else {
map.sourcesContent.push(null);
}
});
processMap(map, context, callback);
});
return;
}
callback(null, input.replace(match[0], ''), map);
}
}