-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
158 lines (141 loc) · 4.84 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
149
150
151
152
153
154
155
156
157
158
/*!
* gulp
* $ npm install gulp-autoprefixer gulp-minify-css gulp-jshint gulp-concat gulp-uglify gulp-imagemin gulp-notify gulp-rename gulp-livereload gulp-cache del gulp-html-replace gulp-connect --save-dev
*/
// Load plugins
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
del = require('del'),
htmlreplace = require('gulp-html-replace'),
connect = require('gulp-connect');
// Our task config
var runList = [
'styles',
// 'images',
'scripts',
'html'
];
var createStream = function(){
// Actually nothing needed here, this function just required to create stream "on('data')"
}
var taskFunctions = {
styles: function () {
return new Promise(function (resolve, reject) {
gulp.src('src/styles/**/*.css')
.pipe(autoprefixer('last 2 version'))
.pipe(gulp.dest('dist/styles'))
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss())
.pipe(gulp.dest('dist/styles'))
.pipe(notify({ message: 'Styles task complete' }))
.on('data', createStream)
.on('end', resolve)
.on('error', reject);
});
},
scripts: function () {
return new Promise(function (resolve, reject) {
gulp.src(['src/scripts/utils/*.js', 'src/scripts/*.js'])
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(concat('all.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(rename({ suffix: '.min' }))
// .pipe(uglify())
.pipe(gulp.dest('dist/scripts'))
.pipe(notify({ message: 'Scripts task complete' }))
.on('data', createStream)
.on('end', resolve)
.on('error', reject);
});
},
images: function(){
return new Promise(function(resolve, reject) {
gulp.src('src/images/**/*')
.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest('dist/images'))
.pipe(notify({ message: 'Images task complete' }))
.on('data', createStream)
.on('end', resolve)
.on('error', reject);
});
},
html: function(){
return new Promise(function(resolve, reject) {
gulp.src('src/*.html')
.pipe(htmlreplace({
// 'css': 'styles.min.css',
'js': 'scripts/all.min.js'
}))
.pipe(gulp.dest('dist'))
.pipe(notify({message: 'HTML task complete'}))
.on('data', createStream)
.on('end', resolve)
.on('error', reject);
});
}
}
// Create our gulp tasks
function createGulpTask(taskName, taskFunction){
gulp.task(taskName, function(){
return taskFunction();
})
}
for(var i in taskFunctions){
createGulpTask(i, taskFunctions[i]);
}
// Clean task
gulp.task('clean', function(cb) {
return del(['dist/*'], cb)
});
// Build task
gulp.task('build', function(){
// Promises
var promiseList = [];
// Run the tasks listed in variable "runList"
for(var i = 0; !!runList && i < runList.length; i++){
promiseList[promiseList.length] = taskFunctions[runList[i]]();
console.info((i+1) + ' of ' + runList.length + ' tasks started');
}
// When all tasks completed
Promise.all(promiseList).then(function(){
console.info('All build tasks complete');
});
});
// Default task
gulp.task('default', ['clean'], function() {
gulp.start('build');
});
// Watch
gulp.task('watch', function() {
// Watch .css files
gulp.watch('src/styles/**/*.css', ['styles']);
// Watch .js files
gulp.watch('src/scripts/**/*.js', ['scripts']);
// Watch image files
gulp.watch('src/images/**/*', ['images']);
// Watch html files
gulp.watch('src/*.html', ['html']);
// Create LiveReload server
livereload.listen(); // This livereload is for when "node server.js" is run.
// Watch any files in dist/, reload on change
gulp.watch(['dist/**']).on('change', livereload.changed);
});
// Test - dependency on build and watch
// Alternative is to run "node server.js"
gulp.task('test', ['build', 'watch'], function(){
connect.server({
livereload: true, // This livereload is for when "gulp test" is run.
root: 'dist',
port: 3000
});
});