From 8aab28bf5c58136fc01ee66e10b06bb578f04c96 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Wed, 23 Sep 2015 19:37:02 -0700 Subject: [PATCH] :goat: Bring more inline with gulp best practices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract non-specific tasks to fns to be more inline with "the gulp way✨" Fixes #289 --- gulpfile.js | 86 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 8b5669192..d8dd0a6a0 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -49,6 +49,50 @@ var styleTask = function (stylesPath, srcs) { .pipe($.size({title: stylesPath})); }; +var jshintTask = function (src) { + return gulp.src(src) + .pipe(reload({stream: true, once: true})) + .pipe($.jshint.extract()) // Extract JS from .html files + .pipe($.jshint()) + .pipe($.jshint.reporter('jshint-stylish')) + .pipe($.if(!browserSync.active, $.jshint.reporter('fail'))); +} + +var imageOptimizeTask = function (src, dest) { + return gulp.src(src) + .pipe($.cache($.imagemin({ + progressive: true, + interlaced: true + }))) + .pipe(gulp.dest(dest)) + .pipe($.size({title: 'images'})); +} + +var optimizeHtmlTask = function(src) { + var assets = $.useref.assets({searchPath: ['.tmp', 'app', 'dist']}); + + return gulp.src(src) + // Replace path for vulcanized assets + .pipe($.if('*.html', $.replace('elements/elements.html', 'elements/elements.vulcanized.html'))) + .pipe(assets) + // Concatenate and minify JavaScript + .pipe($.if('*.js', $.uglify({preserveComments: 'some'}))) + // Concatenate and minify styles + // In case you are still using useref build blocks + .pipe($.if('*.css', $.cssmin())) + .pipe(assets.restore()) + .pipe($.useref()) + // Minify any HTML + .pipe($.if('*.html', $.minifyHtml({ + quotes: true, + empty: true, + spare: true + }))) + // Output files + .pipe(gulp.dest(dest)) + .pipe($.size({title: 'html'})); +} + // Compile and automatically prefix stylesheets gulp.task('styles', function () { return styleTask('styles', ['**/*.css']); @@ -60,27 +104,16 @@ gulp.task('elements', function () { // Lint JavaScript gulp.task('jshint', function () { - return gulp.src([ + return jshint([ 'app/scripts/**/*.js', 'app/elements/**/*.js', 'app/elements/**/*.html' - ]) - .pipe(reload({stream: true, once: true})) - .pipe($.jshint.extract()) // Extract JS from .html files - .pipe($.jshint()) - .pipe($.jshint.reporter('jshint-stylish')) - .pipe($.if(!browserSync.active, $.jshint.reporter('fail'))); + ]); }); // Optimize images gulp.task('images', function () { - return gulp.src('app/images/**/*') - .pipe($.cache($.imagemin({ - progressive: true, - interlaced: true - }))) - .pipe(gulp.dest('dist/images')) - .pipe($.size({title: 'images'})); + return imageOptimizeTask('app/images/**/*', 'dist/images'); }); // Copy all files at the root level (app) @@ -123,28 +156,9 @@ gulp.task('fonts', function () { // Scan your HTML for assets & optimize them gulp.task('html', function () { - var assets = $.useref.assets({searchPath: ['.tmp', 'app', 'dist']}); - - return gulp.src(['app/**/*.html', '!app/{elements,test}/**/*.html']) - // Replace path for vulcanized assets - .pipe($.if('*.html', $.replace('elements/elements.html', 'elements/elements.vulcanized.html'))) - .pipe(assets) - // Concatenate and minify JavaScript - .pipe($.if('*.js', $.uglify({preserveComments: 'some'}))) - // Concatenate and minify styles - // In case you are still using useref build blocks - .pipe($.if('*.css', $.cssmin())) - .pipe(assets.restore()) - .pipe($.useref()) - // Minify any HTML - .pipe($.if('*.html', $.minifyHtml({ - quotes: true, - empty: true, - spare: true - }))) - // Output files - .pipe(gulp.dest('dist')) - .pipe($.size({title: 'html'})); + return optimizeHtmlTask( + ['app/**/*.html', '!app/{elements,test}/**/*.html'], + 'dist'); }); // Polybuild will take care of inlining HTML imports,