Skip to content

Instantly share code, notes, and snippets.

@nojaf
Last active October 15, 2015 18:41
Show Gist options
  • Save nojaf/1cf593a6cfff5d648cf2 to your computer and use it in GitHub Desktop.
Save nojaf/1cf593a6cfff5d648cf2 to your computer and use it in GitHub Desktop.
React ES6 gulpfile
var gulp = require("gulp"); // Good old gulp
var transform = require("vinyl-transform"); // Transform browserify stream
var source = require("vinyl-source-stream"); // Output for bundles
var browserify = require("browserify"); // Bundling tool
var babelify = require("babelify"); // Compile JSX and ES2015
var _ = require("lodash"); // Used to package.json keys
var connect = require("gulp-connect"); // Runs a local dev server
var open = require("gulp-open"); // Open a URL in a web browser
gulp.task("connect", function() {
connect.server({
root: ["dist"],
port: 8080,
base: "http://localhost",
livereload: true
});
});
gulp.task("open", ["connect"], function() {
gulp.src("dist/index.html")
.pipe(open({
uri: "http://localhost:8080/",
"app": "chrome"
}));
});
gulp.task("copy", function() {
var c = gulp.src("src/index.html")
.pipe(gulp.dest("dist/"));
reload(c);
});
gulp.task("vendor", function() {
var npmPackages = getNPMPackages();
return browserify()
.require(npmPackages)
.bundle()
.on("error", handleError)
.pipe(source("libs.js"))
.pipe(gulp.dest("./dist/js/"));
});
gulp.task("bundle", function() {
var npmPackages = getNPMPackages();
var b = browserify("./src/js/main.js")
.external(npmPackages)
.transform(babelify)
.bundle()
.on("error", handleError)
.pipe(source("main.js"))
.pipe(gulp.dest("./dist/js/"));
reload(b);
});
gulp.task("default", ["vendor", "copy", "bundle"]);
gulp.task("watch", ["default", "open"], function() {
gulp.watch("src/**/*.js", ["bundle"]);
gulp.watch("src/*.html", ["copy"]);
});
// Helper functions
function getNPMPackages() {
var packageManifest = {};
try {
packageManifest = require("./package.json");
} catch (e) {}
return _.keys(packageManifest.dependencies) || [];
}
function handleError() {
var args = Array.prototype.slice.call(arguments);
console.log("errors: ", args);
this.emit("end");
}
function reload(stream) {
if (connect && connect.reload) {
stream.pipe(connect.reload());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment