-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
107 lines (94 loc) · 2.97 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
98
99
100
101
102
103
104
105
106
107
//------------------//
// Define Variables //
//------------------//
var browserSync = require('browser-sync');
var gulp = require('gulp');
var sass = require('gulp-sass');
var useref = require('gulp-useref');
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');
var cssnano = require('gulp-cssnano');
var del = require('del');
var postcss = require('gulp-postcss');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
var runSequence = require('run-sequence');
//-------------------//
// Development Tasks //
//-------------------//
// Start browserSync server
gulp.task('browserSync', function () {
browserSync.init({
server: {
baseDir: "app"
}
// Tunnel has some issues currently
//, tunnel: "ssmale"
});
});
// Converts SCSS to CSS, Prefixes for the last two browser vesions and provides source maps to assit in debugging
gulp.task('sass', function () {
return gulp.src('app/scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', function (err) {
console.error(err.message);
browserSync.notify(err.message, 30000); // Display error in the browser
this.emit('end'); // Prevent gulp from catching the error and exiting the watch process
})) // Converts Sass to CSS with gulp-sass
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('app/css'))
.pipe(browserSync.reload({
stream: true
}))
});
// Gulp watch
gulp.task('watch', ['sass'], function () {
gulp.watch('app/scss/**/*.scss', ['sass']);
// Reloads the browser whenever HTML or JS files change
gulp.watch('app/*.html', browserSync.reload);
gulp.watch('app/js/**/*.js', browserSync.reload);
})
//------------------//
// Production Tasks //
//------------------//
// Minifies the JS and CSS files
gulp.task('useref', function () {
return gulp.src('app/*.html')
.pipe(useref())
// Minifies only if it's a JavaScript file
.pipe(gulpIf('*.js', uglify()))
// Minifies only if it's a CSS file
.pipe(gulpIf('*.css', cssnano()))
.pipe(gulp.dest('dist'))
});
// Moves custom fonts to the production folder
gulp.task('fonts', function () {
return gulp.src('app/fonts/**/*')
.pipe(gulp.dest('dist/fonts'))
});
// Moves images to the production folder
gulp.task('images', function () {
return gulp.src('app/images/**/*')
.pipe(gulp.dest('dist/images'))
});
// Deletes the production folder to remove deprecated files
gulp.task('clean:dist', function () {
return del.sync('dist');
});
//------------------//
// Run Gulp Tasks //
//------------------//
gulp.task('build', function (callback) {
runSequence('clean:dist', ['sass', 'useref', 'fonts', 'images'],
callback
)
});
gulp.task('default', function (callback) {
runSequence(['browserSync', 'sass'], 'watch',
callback
)
});