Skip to content

Instantly share code, notes, and snippets.

@m-basov
Last active August 29, 2015 14:19
Show Gist options
  • Save m-basov/0cfd6418749a5ee29dd2 to your computer and use it in GitHub Desktop.
Save m-basov/0cfd6418749a5ee29dd2 to your computer and use it in GitHub Desktop.
Gulp with Stylus, CoffeeScript and Optipng
var gulp = require('gulp'),
gutil = require('gulp-util'),
styl = require('gulp-stylus'),
jeet = require('jeet'),
coffee = require('gulp-coffee'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
sourcemaps = require('gulp-sourcemaps'),
connect = require('gulp-connect'),
imageminOptipng = require('imagemin-optipng'),
plumber = require('gulp-plumber'),
cssmin = require('gulp-cssmin'),
ghPages = require('gulp-gh-pages'),
// Input files
input = {
'styles': 'src/stylus/**/*.styl',
'scripts': 'src/coffeescript/**/*.coffee',
'html': 'src/**/*.html',
'images': 'src/images/**/*.png',
'vendor': {
'css': 'src/vendor/css/**/*.css',
'js': 'src/vendor/js/**/*.js'
}
},
// Output files
output = {
'css': 'dist/css',
'js': 'dist/js',
'html': 'dist',
'images': 'dist/images'
};
// Default task
gulp.task('default', [
'build-vendor',
'build-html',
'build-coffee',
'build-styl',
'build-images',
'connect',
'watch'
]);
// Build production
gulp.task('build-prod', [
'build-vendor',
'build-html',
'build-coffee',
'build-styl',
'build-images'
]);
// Deploy to gh-pages
gulp.task('gh-pages', ['build-prod'], function(){
return gulp.src('./dist/**/*')
.pipe(ghPages());
});
// Build stylus styles
gulp.task('build-styl', function() {
return gulp.src(input.styles)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(styl({use: [jeet()]}))
.pipe(concat('styles.css'))
.pipe(gutil.env.type === 'production' ? cssmin() : gutil.noop())
.pipe(sourcemaps.write())
.pipe(gulp.dest(output.css))
.pipe(connect.reload());
});
// Build coffescript into js
gulp.task('build-coffee', function() {
return gulp.src(input.scripts)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(coffee())
.pipe(concat('app.js'))
.pipe(gutil.env.type === 'production' ? uglify() : gutil.noop())
.pipe(sourcemaps.write())
.pipe(gulp.dest(output.js))
.pipe(connect.reload());
});
// Copy HTML to dist
gulp.task('build-html', function() {
return gulp.src(input.html)
.pipe(gulp.dest(output.html))
.pipe(connect.reload());
});
// Build images
gulp.task('build-images', function() {
return gulp.src(input.images)
.pipe(imageminOptipng({optimizationLevel: 3})())
.pipe(gulp.dest(output.images))
.pipe(connect.reload());
});
// Run livereload
gulp.task('connect', function() {
connect.server({
root: output.html,
livereload: true
});
});
// Build vendor assets
gulp.task('build-vendor', function() {
gulp.src(input.vendor.css)
.pipe(concat('vendor.css'))
.pipe(gulp.dest(output.css))
.pipe(connect.reload());
gulp.src(input.vendor.js)
.pipe(concat('vendor.js'))
.pipe(gulp.dest(output.js))
.pipe(connect.reload());
});
// Watch files
gulp.task('watch', function() {
gulp.watch(input.scripts, ['build-coffee']);
gulp.watch(input.styles, ['build-styl']);
gulp.watch(input.html, ['build-html']);
gulp.watch(input.images, ['build-images']);
gulp.watch(input.vendor.js, ['build-vendor']);
gulp.watch(input.vendor.css, ['build-vendor']);
});
{
"name": "catapult_game",
"version": "0.0.1",
"description": "Catapult game",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Mykola Basov",
"license": "MIT",
"private": true,
"devDependencies": {
"gulp": "^3.8.11",
"gulp-coffee": "^2.3.1",
"gulp-concat": "^2.5.2",
"gulp-connect": "^2.2.0",
"gulp-cssmin": "^0.1.6",
"gulp-gh-pages": "^0.5.0",
"gulp-plumber": "^1.0.0",
"gulp-sourcemaps": "^1.5.1",
"gulp-stylus": "^2.0.1",
"gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.4",
"imagemin-optipng": "^4.2.0",
"jeet": "^6.1.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment