-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
66 lines (54 loc) · 1.77 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
"use strict";
var gulp = require("gulp"),
$ = require("gulp-load-plugins")(),
source = require("vinyl-source-stream"),
browserify = require("browserify"),
watchify = require("watchify"),
path = require("path"),
fs = require("fs"),
babelify = require("babelify");
gulp.task("scripts:server", () => {
return gulp.src("./src-server/**/*.js")
.pipe($.cached("server"))
.pipe($.babel())
.pipe(gulp.dest("./build"));
})
gulp.task("watch:scripts:server", gulp.series("scripts:server", () => {
return gulp.watch("./src-server/**/*.js", gulp.series("scripts:server"))
}));
gulp.task("watch:scripts:client", () => {
const files = fs.readdirSync("./src-client");
for (let i = 0; i < files.length; i++) {
const file = files[i];
if(path.extname(file) !== ".js") continue;
initBundlerWatch(path.join("src-client", file));
}
return gulp.watch("./src-client/**/*.js")
.on("change", initBundlerWatch);
})
gulp.task("watch:scripts", gulp.parallel(
"watch:scripts:client",
"watch:scripts:server"));
let bundlers = {};
function initBundlerWatch(file){
if(bundlers.hasOwnProperty(file)) return;
const bundler = createBundler(file);
bundlers[file] = bundler;
const watcher = watchify(bundler);
const filename = path.basename(file);
function bundle(){
return bundler
.bundle()
.on("error", error => console.error(error))
.pipe(source(filename))
.pipe(gulp.dest("./public/build"));
}
watcher.on("update", bundle);
watcher.on("time", time => console.log(`B:C:${time}ms`));
bundle();
}
function createBundler(file) {
const bundler = browserify(file);
bundler.transform(babelify);
return bundler;
}