Skip to content

Instantly share code, notes, and snippets.

@thgaskell
Created March 25, 2014 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thgaskell/9763636 to your computer and use it in GitHub Desktop.
Save thgaskell/9763636 to your computer and use it in GitHub Desktop.
Creates a mini development environment for serving static content.
var gulp = require('gulp'),
gutil = require('gulp-util');
var paths = {
css: 'assets/css/**/*.css',
html: 'assets/html/**/*.html',
js: 'assets/js/**/*.js',
img: 'assets/images/**/*.{png,jpg,jpeg,gif}'
};
/**
* Configure Express server.
* Serve static files on port 3000
*/
gulp.task('express', function() {
var express = require('express'),
path = require('path'),
app = express(),
port = 3000;
app.use(express.static(path.resolve('./dist')));
app.listen(port);
gutil.log('Listening on port: ' + port);
});
/**
* Automatically add vendor prefixes and minify CSS files.
*/
gulp.task('css', function() {
gulp.src(paths.css)
.pipe(require('gulp-autoprefixer')())
.pipe(require('gulp-csso')())
.pipe(gulp.dest('./dist/css'));
});
/**
* Copy HTML files to dist folder.
* Could be replaced with a templating
* preprocessor like LESS.
*/
gulp.task('html', function() {
gulp.src(paths.html)
.pipe(gulp.dest('./dist'));
});
/**
* Uglify JS files.
*/
gulp.task('js', function() {
gulp.src(paths.js)
.pipe(require('gulp-uglify')())
.pipe(gulp.dest('./dist/js'));
});
/**
* Compress PNG, JPEG, GIF files.
*/
gulp.task('img', function() {
gulp.src(paths.img)
.pipe(require('gulp-imagemin')())
.pipe(gulp.dest('./dist/images'));
});
/**
* Create watch tasks to trigger LiveReload
* to inject styles and scripts and refresh on HTML changes.
* Requires: LiveReload browser plugin.
*/
gulp.task('watch', function() {
var server = require('gulp-livereload')();
[ // Add watch tasks here
gulp.watch(paths.html, ['html']),
gulp.watch(paths.js, ['js']),
gulp.watch(paths.css, ['css']),
gulp.watch(paths.img, ['img'])
]
.forEach(function(task) {
task.on('change', function(file) {
server.changed(file.path);
});
});
});
gulp.task('build', ['html', 'css', 'js', 'img']);
gulp.task('default', ['build', 'express', 'watch']);
{
"name": "gulp-static-server",
"version": "0.0.0",
"description": "Gulp tasks working together to serve static content.",
"scripts": {
"start": "gulp"
},
"license": "WTFPL",
"devDependencies": {
"gulp-livereload": "~1.2.0",
"gulp-util": "~2.2.14",
"gulp": "~3.5.6",
"gulp-uglify": "~0.2.1",
"gulp-autoprefixer": "0.0.6",
"gulp-csso": "~0.2.6",
"gulp-imagemin": "~0.1.5"
},
"dependencies": {
"express": "~3.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment