Skip to content

Instantly share code, notes, and snippets.

@laracasts
Last active February 10, 2024 10:57
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save laracasts/8659865 to your computer and use it in GitHub Desktop.
Save laracasts/8659865 to your computer and use it in GitHub Desktop.
Example Laravel-specific Gulpfile from Laracasts.com
var gulp = require('gulp');
var gutil = require('gulp-util');
var notify = require('gulp-notify');
var sass = require('gulp-ruby-sass');
var autoprefix = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css')
var coffee = require('gulp-coffee');
var exec = require('child_process').exec;
var sys = require('sys');
// Where do you store your Sass files?
var sassDir = 'app/assets/sass';
// Which directory should Sass compile to?
var targetCSSDir = 'public/css';
// Where do you store your CoffeeScript files?
var coffeeDir = 'app/assets/coffee';
// Which directory should CoffeeScript compile to?
var targetJSDir = 'public/js';
// Compile Sass, autoprefix CSS3,
// and save to target CSS directory
gulp.task('css', function () {
return gulp.src(sassDir + '/main.sass')
.pipe(sass({ style: 'compressed' }).on('error', gutil.log))
.pipe(autoprefix('last 10 version'))
.pipe(gulp.dest(targetCSSDir))
.pipe(notify('CSS minified'))
});
// Handle CoffeeScript compilation
gulp.task('js', function () {
return gulp.src(coffeeDir + '/**/*.coffee')
.pipe(coffee().on('error', gutil.log))
.pipe(gulp.dest(targetJSDir))
});
// Run all PHPUnit tests
gulp.task('phpunit', function() {
exec('phpunit', function(error, stdout) {
sys.puts(stdout);
});
});
// Keep an eye on Sass, Coffee, and PHP files for changes...
gulp.task('watch', function () {
gulp.watch(sassDir + '/**/*.sass', ['css']);
gulp.watch(coffeeDir + '/**/*.coffee', ['js']);
gulp.watch('app/**/*.php', ['phpunit']);
});
// What tasks does running gulp trigger?
gulp.task('default', ['css', 'js', 'phpunit', 'watch']);
@laracasts
Copy link
Author

I'm still having trouble catching Sass errors. Once I updated Gulp to 3.5, it doesn't seem like the errors ever get caught. Any help with that?

@mikeerickson
Copy link

So, using the PHPUnit test, do you receive an feedback when there are errors. It no longer "kills" the process, but it still emits the same error I have been trying to resolve (this error is only thrown when a test fails, passing tests work fine as they did before)

events.js:72
throw er; // Unhandled 'error' event
^
Error: Command failed:
at ChildProcess.exithandler (child_process.js:637:15)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:735:16)
at Process.ChildProcess._handle.onexit (child_process.js:802:5)
mikee at Traveller in ~/Documents/Projects/smith-guide on master*
$

@laracasts
Copy link
Author

Works perfectly for me. Are you using all the modules listed above?

Are you using the Gulp exec plugin? Above, I'm just doing:

var exec = require('child_process').exec;
var sys = require('sys');

@patrickheeney
Copy link

Looking at the coffee module docs I see .pipe(coffee({bare: true}).on('error', gutil.log)). It looks like the on('error') has to be attached to the module function. I am not familiar with gulp or the sass module but would the following work?

gulp.task('css', function () {
    return gulp.src(sassDir + '/main.sass')
        .pipe(sass({ style: 'compressed' }).on('error', gutil.log))
        .pipe(autoprefix('last 10 version'))
        .pipe(gulp.dest(targetCSSDir))
        .pipe(notify('CSS minified'));
});

gulp.task('js', function () {
    return gulp.src(coffeeDir + '/**/*.coffee')
        .pipe(coffee().on('error', gutil.log))
        .pipe(gulp.dest(targetJSDir));
});

@laracasts
Copy link
Author

That used to work (with Gulp 3.4), but it doesn't seem to anymore. Even with it included, a parse error kills the watcher.

@patrickheeney
Copy link

Secondly, on the exec problem. It may be due to not catching the errors. See here: https://github.com/robrich/gulp-exec/blob/master/index.js#L24-L33

Edit: hmm was worth a shot. Sounds like a potential problem upstream. I am hoping to switch to gulp soon but unfortunately stuck with grunt at the moment.

@zanmoskotevc
Copy link

Hmm, I was thinking of trying a watch task that would do a composer dump-autoload if new directories were added.
Has anyone done anything similiar?

@bglaz
Copy link

bglaz commented Jan 28, 2014

zanmoskotevc - You should be able to do that the same way this script is running phpunit. You can use child_process.exec to run composer dump-autoload.

@mikeerickson
Copy link

@zanmoskotevc

You can do this as follows using the gulp-exec plugin:

gulp.task('composer-autoload', function() {
var options = {silent: false};
gulp.src('./app/acme/*/.php').pipe(exec('composer dumpautoload',options));
});

gulp.watch('./app/acme/*/.php', function(event){
gulp.run('composer-autoload');
});

@JeffreyWay
Copy link

Though using gulp.run has been deprecated, as of v3.5.

@zanmoskotevc
Copy link

@mikeerickson that's similiar to what I have, thanks! :)

@mikeerickson
Copy link

FYI, I have written a gulp-phpunit plugin and it should be available in nom in the next day or so. This version makes it plain simple to call :-)

param1 = path to php binary
param2 = options (currently only contains debug flag, will be expanded in future) [optional]

gulp.task('phpunit', function() {
var options = {debug: false};
gulp.src('./app/tests/*.php').pipe(unit('./vendor/bin/phpunit',options));
});

@mikeerickson
Copy link

@jeffrey My plugin does not use gulp.run so works with all versions of gulp (including current 3.5)

@zanmoskotevc Have a look at the gulp-phpunit plugin which should be active tomorrow.

@mikeerickson
Copy link

The gulp-phpunit plugin is now available on nom

https://npmjs.org/package/gulp-phpunit

npm install --save-dev gulp-phpunit

@niallobrien
Copy link

@JeffreyWay Use gulp-plumber to catch those errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment