dispatch/client/gulpfile.js

145 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-04-29 22:45:02 +00:00
var exec = require('child_process').exec;
2015-01-17 01:37:21 +00:00
var gulp = require('gulp');
2015-06-07 22:11:03 +00:00
var gutil = require('gulp-util');
2015-01-17 01:37:21 +00:00
var gulpif = require('gulp-if');
var minifyHTML = require('gulp-minify-html');
var minifyCSS = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');
var uglify = require('gulp-uglify');
2015-02-05 00:37:34 +00:00
var gzip = require('gulp-gzip');
2015-05-12 21:50:55 +00:00
var concat = require('gulp-concat');
2015-06-01 22:09:28 +00:00
var eslint = require('gulp-eslint');
2015-01-17 01:37:21 +00:00
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var streamify = require('gulp-streamify');
2015-02-21 06:23:25 +00:00
var babelify = require('babelify');
2015-01-17 01:37:21 +00:00
var strictify = require('strictify');
var watchify = require('watchify');
2015-06-07 22:11:03 +00:00
var merge = require('merge-stream');
2015-06-07 23:06:08 +00:00
var cache = require('gulp-cached');
2015-01-17 01:37:21 +00:00
var argv = require('yargs')
.alias('p', 'production')
.argv;
if (argv.production) {
process.env['NODE_ENV'] = 'production';
}
2015-06-07 22:11:03 +00:00
var deps = Object.keys(require('./package.json').dependencies);
2015-01-17 01:37:21 +00:00
gulp.task('html', function() {
return gulp.src('src/*.html')
2015-01-17 01:37:21 +00:00
.pipe(minifyHTML())
.pipe(gulp.dest('dist'));
2015-01-17 01:37:21 +00:00
});
gulp.task('css', function() {
return gulp.src(['src/css/fontello.css', 'src/css/style.css'])
2015-05-12 21:50:55 +00:00
.pipe(concat('bundle.css'))
2015-01-17 01:37:21 +00:00
.pipe(autoprefixer())
.pipe(minifyCSS())
2015-05-12 21:50:55 +00:00
.pipe(gulp.dest('dist'));
2015-01-17 01:37:21 +00:00
});
gulp.task('js', function() {
return js(false);
});
function js(watch) {
2015-06-07 22:11:03 +00:00
var bundler = browserify('./src/js/app.js', {
2015-01-17 01:37:21 +00:00
debug: !argv.production,
2015-12-11 23:33:05 +00:00
transform: [
babelify.configure({
presets: ['es2015', 'react']
}),
strictify
],
2015-01-17 01:37:21 +00:00
cache: {},
packageCache: {},
fullPaths: watch
});
2015-06-07 22:11:03 +00:00
bundler.external(deps);
2015-01-17 01:37:21 +00:00
2015-06-07 22:11:03 +00:00
var rebundle = function() {
return bundler.bundle()
.on('error', gutil.log)
2015-01-17 01:37:21 +00:00
.pipe(source('bundle.js'))
.pipe(gulpif(argv.production, streamify(uglify())))
.pipe(gulp.dest('dist'));
2015-01-17 01:37:21 +00:00
};
2015-06-07 22:11:03 +00:00
if (watch) {
bundler = watchify(bundler);
bundler.on('update', rebundle);
bundler.on('log', gutil.log);
}
var vendorBundler = browserify({
debug: !argv.production,
require: deps
2015-01-17 01:37:21 +00:00
});
2015-06-07 22:11:03 +00:00
var vendor = vendorBundler.bundle()
.on('error', gutil.log)
.pipe(source('vendor.js'))
.pipe(gulpif(argv.production, streamify(uglify())))
.pipe(gulp.dest('dist'));
2015-12-11 23:33:05 +00:00
2015-06-07 22:11:03 +00:00
return merge(rebundle(), vendor);
2015-01-17 01:37:21 +00:00
}
2015-06-01 22:09:28 +00:00
gulp.task('lint', function() {
return gulp.src('src/js/**/*.{js,jsx}')
2015-06-07 23:06:08 +00:00
.pipe(cache('lint'))
2015-06-01 22:09:28 +00:00
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError());
});
2015-02-16 20:53:01 +00:00
gulp.task('fonts', function() {
return gulp.src('src/font/*')
.pipe(gulp.dest('dist/font'));
2015-02-16 20:53:01 +00:00
});
gulp.task('config', function() {
return gulp.src('../config.default.toml')
.pipe(gulp.dest('dist/gz'));
});
2015-02-16 20:53:01 +00:00
gulp.task('gzip', ['html', 'css', 'js', 'fonts'], function() {
return gulp.src(['dist/**/!(*.gz)', '!dist/{gz,gz/**}'])
2015-02-05 00:37:34 +00:00
.pipe(gzip())
.pipe(gulp.dest('dist/gz'));
2015-02-05 00:37:34 +00:00
});
2015-06-07 23:06:08 +00:00
gulp.task('gzip:watch', function() {
2015-06-07 22:21:10 +00:00
return gulp.src('dist/**/*.{html,css,js}')
2015-06-07 23:06:08 +00:00
.pipe(cache('gzip'))
2015-06-07 22:21:10 +00:00
.pipe(gzip())
.pipe(gulp.dest('dist/gz'));
});
2015-04-29 22:45:02 +00:00
function bindata(cb) {
if (argv.production) {
exec('go-bindata -nomemcopy -nocompress -pkg assets -o ../assets/bindata.go -prefix "dist/gz" dist/gz/...', cb);
2015-04-29 22:45:02 +00:00
} else {
exec('go-bindata -debug -pkg assets -o ../assets/bindata.go -prefix "dist/gz" dist/gz/...', cb);
2015-04-29 22:45:02 +00:00
}
}
2015-06-07 22:21:10 +00:00
gulp.task('bindata', ['gzip', 'config'], bindata);
gulp.task('bindata:watch', ['gzip:watch'], bindata);
2015-04-29 22:45:02 +00:00
2015-01-17 01:37:21 +00:00
gulp.task('watch', ['default'], function() {
2015-04-29 22:45:02 +00:00
gulp.watch('dist/**/*.{html,css,js}', ['gzip:watch', 'bindata:watch'])
gulp.watch('src/*.html', ['html']);
gulp.watch('src/css/*.css', ['css']);
2015-06-01 22:09:28 +00:00
gulp.watch('src/js/**/*.{js,jsx}', ['lint']);
2015-01-17 01:37:21 +00:00
return js(true);
});
2015-12-11 23:33:05 +00:00
gulp.task('default', ['html', 'css', 'js', 'lint', 'fonts', 'config', 'gzip', 'bindata']);