-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder-es6.js
72 lines (66 loc) · 2.43 KB
/
builder-es6.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
66
67
68
69
70
71
72
/**
* @file ES6 build helpers.
* @copyright Ben Mackie 2015
* @license MIT
*/
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var babel = require('gulp-babel');
var traceur = require('gulp-traceur');
var rename = require('gulp-rename');
var _ = require('underscore');
var plumber = require('gulp-plumber');
var debug = require('./depfile').debug('es6');
module.exports = function(builder, util) {
var _this = builder;
var _util = util;
var defaultTranspileOptions = {
transpiler: 'traceur',
recursive : true,
sourceExtension: 'es6'
}
_this.transpileOptions = function(options) {
defaultTranspileOptions = _.extend(defaultTranspileOptions, options);
return _this;
}
_this.transpile = function(sourceDir, targetDir, options) {
// Validate options.
var opt = _.extend(defaultTranspileOptions, options);
if (!sourceDir)
throw new Error('Invalid source directory.');
if (!targetDir)
targetDir = sourceDir;
// Calc full source path.
var sourcePath = sourceDir;
if (opt.recursive)
sourcePath += '/**';
sourcePath += '/*.' + opt.sourceExtension;
// Do transpilation
switch (opt.transpiler) {
case 'babel':
return _util.addStream(function(resolve, reject) {
debug('start babel compile ' + sourcePath);
return gulp.src(sourcePath)
.pipe(plumber(reject))
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(rename({extname: ".js"}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(targetDir));
});
case 'traceur':
return _util.addStream(function(resolve, reject) {
debug('start traceur compile ' + sourcePath);
return gulp.src(sourcePath)
.pipe(plumber(reject))
.pipe(sourcemaps.init())
.pipe(traceur({sourceMaps:true}))
.pipe(rename({extname: ".js"}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(targetDir));
});
default:
throw new Error('Unknown transpiler: ' + opt.transpiler);
}
}
}