-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
236 lines (184 loc) · 9.69 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var http = require('http') // node core module to serve data
var gulp = require('gulp') // gulp task runner
var watch = require('gulp-watch') // watch for files that change
var sass = require('gulp-sass') // compiles sass
var autoprefixer = require('gulp-autoprefixer') // applies CSS vendor prefixes
var rename = require('gulp-rename') // renames files
var livereload = require('gulp-livereload') // reload browser when files change
var concat = require('gulp-concat') // concatenate scripts
var serveStatic = require('serve-static') // serves static files
var finalhandler = require('finalhandler') // standardizes server response
var opn = require('opn') // opens the browser when we gulp
var jshint = require('gulp-jshint') // catches errors in javascript
var stylish = require('jshint-stylish') // makes lint errors look nicer
var plumber = require('gulp-plumber') // keeps pipes working even when error var ppens
var notify = require('gulp-notify') // system notification when error happevar
// paths to files that are used in the project
var paths = {
styles: './src/scss/**/*',
scripts: {
vendor: './src/js/vendor/**/*',
app: './src/js/app/**/*',
},
images: './src/img/**/*',
pages: './src/html/**/*',
dist: './dist'
}
// these tasks execute in order when you run gulp
gulp.task('default', ['styles', 'scripts', 'images', 'html', 'serve', 'watch', 'livereload-listen', 'open'])
/*
███████╗████████╗██╗ ██╗██╗ ███████╗███████╗
██╔════╝╚══██╔══╝╚██╗ ██╔╝██║ ██╔════╝██╔════╝
███████╗ ██║ ╚████╔╝ ██║ █████╗ ███████╗
╚════██║ ██║ ╚██╔╝ ██║ ██╔══╝ ╚════██║
███████║ ██║ ██║ ███████╗███████╗███████║
╚══════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝
*****************************************************/
// compiles scss files into one, starting from style.scss
// sass searches style.scss for import statements and includes those files
// also minifies resulting css and auto-prefixes for browser compatibility
gulp.task('styles', [], function(){
// show notification on scss error
var scssError = function(err){
notify.onError({
title: err.relativePath,
subtitle: 'Line '+err.line,
message: '<%= error.messageOriginal %>'
})(err)
this.emit('end')
}
// do the scss compilation
gulp.src('./src/scss/style.scss')
.pipe(plumber({
errorHandler: scssError
}))
.pipe(sass({outputStyle: 'compressed'}))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(rename('style.css'))
.pipe(gulp.dest(paths.dist))
.pipe(livereload())
})
/*
███████╗ ██████╗██████╗ ██╗██████╗ ████████╗███████╗
██╔════╝██╔════╝██╔══██╗██║██╔══██╗╚══██╔══╝██╔════╝
███████╗██║ ██████╔╝██║██████╔╝ ██║ ███████╗
╚════██║██║ ██╔══██╗██║██╔═══╝ ██║ ╚════██║
███████║╚██████╗██║ ██║██║██║ ██║ ███████║
╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝╚═╝ ╚═╝ ╚══════╝
********************************************************/
// compiles all JS files into one file
// first concatenates js in vendor folder, then js in app folder
gulp.task('scripts', ['lint'], function(){
return gulp.src([paths.scripts.vendor,paths.scripts.app])
.pipe(concat('main.js'))
.pipe(gulp.dest(paths.dist))
.pipe(livereload())
})
// shows notification on js error
gulp.task('lint',function(){
return gulp.src(paths.scripts.app)
.pipe(plumber())
.pipe(jshint({
'asi': true, // allows missing semicolons
'loopfunc': true, // allows functions inside loop
'sub': true // allows dot and bracket notation
}))
.pipe(jshint.reporter('jshint-stylish'))
.pipe(notify(function (file) { // Use gulp-notify as jshint reporter
if (file.jshint.success) {
return false // Don't show something if success
}
var errors = file.jshint.results.map(function (data) {
if (data.error) {
return "(" + data.error.line + ':' + data.error.character + ') ' + data.error.reason
}
}).join("\n")
return file.relative + " (" + file.jshint.results.length + " errors)\n" + errors
}))
})
/*
██╗ ██╗████████╗███╗ ███╗██╗
██║ ██║╚══██╔══╝████╗ ████║██║
███████║ ██║ ██╔████╔██║██║
██╔══██║ ██║ ██║╚██╔╝██║██║
██║ ██║ ██║ ██║ ╚═╝ ██║███████╗
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝
*************************************/
// just copies HTML to the dist folder, no compilation step
// this exists to gain the benefit of livereload when editing pages
gulp.task('html',['styles','images'], function () {
return gulp.src(paths.pages)
.pipe(gulp.dest(paths.dist))
.pipe(livereload())
})
/*
██╗ ███╗ ███╗ █████╗ ██████╗ ███████╗ ███████╗
██║ ████╗ ████║ ██╔══██╗ ██╔════╝ ██╔════╝ ██╔════╝
██║ ██╔████╔██║ ███████║ ██║ ███╗ █████╗ ███████╗
██║ ██║╚██╔╝██║ ██╔══██║ ██║ ██║ ██╔══╝ ╚════██║
██║ ██║ ╚═╝ ██║ ██║ ██║ ╚██████╔╝ ███████╗ ███████║
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚══════╝
****************************************************/
// just copies images to the dist folder, no processing
// this exists to gain the benefit of livereload when changing images
gulp.task('images', function(){
return gulp.src(paths.images,{base:'./src/img/'})
.pipe(gulp.dest(paths.dist+'/img'))
.pipe(livereload())
})
/*
███████╗███████╗██████╗ ██╗ ██╗███████╗██████╗
██╔════╝██╔════╝██╔══██╗██║ ██║██╔════╝██╔══██╗
███████╗█████╗ ██████╔╝██║ ██║█████╗ ██████╔╝
╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██╔══╝ ██╔══██╗
███████║███████╗██║ ██║ ╚████╔╝ ███████╗██║ ██║
╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝
****************************************************/
// running gulp will start a little server in the dist folder
gulp.task('serve',['html'], function() {
// Serve up dist folder
var serve = serveStatic(paths.dist)
// Start server
var server = http.createServer(function(req, res){
var done = finalhandler(req, res)
serve(req, res, done)
})
// Listen for requests on port 3000
// http://localhost:3000
server.listen(3000)
})
// opens browser to the page
gulp.task('open',['serve'], function(){
// opn('http://localhost:3000')
})
// tell livereload to start listening for changed files
// when a file changes the browser reloads itself
gulp.task('livereload-listen', function(){
livereload.listen()
})
/*
██╗ ██╗ █████╗ ████████╗ ██████╗██╗ ██╗
██║ ██║██╔══██╗╚══██╔══╝██╔════╝██║ ██║
██║ █╗ ██║███████║ ██║ ██║ ███████║
██║███╗██║██╔══██║ ██║ ██║ ██╔══██║
╚███╔███╔╝██║ ██║ ██║ ╚██████╗██║ ██║
╚══╝╚══╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝
**********************************************/
// gulp watches the filesystem for changes, then compiles the according files
gulp.task('watch',['serve'], function(){
watch(paths.styles,function(){
gulp.start('styles')
})
watch([paths.scripts.app,paths.scripts.vendor],function(){
gulp.start('scripts')
})
watch(paths.pages,function(){
gulp.start('html')
})
watch(paths.images,function(){
gulp.start('images')
})
})