Skip to content

Instantly share code, notes, and snippets.

@takashi
Created May 31, 2014 11:47
Show Gist options
  • Save takashi/5c2fc118ee19a14e59cf to your computer and use it in GitHub Desktop.
Save takashi/5c2fc118ee19a14e59cf to your computer and use it in GitHub Desktop.
for static html page
var gulp = require('gulp'),
useref = require('gulp-useref'),
sass = require('gulp-ruby-sass'),
concat = require('gulp-concat'),
prefixer = require('gulp-autoprefixer'),
minify = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
plumber = require('gulp-plumber'),
clean = require('gulp-clean'),
cache = require('gulp-cache'),
imagemin = require('gulp-imagemin'),
pngcrush = require('imagemin-pngcrush');
// define paths
var paths = {
scripts: ['./js/plugins.js', './js/main.js'],
vendorScripts: ['./js/vendor/*.js'],
styles: ['./css/normalize.css', './css/main.css'],
sass: ['./sass/main.sass'],
dist: './dist',
images: './img/*',
fonts: ['./css/*.woff', './css/*.eot', './css/*.otf', './css/*.svg', './css/*.ttf'],
tmp: './tmp'
};
gulp.task('clean-dist', function () {
return gulp.src(paths.dist, {read:false})
.pipe(clean({force: true}));
});
/* ============================
tasks for stylesheets
============================= */
gulp.task('css', function () {
return gulp.src(paths.styles)
.pipe(plumber())
.pipe(gulp.dest(paths.tmp))
.pipe(prefixer('last 2 version'))
.pipe(concat('style.css'))
.pipe(minify())
.pipe(gulp.dest(paths.dist));
});
/* compile sass that uses compass to css */
gulp.task('sass', function () {
return gulp.src(paths.sass)
.pipe(plumber())
.pipe(sass())
.pipe(gulp.dest('./css'));
});
/* ============================
tasks for JavaScripts
============================= */
gulp.task('js', function() {
return gulp.src(paths.scripts)
.pipe(gulp.dest(paths.tmp))
.pipe(concat('main.js'))
.pipe(uglify())
.pipe(gulp.dest(paths.dist + '/js'))
})
/* ============================
tasks for html
============================= */
gulp.task('html', function() {
return gulp.src('./*.html')
.pipe(useref.assets())
.pipe(useref.restore())
.pipe(useref())
.pipe(gulp.dest(paths.dist));
});
/* ============================
compress images
============================= */
gulp.task('images', function() {
return gulp.src(paths.images)
.pipe(cache(imagemin({
progressive: true,
interlaced: true,
use: [pngcrush()]
})))
.pipe(gulp.dest(paths.dist + '/img'))
});
/* ============================
tasks for files
============================= */
gulp.task('moveToDist', function() {
gulp.src(paths.vendorScripts)
.pipe(gulp.dest(paths.dist + '/js/vendor'));
gulp.src(paths.fonts)
.pipe(gulp.dest(paths.dist));
});
/* ============================
resiger tasks
============================= */
/* watch task */
gulp.task('watch', function(){
gulp.watch(['./sass/*.sass', './sass/**/*.sass'], ['sass']);
});
/* build task */
gulp.task('build', ['clean-dist'], function(){
gulp.start('sass', 'css', 'js', 'html', 'images', 'moveToDist');
});
/* default tasks is "build" */
gulp.task('default', function(){
gulp.start('watch');
});
// for gulp-devtools
module.exports = gulp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment