Skip to content

Instantly share code, notes, and snippets.

@mugli
Created February 17, 2014 17:52
Show Gist options
  • Save mugli/9055556 to your computer and use it in GitHub Desktop.
Save mugli/9055556 to your computer and use it in GitHub Desktop.
'use strict';
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin');
var Server = require('.');
gulp.task('scripts', function () {
return gulp.src(['client/js/**/*.js', '!client/js/vendor/**'])
.pipe(uglify())
.pipe(gulp.dest('build/js'));
});
gulp.task('images', function () {
return gulp.src('client/img/**')
.pipe(imagemin({ optimizationLevel: 5 }))
.pipe(gulp.dest('build/img'));
});
gulp.task('default', function () {
gulp.run('scripts', 'images');
});
gulp.task('serve', function () {
var serve = new Server();
serve.server({ dir: './client' });
gulp.run('default');
gulp.watch('client/js/**', function (e) {
gulp.run('scripts');
serve.livereload(e);
});
gulp.watch('client/img/**', function (e) {
gulp.run('images');
serve.livereload(e);
});
});
'use strict';
var gutil = require('gulp-util');
var koa = require('koa');
var serve = require('koa-static');
var tinylr = require('tiny-lr');
/**
* Initialize `Server`
*
* @api public
*/
function Server() {
this.app = koa();
this.lr = tinylr();
}
/**
* Create a LiveReload instance
*
* @param {Object} e
* @api public
*/
Server.prototype.livereload = function (e) {
gutil.log(gutil.colors.cyan(e.path), 'changed');
this.lr.changed({
body: {
files: [e.path]
}
});
};
/**
* Create a static server with LiveReload
*
* Options:
*
* - `dir` Root directory for server
* - `port` Port static server listens to
* - `lr` Port LiveReload listens to
*
* @param {Object} opts
* @api public
*/
Server.prototype.server = function (opts) {
opts = opts || {};
var port = opts.port || 8000;
var portLr = opts.lr || 35729;
this.lr.listen(portLr, function () {
gutil.log('LiveReload listening on port', gutil.colors.magenta(portLr));
});
this.app.use(serve(opts.dir));
this.app.listen(port, function () {
gutil.log('Static server listening on port', gutil.colors.magenta(port));
});
};
/**
* Module exports
*/
module.exports = Server;
{
"name": "site",
"scripts": {
"build": "node --harmony ./node_modules/.bin/gulp",
"serve": "node --harmony ./node_modules/.bin/gulp serve"
},
"devDependencies": {
"gulp": "~3.4.0",
"gulp-imagemin": "~0.1.4",
"gulp-uglify": "~0.1.0",
"gulp-util": "~2.2.12",
"koa": "~0.3.0",
"koa-static": "~1.4.2",
"tiny-lr": "0.0.5"
},
"engines": {
"node": ">=0.11.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment