forked from lukeed/taskr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (30 loc) · 1015 Bytes
/
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
'use strict';
const clean = require('flow-remove-types');
module.exports = function (task) {
task.plugin('unflow', { every:false }, function * (files, opts) {
opts = Object.assign({ pretty:true, all:true }, opts);
const type = opts.sourceMap || false;
const maps = [];
files.forEach(file => {
const out = clean(file.data.toString(), opts);
file.data = Buffer.from(out.toString());
const mdata = Buffer.from(type ? JSON.stringify(out.generateMap()) : '');
// handle sourcemaps
if (type === 'inline') {
file.data += Buffer.from(`\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${mdata.toString('base64')}`);
} else if (type === 'external') {
const mfile = `${file.base}.map`;
file.data += Buffer.from(`\n//# sourceMappingURL=${mfile}`);
// push an external sourcemap to output
maps.push({
dir: file.dir,
base: mfile,
data: mdata
});
}
});
if (maps.length > 0) {
this._.files = this._.files.concat(maps);
}
});
};