babel/Gulpfile.js
Henry Zhu 4278ede41f 🐶 🍲
2017-03-27 14:24:16 -04:00

79 lines
2.1 KiB
JavaScript

"use strict";
const plumber = require("gulp-plumber");
const through = require("through2");
const chalk = require("chalk");
const newer = require("gulp-newer");
const babel = require("gulp-babel");
const watch = require("gulp-watch");
const gutil = require("gulp-util");
const gulp = require("gulp");
const path = require("path");
const babelConfig = require("./.babelrc");
const scripts = "./packages/*/src/**/*.js";
let srcEx, libFragment;
if (path.win32 === path) {
srcEx = /(packages\\[^\\]+)\\src\\/;
libFragment = "$1\\lib\\";
} else {
srcEx = new RegExp("(packages/[^/]+)/src/");
libFragment = "$1/lib/";
}
const mapToDest = function (path) { return path.replace(srcEx, libFragment); };
const dest = "packages";
gulp.task("default", ["build"]);
gulp.task("build", function () {
return gulp.src(scripts)
.pipe(plumber({
errorHandler: function (err) {
gutil.log(err.stack);
},
}))
.pipe(newer({ map: mapToDest }))
.pipe(through.obj(function (file, enc, callback) {
gutil.log("Compiling", "'" + chalk.cyan(file.path) + "'...");
callback(null, file);
}))
.pipe(babel(babelConfig))
.pipe(through.obj(function (file, enc, callback) {
file._path = file.path;
file.path = mapToDest(file.path);
callback(null, file);
}))
.pipe(gulp.dest(dest));
});
// TODO: remove this section
// temporarily just copying the old code since watch isn't working
gulp.task("build-watch", function () {
return gulp.src(scripts)
.pipe(plumber({
errorHandler: function (err) {
gutil.log(err.stack);
},
}))
.pipe(through.obj(function (file, enc, callback) {
file._path = file.path;
file.path = file.path.replace(srcEx, libFragment);
callback(null, file);
}))
.pipe(newer(dest))
.pipe(through.obj(function (file, enc, callback) {
gutil.log("Compiling", "'" + chalk.cyan(file._path) + "'...");
callback(null, file);
}))
.pipe(babel(babelConfig))
.pipe(gulp.dest(dest));
});
gulp.task("watch", ["build-watch"], function () {
watch(scripts, { debounceDelay: 200 }, function () {
gulp.start("build-watch");
});
});