Last active
April 14, 2017 00:34
-
-
Save rodrigograca31/7e72d03e0c279a8b67ea77707a3186f1 to your computer and use it in GitHub Desktop.
Example gulpfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// including plugins | |
var gulp = require('gulp'); | |
var browserSync = require('browser-sync'); | |
var minifyHtml = require('gulp-minify-html'); | |
var minifyCss = require('gulp-minify-css'); | |
var uglify = require('gulp-uglify'); | |
var runSequence = require('run-sequence'); | |
var clean = require('gulp-clean'); | |
var injectPartials = require('gulp-inject-partials'); | |
var imagemin = require('gulp-imagemin'); | |
var sitemap = require('gulp-sitemap'); | |
var folder = './_site/'; | |
var siteUrl = 'https://31apps.com/'; | |
// npm init | |
// npm install gulp browser-sync gulp-minify-html gulp-minify-css gulp-uglify run-sequence gulp-clean gulp-inject-partials gulp-sitemap gulp-imagemin --save | |
// https://julienrenaux.fr/2014/05/25/introduction-to-gulp-js-with-practical-examples/ | |
gulp.task('default', ['maintask'], function (callback) { | |
//runs maintask first, then: | |
return runSequence('serve', 'watch', callback); | |
}); | |
gulp.task('maintask', function(callback) { | |
// it runs 'clean' then 'copyall' then the other ones in parallel then callback | |
return runSequence( | |
'clean', | |
'copyall', | |
'sitemap', | |
['minify-html', 'minify-css', 'minify-js', 'minify-imgs'], | |
callback); | |
}); | |
gulp.task('watch',function() { | |
return gulp.watch(folder + '**/*', ['maintask', browserSync.reload]); | |
}); | |
gulp.task('serve', function() { | |
return browserSync({ | |
server: { | |
baseDir: 'dist' | |
}, | |
port: 3010 | |
}); | |
}); | |
gulp.task('clean', function () { | |
return gulp.src('./dist/', {read: false}) | |
.pipe(clean()); | |
}); | |
gulp.task('copyall', function () { | |
return gulp.src([folder + '**/*', folder + '*', folder + '.*']) | |
.pipe(gulp.dest('./dist/')); | |
}); | |
gulp.task('minify-html', function () { | |
return gulp.src(folder + '**/*.html') | |
.pipe(injectPartials()) | |
.pipe(minifyHtml()) | |
.pipe(gulp.dest('./dist/')); | |
}); | |
gulp.task('minify-css', function () { | |
return gulp.src(folder + '**/*.css') | |
.pipe(minifyCss()) | |
.pipe(gulp.dest('./dist/')); | |
}); | |
gulp.task('minify-js', function () { | |
return gulp.src(folder + '**/*.js') | |
.pipe(uglify()) | |
.pipe(gulp.dest('./dist/')); | |
}); | |
gulp.task('minify-imgs', function () { | |
return gulp.src(folder + '**/*.{png,jpg,gif,svg,jpeg}') | |
.pipe(imagemin()) | |
.pipe(gulp.dest('./dist/')); | |
}); | |
gulp.task('sitemap', function () { | |
gulp.src('dist/**/*.html', { | |
read: false | |
}) | |
.pipe(sitemap({ | |
siteUrl: siteUrl | |
})) | |
.pipe(gulp.dest('./dist/')); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"devDependencies": { | |
"browser-sync": "^2.18.8", | |
"gulp": "^3.9.1", | |
"gulp-clean": "^0.3.2", | |
"gulp-imagemin": "^3.1.1", | |
"gulp-inject-partials": "^1.0.2", | |
"gulp-minify-css": "^1.2.4", | |
"gulp-minify-html": "^1.0.6", | |
"gulp-uglify": "^2.0.1", | |
"run-sequence": "^1.2.2", | |
"sw-precache": "^5.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment