-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
58 lines (51 loc) · 1.42 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
// react-native start --transformer ./smTransform.js
var transformer = require('react-native/packager/transformer');
var fs = require("fs");
var sourceMap = require("source-map");
var path = require('path');
const lc = (str) => {
let c = 0
for (let ch of str) {
if (ch === "\n") c++;
}
return c
}
// line to line mapping of sourcemap
const smOneToOne = (srcfile, src) => {
"use strict";
let map = new sourceMap.SourceMapGenerator({file: srcfile})
let n = lc(src)
for (let line = 1; line <= n + 1; line++) {
map.addMapping({
source: srcfile,
original: {line, column: 0},
generated: {line, column: 0}
})
}
return map.toJSON()
}
module.exports = function (data, callback) {
let smfile = data.filename + ".map";
if (fs.existsSync(smfile)) {
transformer(data, (err, mod) => { // eslint-disable-line
"use strict";
if (mod) {
var smap = JSON.parse(fs.readFileSync(smfile).toString());
// Use absolute paths so further transformations would be able to resolve original files
smap.sources = smap.sources.map(source => {
return path.join(path.dirname(smfile), source);
});
mod.map = smap;
}
callback(null, mod);
})
} else {
transformer(data, (err, mod) => {
"use strict";
if (mod && !mod.map) {
mod.map = smOneToOne(mod.filename, mod.code)
}
return callback(err, mod);
});
}
};