-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
61 lines (49 loc) · 1.31 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
const { series, src, dest, task, parallel, gutil } = require('gulp');
const uglify = require('gulp-uglify-es').default;
const rename = require('gulp-rename');
const watch = require('gulp-watch');
const log = require('fancy-log');
const { spawn } = require('child_process');
let workerProcess
const jsFiles = ['./js/app.js', 'main.js'];
const start = async (st) => {
workerProcess = spawn('npm', ['start'], {
stdio: 'inherit'
})
workerProcess.on('data', (data) => {
console.log(data);
});
workerProcess.on('close', (code) => {
console.log(`子进程退出 ${code}`);
});
}
const stop = async () => {
workerProcess && workerProcess.pid && process.kill(workerProcess.pid);
}
const restart = async () => {
await stop();
await start('restart');
}
const compile = async (vinylFile) => {
const out = src(jsFiles)
.pipe(uglify())
.pipe(rename({
suffix: ".min",
}))
.pipe(dest('./'));
if (vinylFile.relative && vinylFile.relative.includes('main.js')) {
restart()
}
return out;
}
const watchFile = async () => {
return await watch(jsFiles, (vinylFile) => {
log(vinylFile.relative)
compile(vinylFile);
});
}
task('start', start);
task('watch', watchFile);
task('compile', compile);
task('default', parallel('watch', 'start'));
task('build', series('compile'));