Skip to content

Instantly share code, notes, and snippets.

@fahimshani
Created September 7, 2016 21:11
Show Gist options
  • Save fahimshani/9a9a0fc07c4eb716320d9a252f71e6aa to your computer and use it in GitHub Desktop.
Save fahimshani/9a9a0fc07c4eb716320d9a252f71e6aa to your computer and use it in GitHub Desktop.
Basic Gulp project
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
svgSprite = require('gulp-svg-sprite'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
fileinclude = require('gulp-file-include'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
sourcemaps = require('gulp-sourcemaps'),
browserSync = require('browser-sync'),
gutil = require('gulp-util');
reload = browserSync.reload;
var gutil = require('gulp-util');
var postcss = require('gulp-postcss');
var simplevars = require('postcss-simple-vars');
var autoprefixer = require('autoprefixer-core');
var mqpacker = require('css-mqpacker');
var csswring = require('csswring');
var nestedcss = require('postcss-nested');
var corepostcss = require('postcss');
// Static server
gulp.task('browser-sync', function() {
browserSync({
open: false,
server: {
baseDir: "./",
}
});
});
// Basic configuration example
config = {
mode: {
css: { // Activate the «css» mode
render: {
css: true // Activate CSS output (with default options)
}
}
}
};
// Iconizr
gulp.task('iconizr', function() {
gulp.src('svgs/*.svg')
.pipe(svgSprite(config))
.pipe(gulp.dest('img/sprite'));
});
gulp.task('styles', function () {
var processors = [
autoprefixer({browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4']}),
simplevars,
nestedcss
];
return gulp.src('source/preCSS/*.css')
.pipe(fileinclude())
.pipe(postcss(processors))
// .pipe(postcss(processors).on('error', gutil.log))
.pipe(gulp.dest('./dist'));
});
gulp.task('fileinclude', function() {
gulp.src(['source/index.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: 'source/includes/'
}))
.pipe(gulp.dest('./'))
.pipe(reload({stream:true}))
.pipe(notify({ message: 'HTML built' }));
});
// Concatenate & Minify JS
gulp.task('scripts', function() {
return gulp.src('source/js/*.js')
.pipe(sourcemaps.init())
.pipe(concat('all.js'))
.pipe(gulp.dest('dist/'))
.pipe(rename('all.min.js'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist/'))
.pipe(reload({stream:true}));
});
// Images
gulp.task('images', function() {
return gulp.src('source/img/**/*')
.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest('dist/images'))
.pipe(notify({ message: 'Images task complete' }));
});
// Watch
gulp.task('watch', function() {
// Watch .scss files
gulp.watch('source/preCSS/**/*.css', ['styles', browserSync.reload]);
// Watch .js files
gulp.watch(['source/js/**/*.js','source/main.js'], ['scripts', browserSync.reload]);
// Watch .html partial files
gulp.watch(['source/includes/**/*', 'source/*.html'], ['fileinclude']);
// Watch image files
gulp.watch('source/img/**/*', ['images']);
// Watch any files in root html, reload on change
gulp.watch("*.html", browserSync.reload);
});
gulp.task('default', ['styles', 'browser-sync', 'scripts', 'watch']);
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Quick tests</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="dist/styles.css">
</head>
<body>
@@include('_menu.html')
<script src="dist/all.min.js"></script>
</body>
</html>
var menuExpose = document.querySelector('.hd-Nav_Expose');
var bodyNode = document.querySelector('body');
menuExpose.addEventListener('touchstart', function(event) {
event.preventDefault();
console.log('I was touched ' + event.type);
bodyNode.classList.add('is-ExposedMenu');
}, false);
menuExpose.addEventListener('click', function(event) {
event.preventDefault();
console.log('I was touched ' + event.type);
bodyNode.classList.add('is-ExposedMenu');
}, false);
{
"name": "Quick-setup",
"description": "Basic Gulp niceties for rapidly building stuff",
"version": "0.0.2",
"author": "Ben Frain",
"private": true,
"devDependencies": {
"autoprefixer-core": "^5.1.5",
"browser-sync": "^2.0.0-rc4",
"css-mqpacker": "^3.0.1",
"csswring": "^3.0.1",
"gulp": "^3.8.10",
"gulp-autoprefixer": "~2.1.0",
"gulp-cache": "~0.2.4",
"gulp-concat": "~2.4.2",
"gulp-file-include": "^0.8.0",
"gulp-imagemin": "~2.0.0",
"gulp-include": "^1.1.1",
"gulp-jshint": "~1.9.0",
"gulp-notify": "~2.0.1",
"gulp-postcss": "^4.0.3",
"gulp-rename": "~1.2.0",
"gulp-sourcemaps": "^1.5.0",
"gulp-svg-sprite": "^1.0.18",
"gulp-uglify": "~1.0.1",
"gulp-util": "^3.0.4",
"postcss": "^4.0.4",
"postcss-nested": "^0.2.1",
"postcss-simple-vars": "^0.2.3",
"vinyl": "^0.4.6"
}
}

dist [populated by build] source -- includes [html] -- js -- preCSS [un built CSS] index.html [gets built into root index.html [gets generated]

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