-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (46 loc) · 1.76 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
var continuation = require('continuation');
var through = require('through2');
var sourceMap = require('source-map');
var SourceMapGenerator = sourceMap.SourceMapGenerator;
var SourceMapConsumer = sourceMap.SourceMapConsumer;
var convertSourceMap = require('convert-source-map');
module.exports = function continuatify(filename, options) {
var chunks = [];
function ondata(buffer, encoding, cb) {
chunks.push(buffer.toString('utf8'));
cb();
}
function onend(cb) {
try {
var code = chunks.join('');
var mapBefore = null;
if (options._flags.debug) {
mapBefore = convertSourceMap.fromSource(code);
if (mapBefore) {
mapBefore = mapBefore.toObject();
}
options.sourceMap = true;
}
var compiled = continuation.compile(code, options);
if (options._flags.debug) {
var file = filename;
if (mapBefore) {
file = mapBefore.file;
}
var continuationMap = continuation.getSourceMap(file, [file]);
var resultMap = convertSourceMap.fromJSON(continuationMap);
if (mapBefore) {
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(JSON.parse(continuationMap)));
generator.applySourceMap(new SourceMapConsumer(mapBefore));
resultMap = convertSourceMap.fromJSON(generator.toString());
}
compiled += "\n" + resultMap.toComment();
}
this.push(compiled);
cb();
} catch (e) {
return cb(e);
}
}
return through(ondata, onend);
};