-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
97 lines (88 loc) · 2.98 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var gulp = require('gulp');
var sass = require('gulp-sass'); // convert sass files to css
var useref = require('gulp-useref'); // concatinate js/css files
var gulpIf = require('gulp-if'); // ensure file type
var uglify = require('gulp-uglify'); // minify js files
var cssnano = require('gulp-cssnano'); // minify css files
var imagemin = require('gulp-imagemin'); // optimize images
var cache = require('gulp-cache'); // cache optimized images locally
var runSequence = require('run-sequence'); // run tasks in order
var del = require('del'); // cleaning/deleting unused files
var sourcemaps = require('gulp-sourcemaps'); // sourcemaps for js
var babel = require('gulp-babel'); // compile es6
var concat = require('gulp-concat'); //
var browserSync = require('browser-sync').create(); // sync and reload browser
// build function to optimize project build
gulp.task('build', function(callback) {
runSequence('clean:dist', ['sass', 'es6', 'useref', 'images'], callback);
});
// default task for production serving runs browser sync and reloads on files changes
gulp.task('default', function(callback) {
runSequence(['sass', 'es6', 'browserSync', 'watch'], callback);
});
// Converts all Sass files to CSS with gulp-sass
gulp.task('sass', function() {
return gulp
.src('app/scss/**/*.scss')
.pipe(sass())
.pipe(gulp.dest('app/css'))
.pipe(
browserSync.reload({
stream: true
})
);
});
// Compile es6 js
gulp.task('es6', function() {
return gulp
.src('app/**/*.js')
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(concat('main.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist'));
});
// Optimise images and cache them to save processing time
gulp.task('images', function() {
return (
gulp
.src('app/images/**/*.+(png|jpg|jpeg|gif|svg)')
// Caching images that ran through imagemin
.pipe(
cache(
imagemin({
interlaced: true
})
)
)
.pipe(gulp.dest('dist/images'))
);
});
// Initialises Browser Sync
gulp.task('browserSync', function() {
browserSync.init({
server: {
baseDir: 'app'
}
});
});
// Watches scss/html/js files and on save, reloads Browser Sync
// Runs sass task when a Sass file is saved
gulp.task('watch', function() {
gulp.watch('app/scss/**/*.scss', ['sass']);
gulp.watch('app/*.html', browserSync.reload);
gulp.watch('app/js/**/*.js', browserSync.reload);
});
// Concatinate and minify js and css files
gulp.task('useref', function() {
return gulp
.src('app/*.html')
.pipe(useref())
.pipe(gulpIf('*.js', uglify()))
.pipe(gulpIf('*.css', cssnano()))
.pipe(gulp.dest('dist'));
});
// Cleaning/deleting dist folder to be rebuilt without unused files
gulp.task('clean:dist', function() {
return del.sync('dist');
});