-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
148 lines (130 loc) · 3.55 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
143
144
145
146
147
148
// global
var del = require('del');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserSync = require('browser-sync');
var browserReload = browserSync.reload;
var deploy = require('gulp-gh-pages');
var options = require('minimist')(process.argv.slice(2));
// html
var jade = require('gulp-jade');
var htmlmin = require('gulp-minify-html');
// css
var sass = require('gulp-sass');
var prefix = require('gulp-autoprefixer');
var cssmin = require('gulp-cssmin');
// js
var jshint = require('gulp-jshint');
var stylish = require('jshint-stylish');
var uglify = require('gulp-uglify');
// imgs
var imagemin = require('gulp-imagemin');
var pngcrush = require('imagemin-pngcrush');
// opts
var opts = {
del: { force: true },
jade: { pretty: true },
htmlmin: { comments: false },
sass: {
sourceMap: 'none',
sourceComments: 'map'
},
browserify: {
debug: true,
insertGlobals: false
},
browserSync: {
// open: false,
server: { baseDir: 'dist/' }
}
};
// env
var inProd = options.prod;
// handle errors
var handleError = function(err) {
var taskErrorMessage = 'An error occurred';
var errorMessage = err.toString();
gutil.log(gutil.colors.red(taskErrorMessage));
gutil.log(gutil.colors.red(errorMessage));
this.emit('end');
};
// html task
gulp.task('html', function(){
if(inProd)
del('dist/*.html', opts.del);
return gulp.src(['./src/jade/**/*.jade', '!./src/jade/partials/**'])
.pipe(inProd ? jade() : jade(opts.jade))
.on('error', handleError)
.pipe(inProd ? htmlmin(opts.htmlmin) : gutil.noop())
.pipe(gulp.dest('./dist/'))
.pipe(inProd ? gutil.noop() : browserReload({ stream: true }));
});
// css task
gulp.task('css', function(){
return gulp.src('./src/scss/*.scss')
.pipe(inProd ? sass() : sass(opts.sass))
.on('error', handleError)
.pipe(prefix('last 1 version', '> 1%', 'ie 8', 'ie 7'))
.on('error', handleError)
.pipe(inProd ? cssmin() : gutil.noop())
.pipe(gulp.dest('./dist/css/'))
.pipe(inProd ? gutil.noop() : browserReload({ stream: true }));
});
// js task
gulp.task('js', function(){
gulp.src('./src/js/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(inProd ? uglify() : gutil.noop())
.pipe(gulp.dest('dist/js/'))
.pipe(inProd ? gutil.noop() : browserReload({ stream: true, once: true}));
});
// webfonts task
gulp.task('fonts', function(){
return gulp.src('./src/fonts/**')
.pipe(gulp.dest('./dist/fonts/'))
});
// imgs task
gulp.task('imgs', function(){
return gulp.src('src/imgs/**')
.pipe(inProd ? imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngcrush()]
}) : gutil.noop())
.pipe(gulp.dest('dist/imgs/'));
});
// watch task
gulp.task('watch', function(){
gulp.watch('src/js/**', ['js']);
gulp.watch('src/scss/**', ['css']);
gulp.watch('src/jade/**', ['html']);
gulp.watch('src/imgs/**', ['imgs']);
gulp.watch('src/fonts/**', ['fonts']);
});
// serve task
gulp.task('serve', function(){
browserSync.init(opts.browserSync);
});
// group tasks
gulp.task('generate-files', [
'prebuild',
'html',
'imgs',
'fonts',
'css',
'js'
]);
gulp.task('default', [
'generate-files',
'serve',
'watch'
]);
// build tasks
gulp.task('prebuild', function(){
return del('dist/', opts.del);
});
gulp.task('deploy', function(){
return gulp.src('./dist/**/*')
.pipe(deploy());
});