-
Notifications
You must be signed in to change notification settings - Fork 183
/
babel-plugin.js
47 lines (39 loc) · 1.36 KB
/
babel-plugin.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
/* globals require, module, Buffer, console */
/* eslint-disable no-console, prefer-template */
var gutil = require('gulp-util');
var through = require('through2');
var applySourceMap = require('vinyl-sourcemaps-apply');
var objectAssign = require('object-assign');
var replaceExt = require('replace-ext');
var babel = require('babel-core');
module.exports = function(opts) {
opts = opts || {};
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new gutil.PluginError('gulp-babel',
'Streaming not supported'));
}
try {
var fileOpts = objectAssign({}, opts, {
filename: file.path,
filenameRelative: file.relative,
sourceMap: !!file.sourceMap,
});
var res = babel.transform(file.contents.toString(), fileOpts);
if (file.sourceMap && res.map) {
applySourceMap(file, res.map);
}
file.contents = new Buffer(res.code);
file.path = replaceExt(file.path, '.js');
this.push(file);
} catch (err) {
console.log('[Babel] Syntax Error: ' + file.path);
console.log(err.codeFrame);
this.emit('end');
}
cb();
});
};