Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active May 26, 2023 19:38
Show Gist options
  • Save joepie91/10a016dae4a5c3d73891 to your computer and use it in GitHub Desktop.
Save joepie91/10a016dae4a5c3d73891 to your computer and use it in GitHub Desktop.
gulpfile for CoffeeScript + LiveReload + some other stuff
/* bundleLogger
------------
Provides gulp style logs to the bundle method in browserify.js
*/
var gutil = require('gulp-util');
var prettyHrtime = require('pretty-hrtime');
var startTime;
module.exports = {
start: function() {
startTime = process.hrtime();
gutil.log('Running', gutil.colors.green("'bundle'") + '...');
},
end: function() {
var taskTime = process.hrtime(startTime);
var prettyTime = prettyHrtime(taskTime);
gutil.log('Finished', gutil.colors.green("'bundle'"), 'in', gutil.colors.magenta(prettyTime));
}
};
var notify = require("gulp-notify");
module.exports = function() {
var args = Array.prototype.slice.call(arguments);
// Send error to notification center with gulp-notify
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
// Keep gulp from hanging on this task
this.emit('end');
};
var gulp = require('gulp');
/* CoffeeScript compile deps */
var path = require('path');
var gutil = require('gulp-util');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var coffee = require('gulp-coffee');
var cache = require('gulp-cached');
var remember = require('gulp-remember');
var plumber = require('gulp-plumber');
var livereload = require('gulp-livereload');
var nodemon = require("gulp-nodemon");
var jade = require("gulp-jade");
var net = require("net");
/* For Browserify task; see https://github.com/greypants/gulp-starter/blob/master/gulp/tasks/browserify.js */
var browserify = require('browserify');
var watchify = require('watchify');
var bundleLogger = require('./gulp.bundleLogger');
var handleErrors = require('./gulp.handleErrors');
var source = require('vinyl-source-stream');
task = {
"source": ["templateUtil.coffee", "routes/**/*.coffee", "models/**/*.coffee", "app_modules/**/*.coffee", "app.coffee", "util.coffee", "middleware.coffee", "knexfile.coffee"],
"frontend": ["./public/js/script.coffee"],
//"jade": ["views/angular/**/*.jade"]
}
gulp.task('coffee', function() {
return gulp.src(task.source, {base: "."})
.pipe(plumber())
.pipe(cache("coffee"))
.pipe(coffee({bare: true}).on('error', gutil.log)).on('data', gutil.log)
.pipe(remember("coffee"))
.pipe(gulp.dest("."));
});
gulp.task('browserify', function() {
var bundler = browserify({
cache: {}, packageCache: {}, fullPaths: true,
entries: task.frontend,
extensions: ['.coffee'],
debug: true
});
var bundle = function() {
bundleLogger.start();
console.log("Bundling...");
return bundler
.bundle()
.on('error', handleErrors)
.pipe(source('bundled.js'))
.pipe(gulp.dest('./public/js/'))
.on('end', bundleLogger.end);
};
if(global.isWatching) {
console.log("Starting Browserify watcher...");
bundler = watchify(bundler);
bundler.on('update', bundle);
}
return bundle();
});
/*gulp.task("jade", function() {
return gulp.src(task.jade, {base: "./views/angular"})
.pipe(plumber())
.pipe(cache("jade"))
.pipe(jade({locals: require("./templateUtil")}))
.pipe(remember("jade"))
.pipe(gulp.dest("public/templates"))
});*/
function checkServerUp(){
setTimeout(function(){
var sock = new net.Socket();
sock.setTimeout(50);
sock.on("connect", function(){
console.log("Triggering page reload...");
livereload.changed();
sock.destroy();
})
.on("timeout", checkServerUp)
.on("error", checkServerUp)
.connect(3000);
}, 70);
}
gulp.task('watch', function () {
global.isWatching = true;
livereload.listen();
gulp.watch(task.jade, ['jade'])
gulp.watch(['./**/*.css', 'views/**/*.jade', '!views/angular/**/*.jade', 'package.json']).on('change', livereload.changed);
//gulp.watch(['public/templates/**/*.html']).on('change', function() { livereload.changed("*"); }); // We need to explicitly reload everything here; Angular doesn't do partial reloading
gulp.watch(task.source, ['coffee']);
// theseus disabled for now, it was screwing with my tracebacks
//nodemon({script: "./bin/www", ext: "js", nodeArgs: ['/usr/bin/node-theseus']}).on("start", checkServerUp);
nodemon({script: "./bin/www", ext: "js", delay: 500}).on("start", checkServerUp);
});
gulp.task('default', ['coffee', /*'jade',*/ 'watch', 'browserify']);
[...]
"devDependencies": {
"browserify": "^5.11.2",
"browserify-shim": "^3.7.0",
"coffeeify": "^0.7.0",
"gulp": "~3.8.0",
"gulp-cached": "~0.0.3",
"gulp-coffee": "~2.0.1",
"gulp-concat": "~2.2.0",
"gulp-jade": "^0.7.0",
"gulp-livereload": "~2.1.0",
"gulp-nodemon": "~1.0.4",
"gulp-notify": "^1.6.0",
"gulp-plumber": "~0.6.3",
"gulp-remember": "~0.2.0",
"gulp-rename": "~1.2.0",
"gulp-util": "~2.2.17",
"pretty-hrtime": "^0.2.1",
"vinyl-source-stream": "^1.0.0",
"watchify": "^1.0.2"
},
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment