-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
74 lines (61 loc) · 1.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
const gulp = require('gulp')
const sourcemaps = require('gulp-sourcemaps')
const nodemon = require('gulp-nodemon')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const browserify = require('browserify')
const watchify = require('watchify')
function getTimeSegment(date, funcName) {
const timeSegment = date[funcName]()
if (timeSegment < 10) {
return `0${timeSegment}`
}
return timeSegment
}
function getTimestamp() {
const date = new Date()
const hours = getTimeSegment(date, 'getHours')
const minutes = getTimeSegment(date, 'getMinutes')
const seconds = getTimeSegment(date, 'getSeconds')
return `${hours}:${minutes}:${seconds}`
}
function compile(watch) {
const bundler = watchify(
browserify('./src/index.jsx', { debug: true }).
transform('babelify', { presets: ['es2015', 'react'] })
)
function rebundle() {
bundler.bundle().on('error', function(err) {
console.error(err)
this.emit('end')
}).pipe(source('bundle.js')).
pipe(buffer()).
pipe(sourcemaps.init({ loadMaps: true })).
pipe(sourcemaps.write('./')).
pipe(gulp.dest('./src/public')).
on('end', () => {
console.log(`[${getTimestamp()}] done!`)
})
}
function onUpdate() {
console.log(`[${getTimestamp()}] bundling...`)
rebundle()
}
if (watch) {
bundler.on('update', onUpdate)
}
rebundle()
}
function watch() {
return compile(true)
}
function build() {
return compile()
}
function serve() {
nodemon({ script: 'src/server.js', ignore: 'src/public/bundle.js' })
}
gulp.task('build', build)
gulp.task('watch', watch)
gulp.task('serve', serve)
gulp.task('default', ['watch', 'serve'])