forked from jonashcroft/Nowify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
105 lines (95 loc) · 2.89 KB
/
gulpfile.babel.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
'use strict';
import gulp from 'gulp';
import plumber from 'gulp-plumber';
import browserify from 'browserify';
import babelify from 'babelify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import gutil from 'gulp-util';
import sass from 'gulp-sass';
import autoprefixer from 'gulp-autoprefixer';
import cssnano from 'gulp-cssnano';
import sourcemaps from 'gulp-sourcemaps';
import rename from 'gulp-rename';
import concat from 'gulp-concat';
import uglify from 'gulp-uglify';
import browserSync from 'browser-sync';
import notify from 'gulp-notify';
gulp.task('browser-sync', () => {
browserSync.init({
server: './'
});
});
/*------------------------------------
Our JS functions,
TODO: gulp-plumber doesn't work, gulp will
crash when this task errors.
------------------------------------*/
gulp.task('browserify', () => {
const b = browserify({
entries: './src/js/app.js',
debug: true,
});
return b
.transform(babelify)
.bundle()
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.on('error', gutil.log)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./src/'));
});
gulp.task('bundle', ['browserify'], () => {
return gulp.src([
'./src/app.js'
])
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(concat('app.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/'))
.pipe(browserSync.reload({stream: true}));
});
/*------------------------------------
Sass & CSS
TODO: There's a bug where if you make a
new file, this task won't pick up the new
files and gulp will need to be restarted.
------------------------------------*/
gulp.task('Styles', () => {
gulp.src('./src/scss/**/*.scss')
.pipe(plumber({ errorHandler: function(err) {
notify.onError({
title: 'Something\'s gone wrong',
message: 'Error: Sass could not compile'
})(err);
this.emit('end');
}}))
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest('./src/')) // unminified
.pipe(cssnano({
zindex: false
}))
.pipe(sourcemaps.write('./'))
.pipe(rename(function (path) {
if(path.extname === '.css') {
path.basename += '.min';
}
}))
.pipe(gulp.dest('./dist/'))
.pipe(browserSync.reload({stream: true}));
});
// Watch for changes and reload browser
gulp.task('default', ['browser-sync'], () => {
gulp.watch('./src/scss/**/*.scss', ['Styles']);
gulp.watch('./src/js/**/*.js', ['browserify', 'bundle']);
gulp.watch('**/*.html').on('change', browserSync.reload);
});