Skip to content

Instantly share code, notes, and snippets.

@Epenance
Created September 9, 2015 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Epenance/6b6f39b443c03fe166d5 to your computer and use it in GitHub Desktop.
Save Epenance/6b6f39b443c03fe166d5 to your computer and use it in GitHub Desktop.
/*global -$ */
'use strict';
// generated on 2015-09-03 using generator-modern-frontend 0.1.7
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var through2 = require('through2');
var browserify = require('browserify');
gulp.task('stylesheet', ['sprites'], function () {
return gulp.src('app/css/main.scss')
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'nested', // libsass doesn't support expanded yet
precision: 10,
includePaths: ['.'],
onError: console.error.bind(console, 'Sass error:')
}))
.on('error', function (error) {
console.log(error.stack);
this.emit('end');
})
.pipe($.postcss([
require('autoprefixer-core')({browsers: ['last 1 version']})
]))
.pipe($.sourcemaps.write())
.pipe(gulp.dest('.tmp/css'))
.pipe(reload({stream: true}));
});
gulp.task('sprites', function() {
var spritesPath = 'app/images/sprites';
var identifiers = fs.readdirSync(spritesPath).filter(function(spritePath) {
var stat = fs.statSync(spritesPath + '/' + spritePath);
return stat.isDirectory();
});
for (var i = 0; i < identifiers.length; i++) {
var spriteData = gulp.src(spritesPath + '/' + identifiers[i] + '/*.png').pipe($.spritesmith({
imgName: 'sprite_' + identifiers[i] + '.png',
cssName: identifiers[i] + '..scss',
imgPath: '../images/sprite_' + identifiers[i] + '.png',
cssFormat: 'sass'
}));
// Pipe image stream
spriteData.img
.pipe(gulp.dest('.tmp/images'))
.pipe(gulp.dest('dist/images'))
// Pipe CSS stream
spriteData.css
.pipe(gulp.dest('app/css/sprites'));
}
});
gulp.task('javascript', function () {
return gulp.src('app/js/main.js')
.pipe(through2.obj(function (file, enc, next){ // workaround for https://github.com/babel/babelify/issues/46
browserify(file.path)
.transform('babelify')
.transform('envify')
.bundle(function(err, res){
if (err) { return next(err); }
file.contents = res;
next(null, file);
});
}))
.on('error', function (error) {
console.log(error.stack);
this.emit('end');
})
.pipe(gulp.dest('dist/js'))
.pipe($.sourcemaps.init())
.pipe($.uglify())
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('.tmp/js'));
});
gulp.task('jshint', function () {
return gulp.src('app/js/**/*.js')
.pipe(reload({stream: true, once: true}))
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.if(!browserSync.active, $.jshint.reporter('fail')));
});
gulp.task('html', ['stylesheet'], function () {
var assets = $.useref.assets({searchPath: ['.tmp', 'app/*.html', '.']});
return gulp.src('app/*.html')
.pipe(assets)
//.pipe($.if('*.js', $.uglify()))
.pipe($.if('*.css', $.csso()))
.pipe(assets.restore())
.pipe($.useref())
.pipe($.if('*.html', $.minifyHtml({conditionals: true, loose: true})))
.pipe(gulp.dest('dist'));
});
gulp.task('images', function () {
return gulp.src('app/images/**/*')
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true,
// don't remove IDs from SVGs, they are often used
// as hooks for embedding and styling
svgoPlugins: [{cleanupIDs: false}]
})))
.pipe(gulp.dest('dist/images'));
});
gulp.task('fonts', function () {
return gulp.src(require('main-bower-files')({
filter: '**/*.{eot,svg,ttf,woff,woff2}'
}).concat('app/fonts/**/*'))
.pipe(gulp.dest('.tmp/fonts'))
.pipe(gulp.dest('dist/fonts'));
});
gulp.task('extras', function () {
return gulp.src([
'app/*.*',
'!app/*.html'
], {
dot: true
}).pipe(gulp.dest('dist'));
});
gulp.task('clean', require('del').bind(null, ['.tmp', 'dist']));
gulp.task('serve', ['stylesheet', 'javascript', 'fonts'], function () {
browserSync({
notify: false,
port: 9000,
server: {
baseDir: ['.tmp', 'app'],
routes: {
'/bower_components': 'bower_components'
}
}
});
// watch for changes
gulp.watch([
'app/*.html',
'.tmp/js/*.js',
'app/images/**/*',
'.tmp/fonts/**/*'
]).on('change', reload);
gulp.watch(['app/css/**/*.scss', '!app/css/sprites/*.scss'], ['stylesheet']);
gulp.watch('app/js/**/*.js', ['javascript']);
gulp.watch('app/fonts/**/*', ['fonts']);
gulp.watch('bower.json', ['wiredep', 'fonts']);
});
gulp.task('serve:dist', function () {
browserSync({
notify: false,
port: 9000,
server: {
baseDir: ['dist']
}
});
});
// inject bower components
gulp.task('wiredep', function () {
var wiredep = require('wiredep').stream;
gulp.src('app/css/*.scss')
.pipe(wiredep({
ignorePath: /^(\.\.\/)+/
}))
.pipe(gulp.dest('app/css'));
gulp.src('app/*.html')
.pipe(wiredep({
// exclude: ['bootstrap-sass-official'],
ignorePath: /^(\.\.\/)*\.\./
}))
.pipe(gulp.dest('app'));
});
gulp.task('build', ['jshint', 'javascript', 'stylesheet', 'html', 'images', 'fonts', 'extras'], function () {
return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true}));
});
gulp.task('default', ['clean'], function () {
gulp.start('build');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment