forked from sindresorhus/gulp-traceur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (53 loc) · 1.43 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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var traceur = require('traceur');
var applySourcemap = require('vinyl-sourcemaps-apply');
module.exports = function (options) {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-traceur', 'Streaming not supported'));
return cb();
}
var ret;
options = options || {};
options.filename = path.basename(file.path);
if(file.sourceMap) {
options.sourceMap = true;
}
try {
ret = traceur.compile(file.contents.toString(), options);
if (ret.js) {
if (ret.sourceMap && !file.sourceMap) {
ret.js += '\n//# sourceMappingURL=' + options.filename + '.map';
}
file.contents = new Buffer(ret.js);
}
if (ret.sourceMap) {
if(file.sourceMap) {
applySourcemap(ret.sourceMap);
} else {
this.push(new gutil.File({
cwd: file.cwd,
base: file.base,
path: file.path + '.map',
contents: new Buffer(ret.sourceMap)
}));
}
}
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-traceur', err));
}
if (ret.errors.length > 0) {
this.emit('error', new gutil.PluginError('gulp-traceur', '\n' + ret.errors.join('\n')));
}
this.push(file);
cb();
});
};
module.exports.RUNTIME_PATH = traceur.RUNTIME_PATH;