-
Notifications
You must be signed in to change notification settings - Fork 0
/
single-file-source-map.js
44 lines (33 loc) · 1.4 KB
/
single-file-source-map.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
var through = require('through2');
var fs = require("fs");
function removeSubPath(path){
var parts = path.split(/[\/\\]/);
return parts[parts.length - 1];
}
module.exports.afterUglify = function() {
return through.obj(function(file, encoding, callback) {
// Here we fix "?" in sources
var fileName = file.history[file.history.length - 1];
file.sourceMap.sources[0] = removeSubPath(fileName);
this.push(file);
callback();
});
};
module.exports.afterDest = function() {
return through.obj(function(file, encoding, callback) {
var fileName = file.history[file.history.length - 1];
if(fileName.substring(fileName.length - 4, fileName.length) == '.map') {
// Here we remove subpath for MAP-file
var mapFile = JSON.parse(fs.readFileSync(fileName, "utf8"));
mapFile.file = removeSubPath(mapFile.file);
fs.writeFile(fileName, JSON.stringify(mapFile), "utf8", callback);
}else if(fileName.substring(fileName.length - 7, fileName.length) == '.min.js') {
// Here we remove subpath for uglifyed file
var uglifyled = fs.readFileSync(fileName, "utf8");
var result = uglifyled.replace(/sourceMappingURL=(.+\/)?([^\/]+\.map)/, "sourceMappingURL=$2");
fs.writeFile(fileName, result, "utf8", callback);
}else{
callback();
}
});
};