Skip to content

Instantly share code, notes, and snippets.

@thisisjamessmith
Created July 25, 2018 08:48
Show Gist options
  • Save thisisjamessmith/166bb7eed59c0d2560d217a27e9f17cc to your computer and use it in GitHub Desktop.
Save thisisjamessmith/166bb7eed59c0d2560d217a27e9f17cc to your computer and use it in GitHub Desktop.
Gulpfile for static sites
var gulp = require('gulp');
// --------------------------------------------
// ENVIRONMENT VARS
// --------------------------------------------
var localHost = 'example.test';
var compiledFolder = 'public_html/assets';
var srcFolder = 'src';
var tmplFolder = 'public_html';
// --------------------------------------------
// PLUGINS
// --------------------------------------------
var browserSync = require('browser-sync');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var del = require('del');
var fs = require("fs");
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// --------------------------------------------
// BROWSER SYNC
// --------------------------------------------
gulp.task('browser-sync', function() {
browserSync({
proxy: localHost,
reloadOnRestart: false, // https://github.com/BrowserSync/browser-sync/issues/386
open: false
});
});
gulp.task('bs-reload', function () {
browserSync.reload();
});
// --------------------------------------------
// STYLES
// --------------------------------------------
gulp.task('styles', function(){
return gulp.src(srcFolder + '/sass/style.scss')
.pipe(sass({ outputStyle: 'compressed' }))
.on('error', function(err) { gutil.log('Line: ' + err.lineNumber + ' - ' + err.message); gutil.beep(); })
.pipe(autoprefixer())
.pipe(gulp.dest(compiledFolder+'/css'))
.pipe(browserSync.reload({ stream: true }));
});
// --------------------------------------------
// SCRIPTS
// --------------------------------------------
gulp.task('scripts', function(){
return gulp.src(srcFolder + '/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'))
//.pipe(jshint.reporter('fail'))
.pipe(uglify())
.on('error', function(err) { gutil.log(err.message);gutil.beep(); })
.pipe(concat('js.min.js'))
.pipe(gulp.dest(compiledFolder+'/js'))
.pipe(browserSync.reload({ stream: true }));
});
// --------------------------------------------
// IMAGE MINIFIER
// --------------------------------------------
gulp.task('images', function () {
return gulp.src(compiledFolder + '/img/**/*')
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest(compiledFolder + '/img'));
});
// --------------------------------------------
// WATCH
// --------------------------------------------
gulp.task('watch', function(){
gulp.watch(srcFolder + '/sass/**/*.scss', ['styles']);
gulp.watch(tmplFolder + '/**/*.php', ['bs-reload']);
gulp.watch(srcFolder + '/js/*', ['scripts']);
});
// --------------------------------------------
// DEFAULT TRIGGER (typing 'gulp' at command line triggers these tasks)
// --------------------------------------------
gulp.task('default', ['styles', 'scripts', 'watch', 'browser-sync']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment