-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
142 lines (116 loc) · 3.91 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
// Load Gulp and plugins
var gulp = require('gulp'),
$ = require('gulp-load-plugins')(),
production = !!$.util.env.production,
path = require('path'),
del = require('del'),
browserSync = require('browser-sync'),
browserify = require('browserify'),
through2 = require('through2'),
paths = {
styles: {
dist: path.join('dist', 'styles'),
src: path.join('src', 'styles')
},
scripts: {
dist: path.join('dist', 'scripts'),
src: path.join('src', 'scripts')
}
},
config = {
autoprefixer: {
browsers: [
'last 2 versions'
]
},
browserify: {
cache: {},
debug: !production,
packageCache: {},
fullPaths: false,
extensions: ['.hbs']
},
browserSync: {
server: {
baseDir: './',
proxy: 'zonebpgulp.dev'
}
},
sass: {
style: 'expanded',
sourceComments: 'map',
errLogToConsole: true
}
},
errorLogger = function(error) {
return $.util.log($.util.colors.red.bold('Error' + (error.plugin ? ': ' + error.plugin : '')), '\n\n' + error.message + '\n');
};
$.stats(gulp);
// What mode?
$.util.log('Running in', (production ? $.util.colors.red.bold('production') : $.util.colors.green.bold('development')), 'mode');
// Styles
gulp.task('styles', function() {
// @todo: Add sourcemaps once a fix is released for autoprefixer and sourcemaps.
return gulp.src(path.join(paths.styles.src, '*.scss'))
.pipe($.sass(config.sass))
.on('error', errorLogger)
.pipe($.autoprefixer(config.autoprefixer))
.on('error', errorLogger)
.pipe($.minifyCss())
.pipe(gulp.dest(path.join(paths.styles.dist)))
.pipe(browserSync.reload({ stream: true }));
});
// JSHint
gulp.task('jshint', function() {
return gulp.src([
path.join(paths.scripts.src, '**', '*.js'),
'!' + path.join(paths.scripts.src, 'libs', '*.js'),
'!' + path.join(paths.scripts.src, 'plugins', '*.js'),
'!' + path.join(paths.scripts.src, '*.min.js'),
'!' + path.join(paths.scripts.src, 'modernizr.js')
])
.pipe($.jshint('.jshintrc'))
.pipe($.jshint.reporter('jshint-stylish'));
});
// Scripts
gulp.task('scripts', function() {
gulp.src(path.join(paths.scripts.src, 'app.js'))
.pipe(through2.obj(function (file, enc, next){
browserify(config.browserify)
.add(file.path)
.bundle(function(err, res){
// assumes file.contents is a Buffer
file.contents = res;
next(null, file);
})
.on('error', function(err) {
errorLogger(err);
this.emit('end');
});
}))
.pipe($.uglify())
.pipe(gulp.dest(paths.scripts.dist))
.pipe(browserSync.reload({ stream: true }));
});
// Clean
gulp.task('clean', function(cb) {
return del('dist', cb);
});
// Watch
gulp.task('watch', ['scripts', 'styles'], function() {
// Create browserSync server
browserSync(config.browserSync);
// Watch .scss files
gulp.watch(path.join(paths.styles.src, '**', '*.scss'), ['styles']);
// Watch .js and .hbs files
gulp.watch(path.join(paths.scripts.src, '**', '*.js'), ['jshint', 'scripts']);
// Watch .html files
gulp.watch(path.join('**', '*.html'), browserSync.reload);
// Watch .hbs files
gulp.watch(path.join('**', '*.hbs'), ['jshint', 'scripts']);
});
// Default
gulp.task('default', ['clean'], function() {
return gulp.start(production ? ['scripts', 'styles'] : ['jshint', 'scripts', 'styles', 'watch']);
});