-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
62 lines (57 loc) · 1.52 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
/*
* @Author: Damom
* @Date: 2020-08-20 22:56:45
* @LastEditors: Damom
* @LastEditTime: 2020-12-05 14:57:08
* @Description: file content
*/
let gulp = require('gulp')
let nodemon = require('gulp-nodemon')
let path = require('path')
let browserSync = require('browser-sync').create()
let ROOT = path.resolve(__dirname)
let server = path.resolve(ROOT, 'src/mock')
// browser-sync配置,配置里启动nodemon任务
gulp.task('browser-sync', ['nodemon'], function () {
browserSync.init(null, {
proxy: "http://localhost:8080", //与webpack端口一致
port: 8181
})
})
// browser-sync 监听文件
gulp.task('mock', ['browser-sync'], function () {
gulp.watch(['./src/mock/server/db.js', './src/mock/server/**'], ['bs-delay'])
})
// 延时刷新
gulp.task('bs-delay', function () {
setTimeout(function () {
browserSync.reload()
console.log('restart')
}, 1000)
})
// 服务器重启
gulp.task('nodemon', function (cb) {
// 设个变量来防止重复重启
let started = false
let stream = nodemon({
script: './src/mock/server/index.js',
// 监听文件的后缀
ext: "js",
env: {
'NODE_ENV': 'development'
},
// 监听的路径
watch: [
server
]
})
stream.on('start', function () {
if (!started) {
cb()
started = true
}
}).on('crash', function () {
console.error('Application has crashed!\n')
stream.emit('restart', 10)
})
})