Skip to content

Instantly share code, notes, and snippets.

@aegyed91
Created May 20, 2015 20:56
Show Gist options
  • Save aegyed91/71235522048bc4d6b154 to your computer and use it in GitHub Desktop.
Save aegyed91/71235522048bc4d6b154 to your computer and use it in GitHub Desktop.
'use strict';
var gulp = require('gulp'),
$ = require('gulp-load-plugins')(),
_ = require('lodash'),
browserSync = require('browser-sync'),
reload = browserSync.reload,
nodemon = require('gulp-nodemon'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
browserify = require('browserify'),
babelify = require("babelify"),
watchify = require('watchify'),
lrload = require('livereactload');
var isProd = process.env.NODE_ENV === 'production';
var bundler = browserify({
entries: ['./public/scripts/index.js'],
extensions: ['.jsx'],
transform: isProd ? [ babelify ] : [ babelify, lrload ],
debug: !isProd,
cache: {},
packageCache: {},
fullPaths: true // for watchify
});
// --------------------------------------------------------------------------
// Single tasks
// --------------------------------------------------------------------------
gulp.task('jswatch', function() {
// start listening reload notifications
lrload.monitor('public/.tmp/scripts/bundle.js', {displayNotification: true});
// start JS file watching and rebundling with watchify
var watcher = watchify(bundler);
rebundle();
return watcher
.on('error', $.util.log)
.on('update', rebundle);
function rebundle() {
$.util.log('Update JavaScript bundle');
watcher
.bundle()
.on('error', $.util.log)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulp.dest('public/.tmp/scripts'))
}
});
gulp.task('serverwatch', function() {
nodemon({
script: 'index.js',
watch: ['index.js', 'server.js', 'controllers/**/*.js', 'models/**/*.js', 'middlewares/**/*.js']
})
.on('change', [])
.on('restart', function () {
console.log('Server restarted')
})
});
gulp.task('styles', function () {
return gulp.src('public/styles/main.scss')
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'nested', // libsass doesn't support expanded yet
precision: 10,
includePaths: ['.'],
onError: console.error.bind(console, 'Sass error:')
}))
.pipe($.postcss([
require('autoprefixer-core')({browsers: ['last 1 version']})
]))
.pipe($.sourcemaps.write())
.pipe(gulp.dest('public/.tmp/styles'))
.pipe(reload({stream: true}));
});
gulp.task('fonts', function () {
gulp.src([
'./node_modules/bootstrap-sass/assets/fonts/bootstrap',
]).pipe(gulp.dest('output/folder'));
return gulp.src(require('main-bower-files')({
filter: '**/*.{eot,svg,ttf,woff,woff2}'
}).concat('app/fonts/**/*'))
.pipe(gulp.dest('public/.tmp/fonts'));
});
gulp.task('html', ['styles'], function () {
var assets = $.useref.assets({searchPath: ['public/.tmp', 'public', '.']});
return gulp.src('public/*.hbs')
.pipe(assets)
.pipe($.if('*.css', $.csso()))
.pipe($.rev())
.pipe(assets.restore())
.pipe($.useref())
.pipe($.revReplace())
.pipe($.if('*.hbs', $.minifyHtml({conditionals: true, loose: true})))
.pipe(gulp.dest('public/dist'));
});
gulp.task('clean', require('del').bind(null, ['public/.tmp', 'public/dist']));
// --------------------------------------------------------------------------
// Tasks
// --------------------------------------------------------------------------
gulp.task('serve', ['jswatch', 'serverwatch', 'styles'], function () {
browserSync({
notify: false,
browser: "google chrome",
proxy: "http://localhost:3001",
port: 3000
});
// watch for changes
gulp.watch('public/styles/**/*.scss', ['styles']);
});
gulp.task('build', ['html'], function () {
return gulp.src('public/dist/**/*').pipe($.size({title: 'build', gzip: true}));
});
gulp.task('default', ['clean'], function () {
gulp.start('build');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment