Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Last active August 29, 2015 13:55
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 juanbrujo/8777765 to your computer and use it in GitHub Desktop.
Save juanbrujo/8777765 to your computer and use it in GitHub Desktop.
Basic gulpfile.js to work a basic HTML/JS/LESS/Images project under Gulp.JS/Express
var gulp = require('gulp');
var less = require('gulp-less');
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');
var imagemin = require('gulp-imagemin');
var EXPRESS_PORT = 4000;
var EXPRESS_ROOT = __dirname;
var LIVERELOAD_PORT = 35729;
gulp.task('less', function() {
gulp.src('css/*.less')
.pipe(watch())
.pipe(less())
.pipe(gulp.dest('assets'))
});
gulp.task('compress', function() {
gulp.src('js/*.js')
.pipe(uglify({outSourceMaps: true}))
.pipe(gulp.dest('assets'))
});
gulp.task('imagemin', function () {
gulp.src('images/*.*')
.pipe(imagemin())
.pipe(gulp.dest('assets/images'));
});
function startExpress() {
var express = require('express');
var app = express();
app.use(require('connect-livereload')());
app.use(express.static(EXPRESS_ROOT));
app.listen(EXPRESS_PORT);
}
var lr;
function startLivereload() {
lr = require('tiny-lr')();
lr.listen(LIVERELOAD_PORT);
}
function notifyLivereload(event) {
gulp.src(event.path, {read: false})
.pipe(require('gulp-livereload')(lr));
}
gulp.task('default', function () {
startExpress();
startLivereload();
gulp.watch('*.html', notifyLivereload);
gulp.watch('js/*.js', function() {
gulp.run('compress');
notifyLivereload;
});
gulp.watch('css/*.less', function() {
gulp.run('less');
notifyLivereload;
});
gulp.watch('images/*.*', function() {
gulp.run('imagemin');
notifyLivereload;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment