2014-09-13 15:00:05 +00:00
|
|
|
var gulp = require("gulp");
|
2014-12-30 08:51:07 +00:00
|
|
|
var path = require('path');
|
|
|
|
var _ = require('lodash');
|
2014-09-13 15:00:05 +00:00
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
var browserify = require('browserify');
|
2014-09-13 15:00:05 +00:00
|
|
|
var concat = require('gulp-concat');
|
2014-12-30 08:51:07 +00:00
|
|
|
var connect = require('gulp-connect');
|
|
|
|
var buffer = require('vinyl-buffer');
|
2014-09-13 15:00:05 +00:00
|
|
|
var jshint = require("gulp-jshint");
|
|
|
|
var less = require("gulp-less");
|
|
|
|
var livereload = require("gulp-livereload");
|
2014-12-30 08:51:07 +00:00
|
|
|
var map = require("map-stream");
|
2014-09-13 15:00:05 +00:00
|
|
|
var minifyCSS = require('gulp-minify-css');
|
2014-09-13 23:21:13 +00:00
|
|
|
var notify = require("gulp-notify");
|
2014-09-13 21:05:12 +00:00
|
|
|
var plumber = require("gulp-plumber");
|
2014-12-30 08:51:07 +00:00
|
|
|
var rev = require("gulp-rev");
|
2014-09-13 15:00:05 +00:00
|
|
|
var react = require("gulp-react");
|
2014-12-30 08:51:07 +00:00
|
|
|
var reactify = require('reactify');
|
2014-09-17 19:14:55 +00:00
|
|
|
var rename = require("gulp-rename");
|
2014-12-30 08:51:07 +00:00
|
|
|
var replace = require('gulp-replace');
|
|
|
|
var source = require('vinyl-source-stream');
|
2014-09-13 15:00:05 +00:00
|
|
|
var sourcemaps = require('gulp-sourcemaps');
|
2014-12-30 08:51:07 +00:00
|
|
|
var transform = require('vinyl-transform');
|
2014-09-13 15:00:05 +00:00
|
|
|
var uglify = require('gulp-uglify');
|
2015-01-02 22:46:51 +00:00
|
|
|
var peg = require("gulp-peg");
|
|
|
|
var filelog = require('gulp-filelog');
|
2014-09-13 15:00:05 +00:00
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
var packagejs = require('./package.json');
|
2015-01-02 22:46:51 +00:00
|
|
|
var conf = require('./conf.js');
|
2014-09-13 15:00:05 +00:00
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
|
|
|
|
// FIXME: react-with-addons.min.js for prod use issue
|
|
|
|
|
|
|
|
var manifest = {
|
|
|
|
"vendor.css": "vendor.css",
|
|
|
|
"app.css": "app.css",
|
|
|
|
"vendor.js": "vendor.js",
|
|
|
|
"app.js": "app.js",
|
2014-09-13 23:21:13 +00:00
|
|
|
};
|
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
var vendor_packages = _.difference(
|
2015-01-02 22:46:51 +00:00
|
|
|
_.union(
|
|
|
|
_.keys(packagejs.dependencies),
|
|
|
|
conf.js.vendor_includes
|
|
|
|
),
|
|
|
|
conf.js.vendor_excludes
|
|
|
|
);
|
2014-12-30 08:51:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Custom linting reporter used for error notify
|
|
|
|
var jsHintErrorReporter = function(){
|
|
|
|
return map(function (file, cb) {
|
|
|
|
if (file.jshint && !file.jshint.success) {
|
|
|
|
file.jshint.results.forEach(function (err) {
|
|
|
|
if (err) {
|
|
|
|
var msg = [
|
|
|
|
path.basename(file.path),
|
|
|
|
'Line: ' + err.error.line,
|
|
|
|
'Reason: ' + err.error.reason
|
|
|
|
];
|
|
|
|
notify.onError(
|
|
|
|
"Error: <%= error.message %>"
|
|
|
|
)(new Error(msg.join("\n")));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cb(null, file);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
var save_rev = function(){
|
|
|
|
return map(function(file, callback){
|
|
|
|
if (file.revOrigBase){
|
|
|
|
manifest[path.basename(file.revOrigPath)] = path.basename(file.path);
|
|
|
|
}
|
|
|
|
callback(null, file);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var dont_break_on_errors = function(){
|
|
|
|
return plumber(
|
|
|
|
function(error){
|
|
|
|
notify.onError("Error: <%= error.message %>").apply(this, arguments);
|
|
|
|
this.emit('end');
|
|
|
|
}
|
|
|
|
);
|
2014-09-13 15:00:05 +00:00
|
|
|
};
|
2014-09-16 09:41:49 +00:00
|
|
|
|
|
|
|
|
2014-09-13 15:00:05 +00:00
|
|
|
gulp.task("fonts", function () {
|
2015-01-02 22:46:51 +00:00
|
|
|
return gulp.src(conf.fonts)
|
|
|
|
.pipe(gulp.dest(conf.dist + "fonts"));
|
2014-09-13 15:00:05 +00:00
|
|
|
});
|
|
|
|
|
2014-09-16 09:41:49 +00:00
|
|
|
|
|
|
|
function styles_dev(files) {
|
2014-12-30 08:51:07 +00:00
|
|
|
return (gulp.src(files)
|
2014-09-16 09:41:49 +00:00
|
|
|
.pipe(dont_break_on_errors())
|
|
|
|
.pipe(sourcemaps.init())
|
|
|
|
.pipe(less())
|
|
|
|
.pipe(sourcemaps.write(".", {sourceRoot: "/static"}))
|
2015-01-02 22:46:51 +00:00
|
|
|
.pipe(gulp.dest(conf.static))
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(livereload({ auto: false })));
|
2014-09-16 09:41:49 +00:00
|
|
|
}
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.task("styles-app-dev", function(){
|
2015-01-02 22:46:51 +00:00
|
|
|
styles_dev(conf.css.app);
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
|
|
|
gulp.task("styles-vendor-dev", function(){
|
2015-01-02 22:46:51 +00:00
|
|
|
styles_dev(conf.css.vendor);
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
2014-09-16 09:41:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
function styles_prod(files) {
|
2014-12-30 08:51:07 +00:00
|
|
|
return (gulp.src(files)
|
2014-09-13 15:00:05 +00:00
|
|
|
.pipe(less())
|
|
|
|
// No sourcemaps support yet :-/
|
|
|
|
// https://github.com/jonathanepollack/gulp-minify-css/issues/34
|
2014-09-16 09:41:49 +00:00
|
|
|
.pipe(minifyCSS())
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(rev())
|
|
|
|
.pipe(save_rev())
|
2015-01-02 22:46:51 +00:00
|
|
|
.pipe(gulp.dest(conf.static))
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(livereload({ auto: false })));
|
2014-09-13 15:00:05 +00:00
|
|
|
}
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.task("styles-app-prod", function(){
|
2015-01-02 22:46:51 +00:00
|
|
|
styles_prod(conf.css.app);
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
|
|
|
gulp.task("styles-vendor-prod", function(){
|
2015-01-02 22:46:51 +00:00
|
|
|
styles_prod(conf.css.vendor);
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
2014-09-13 15:00:05 +00:00
|
|
|
|
2014-09-16 09:41:49 +00:00
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
function vendor_stream(debug){
|
|
|
|
var vendor = browserify(vendor_packages, {debug: debug});
|
|
|
|
_.each(vendor_packages, function(v){
|
|
|
|
vendor.require(v);
|
|
|
|
});
|
|
|
|
return vendor.bundle()
|
|
|
|
.pipe(source("dummy.js"))
|
|
|
|
.pipe(rename("vendor.js"));
|
2014-09-13 15:00:05 +00:00
|
|
|
}
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.task("scripts-vendor-dev", function (){
|
|
|
|
return vendor_stream(true)
|
2015-01-02 22:46:51 +00:00
|
|
|
.pipe(gulp.dest(conf.static));
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
|
|
|
gulp.task("scripts-vendor-prod", function(){
|
|
|
|
return vendor_stream(false)
|
|
|
|
.pipe(buffer())
|
|
|
|
.pipe(uglify())
|
|
|
|
.pipe(rev())
|
|
|
|
.pipe(save_rev())
|
2015-01-02 22:46:51 +00:00
|
|
|
.pipe(gulp.dest(conf.static));
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
2014-09-16 09:41:49 +00:00
|
|
|
|
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
function app_stream(debug) {
|
|
|
|
var browserified = transform(function(filename) {
|
|
|
|
var b = browserify(filename, {debug: debug})
|
|
|
|
_.each(vendor_packages, function(v){
|
|
|
|
b.external(v);
|
|
|
|
});
|
|
|
|
b.transform(reactify);
|
|
|
|
return b.bundle();
|
|
|
|
});
|
2015-01-02 22:46:51 +00:00
|
|
|
return gulp.src([conf.js.app])
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(dont_break_on_errors())
|
|
|
|
.pipe(browserified)
|
|
|
|
.pipe(rename("app.js"));
|
|
|
|
};
|
|
|
|
gulp.task('scripts-app-dev', function () {
|
|
|
|
return app_stream(true)
|
2015-01-02 22:46:51 +00:00
|
|
|
.pipe(gulp.dest(conf.static))
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(livereload({ auto: false }));
|
|
|
|
});
|
|
|
|
gulp.task('scripts-app-prod', function () {
|
|
|
|
return app_stream(true)
|
|
|
|
.pipe(buffer())
|
2014-09-16 09:41:49 +00:00
|
|
|
.pipe(uglify())
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(rev())
|
|
|
|
.pipe(save_rev())
|
2015-01-02 22:46:51 +00:00
|
|
|
.pipe(gulp.dest(conf.static));
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
2014-09-13 15:00:05 +00:00
|
|
|
|
2014-09-16 09:41:49 +00:00
|
|
|
|
2014-09-13 15:00:05 +00:00
|
|
|
gulp.task("jshint", function () {
|
2015-01-02 22:46:51 +00:00
|
|
|
return gulp.src(conf.js.jshint)
|
2014-09-13 23:21:13 +00:00
|
|
|
.pipe(dont_break_on_errors())
|
2014-09-16 09:41:49 +00:00
|
|
|
.pipe(react())
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(plumber())
|
2014-09-13 15:00:05 +00:00
|
|
|
.pipe(jshint())
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(jshint.reporter("jshint-stylish"))
|
|
|
|
.pipe(jsHintErrorReporter());
|
2014-12-12 16:49:10 +00:00
|
|
|
});
|
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.task("copy", function(){
|
2015-01-02 22:46:51 +00:00
|
|
|
return gulp.src(conf.copy, {base:"src/"})
|
|
|
|
.pipe(gulp.dest(conf.dist));
|
2014-09-17 19:14:55 +00:00
|
|
|
});
|
2014-09-16 09:41:49 +00:00
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
function templates(){
|
2015-01-02 22:46:51 +00:00
|
|
|
return gulp.src(conf.templates, {base:"src/"})
|
2014-12-30 08:51:07 +00:00
|
|
|
.pipe(replace(/\{\{\{(\S*)\}\}\}/g, function(match, p1) {
|
|
|
|
return manifest[p1];
|
|
|
|
}))
|
2015-01-02 22:46:51 +00:00
|
|
|
.pipe(gulp.dest(conf.dist));
|
2014-12-30 08:51:07 +00:00
|
|
|
};
|
|
|
|
gulp.task('templates', templates);
|
2014-09-13 15:00:05 +00:00
|
|
|
|
2015-01-02 22:46:51 +00:00
|
|
|
gulp.task("peg", function () {
|
|
|
|
return gulp.src(conf.peg, {base: "src/"})
|
|
|
|
.pipe(dont_break_on_errors())
|
|
|
|
.pipe(peg())
|
|
|
|
.pipe(filelog())
|
|
|
|
.pipe(gulp.dest("src/"));
|
|
|
|
});
|
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.task('connect', function() {
|
|
|
|
connect.server({
|
2015-01-02 22:46:51 +00:00
|
|
|
port: conf.port
|
2014-12-30 08:51:07 +00:00
|
|
|
});
|
2014-09-13 23:21:13 +00:00
|
|
|
});
|
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.task(
|
|
|
|
"dev",
|
|
|
|
[
|
|
|
|
"fonts",
|
|
|
|
"copy",
|
|
|
|
"styles-vendor-dev",
|
|
|
|
"styles-app-dev",
|
|
|
|
"scripts-vendor-dev",
|
2015-01-02 22:46:51 +00:00
|
|
|
"peg",
|
2014-12-30 08:51:07 +00:00
|
|
|
"scripts-app-dev",
|
|
|
|
],
|
|
|
|
templates
|
|
|
|
);
|
|
|
|
gulp.task(
|
|
|
|
"prod",
|
|
|
|
[
|
|
|
|
"fonts",
|
|
|
|
"copy",
|
|
|
|
"styles-vendor-prod",
|
|
|
|
"styles-app-prod",
|
|
|
|
"scripts-vendor-prod",
|
2015-01-02 22:46:51 +00:00
|
|
|
"peg",
|
2014-12-30 08:51:07 +00:00
|
|
|
"scripts-app-prod",
|
|
|
|
"connect"
|
|
|
|
],
|
|
|
|
templates
|
|
|
|
);
|
2014-09-16 09:41:49 +00:00
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.task("default", ["dev", "connect"], function () {
|
2014-09-13 15:00:05 +00:00
|
|
|
livereload.listen({auto: true});
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.watch(["src/css/vendor*"], ["styles-vendor-dev"]);
|
2015-01-02 22:46:51 +00:00
|
|
|
gulp.watch(conf.peg, ["peg", "scripts-app-dev"]);
|
2014-12-30 08:51:07 +00:00
|
|
|
gulp.watch(["src/js/**"], ["scripts-app-dev", "jshint"]);
|
|
|
|
gulp.watch(["src/css/**"], ["styles-app-dev"]);
|
2015-01-02 22:46:51 +00:00
|
|
|
gulp.watch(conf.templates, ["templates"]);
|
|
|
|
gulp.watch(conf.copy, ["copy"]);
|
2014-09-15 16:08:26 +00:00
|
|
|
});
|