This repository was archived by the owner on May 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathgulpfile.babel.js
94 lines (84 loc) · 2.42 KB
/
gulpfile.babel.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
import gulp from 'gulp'
import plumber from 'gulp-plumber'
import sass from 'gulp-sass'
import sourcemaps from 'gulp-sourcemaps'
import browserify from 'browserify'
import source from 'vinyl-source-stream'
import buffer from 'vinyl-buffer'
import uglify from 'gulp-uglify'
import exorcist from 'exorcist'
import ifElse from 'gulp-if'
import browserSync from 'browser-sync'
import merge from 'merge-stream'
import nodemon from 'gulp-nodemon'
let bs = browserSync.create()
let errorHandler = function(msgSource) {
return function({message, plugin = msgSource}){
console.error( `\n${plugin}: ${message}\n`)
this.emit('end')
}
}
gulp.task('makeStyle',() => {
return gulp.src('public/styles/src/main.scss')
.pipe(plumber(errorHandler('makeStyle')))
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed',
includePaths: [
'node_modules',
'public/styles/src/includes'
]
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('public/styles/'))
.pipe(bs.stream())
})
gulp.task('makeScript',() => {
const ENTRY_SCRIPTS = [
'public/scripts/src/main.js'
]
return merge(ENTRY_SCRIPTS.map((entry) => {
let filePath = entry.split('.').shift()
let fileName = filePath.split('/').pop()
return browserify(entry,{ debug: true })
.transform('babelify')
.bundle()
.on('error', errorHandler('browserify'))
.pipe(exorcist(`public/scripts/${fileName}.js.map`))
.pipe(source(`${fileName}.js`))
.pipe(plumber(errorHandler('makeScript')))
.pipe(buffer())
.pipe(ifElse(process.env.NODE_ENV === 'production', uglify()))
.pipe(gulp.dest('public/scripts/'))
}))
})
gulp.task('scriptWatch',['makeScript'], function(done) {
bs.reload()
done()
})
gulp.task('startServer', function (cb) {
let started = false
nodemon({
script: 'index.js',
ext: 'js',
ignore: ['gulpfile.babel.js', 'public', 'views']
}).on('start', function () {
if (!started) {
cb()
started = true
}
}).on('restart', function () {
bs.reload()
})
})
gulp.task('watch', ['makeStyle','makeScript','startServer'], () => {
bs.init({
proxy: 'localhost:5000',
port: 3000,
notify: true
})
gulp.watch(['public/styles/src/**/**/**'], ['makeStyle'])
gulp.watch(['public/scripts/src/**/**/**'], ['scriptWatch'])
gulp.watch(['views/*.pug'], () => { bs.reload() })
})
gulp.task('default', ['makeStyle','makeScript'])