Last active
August 29, 2015 14:07
-
-
Save jaceju/17c4386e38501a31d977 to your computer and use it in GitHub Desktop.
Gulpfile for Laravel 4.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/////// COMMON /////// | |
var gulp = require('gulp'); | |
var del = require('del'); | |
var path = require('path'); | |
var runSequence = require('run-sequence'); | |
// Load plugins | |
var $ = require('gulp-load-plugins')(); | |
var browserify = require('browserify'); | |
var transform = require('vinyl-transform'); | |
// Templates | |
gulp.task('views:develop', function() { | |
return gulp.src('app/templates/**/*.blade.php') | |
.pipe($.plumber()) | |
.pipe(gulp.dest('app/views')) | |
.pipe($.size({ title: 'views:develop' })); | |
}); | |
// Styles | |
gulp.task('styles', function() { | |
return gulp.src('assets/styles/*.scss') | |
.pipe($.plumber()) | |
.pipe($.rubySass({ | |
compass: true, | |
style: 'compressed' | |
})) | |
.pipe($.autoprefixer('last 1 version')) | |
.pipe(gulp.dest('public/styles')) | |
.pipe($.size({ title: 'styles' })); | |
}); | |
// Scripts | |
gulp.task('scripts', function() { | |
var browserified = transform(function(filename) { | |
var b = browserify(filename); | |
return b.bundle() | |
.on('error', function (err) { | |
console.log(err.toString()); | |
this.emit('end'); | |
}); | |
}); | |
return gulp.src('./assets/scripts/*.js') | |
.pipe($.jshint('.jshintrc')) | |
.pipe($.jshint.reporter('jshint-stylish')) | |
.pipe(browserified) | |
.pipe(gulp.dest('public/scripts')) | |
.pipe($.size({ title: 'scripts' })); | |
}); | |
// Testing | |
gulp.task('jest', function() { | |
var nodeModules = path.resolve('./node_modules'); | |
return gulp.src('assets/scripts/**/__tests__') | |
.pipe($.jest({ | |
scriptPreprocessor: nodeModules + '/gulp-jest/preprocessor.js', | |
unmockedModulePathPatterns: [nodeModules + '/react'] | |
})); | |
}); | |
// Images | |
gulp.task('images', function() { | |
return gulp.src('assets/images/**/*') | |
.pipe($.plumber()) | |
.pipe($.cache($.imagemin({ | |
optimizationLevel: 3, | |
progressive: true, | |
interlaced: true | |
}))) | |
.pipe(gulp.dest('public/images')) | |
.pipe($.size({ title: 'images' })); | |
}); | |
// Fonts | |
gulp.task('fonts', function () { | |
return gulp.src('public/bower_components/**/fonts/**/*.{otf,eot,svg,ttf,woff}') | |
.pipe($.flatten()) | |
.pipe(gulp.dest('public/fonts')) | |
.pipe($.size({ title: 'fonts' })); | |
}); | |
/////// DEVELOPMENT /////// | |
// Clean | |
gulp.task('clean:develop', function(cb) { | |
del([ | |
'app/views', | |
'public/styles', | |
'public/scripts', | |
'.sass-cache' | |
], cb); | |
}); | |
// Prepare for development | |
gulp.task('prepare:develop', function (cb) { | |
runSequence( | |
'clean:develop', | |
[ | |
'views:develop', | |
'styles', | |
'scripts', | |
'images', | |
'fonts' | |
], | |
cb); | |
}); | |
// Start Web server | |
gulp.task('serve', function () { | |
var spawn = require('child_process').spawn, | |
child = spawn('php', [ 'artisan', 'serve' ], { cwd: process.cwd() }), | |
log = function (data) { console.log(data.toString()) }; | |
child.stdout.on('data', log); | |
child.stderr.on('data', log); | |
process.on('exit', function () { | |
child.kill(); | |
}); | |
process.on('uncaughtException', function () { | |
child.kill(); | |
}); | |
}); | |
// Start Livereload server | |
gulp.task('livereload', function () { | |
var server = $.livereload; | |
server.listen(); | |
gulp.watch([ | |
'public/**/*', | |
'!public/bower_components/**/*', | |
]).on('change', server.changed); | |
}); | |
// Watch | |
gulp.task('watch', ['prepare:develop'], function() { | |
gulp.start('serve'); | |
gulp.start('livereload'); | |
gulp.watch('app/templates/**/*.php', ['views:develop']); | |
gulp.watch('assets/styles/**/*.scss', ['styles']); | |
gulp.watch('assets/scripts/**/*.js', ['scripts', 'jest']); | |
gulp.watch('assets/images/**/*', ['images']); | |
}); | |
/////// BUILD /////// | |
// HTML | |
gulp.task('views:build', ['views:develop', 'styles', 'scripts', 'images', 'fonts'], function() { | |
var jsFilter = $.filter('**/*.js'); | |
var saveLicense = require('uglify-save-license'); | |
var useref = $.useref; | |
var assets = useref.assets({ | |
searchPath: 'public' | |
}); | |
return gulp.src('app/views/**/*.blade.php') | |
.pipe($.plumber()) | |
.pipe(assets) | |
.pipe(jsFilter) | |
.pipe($.uglify({ preserveComments: saveLicense })) | |
.pipe(jsFilter.restore()) | |
.pipe($.rev()) | |
.pipe(gulp.dest('public')) | |
.pipe(useref.restore()) | |
.pipe(useref()) | |
.pipe($.revReplace({ | |
replaceInExtensions: ['.php'] | |
})) | |
.pipe(gulp.dest('app/views')) | |
.pipe($.size({ title: 'views' })); | |
}); | |
// Clean Cache | |
gulp.task('clean:cache', function (cb) { | |
return $.cache.clearAll(cb); | |
}); | |
// Clean | |
gulp.task('clean:build', ['clean:develop', 'clean:cache'], function(cb) { | |
del([ | |
'public/css', | |
'public/js', | |
'public/fonts', | |
'public/images' | |
], cb); | |
}); | |
// Clean temporary assets | |
gulp.task('clean:temporary', function (cb) { | |
del([ | |
'public/styles', | |
'public/scripts', | |
'app/views/css', | |
'app/views/js', | |
'.sass-cache' | |
], cb); | |
}); | |
// Build | |
gulp.task('default', function(cb) { | |
runSequence( | |
'clean:build', | |
'views:build', | |
'clean:temporary', | |
cb); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment