UNPKG

1.07 kBJavaScriptView Raw
1const path = require("path");
2const gulp = require("gulp");
3const chalk = require("chalk");
4const log = require("./logger");
5const compile = require("./compile");
6const fs = require("fs-extra");
7const blue = chalk.keyword("green");
8
9module.exports = function (options) {
10 const filter = options.filter;
11
12 const watcher = gulp.watch(options.glob, {
13 cwd: options.appSrc,
14 allowEmpty: true,
15 delay: 300,
16 });
17
18 watcher.on("change", async function (file, stats) {
19 if (filter && !filter(file)) {
20 return;
21 }
22
23 await compile(file, options);
24 log("文件修改:%s", blue(file));
25 log.flush();
26 });
27
28 watcher.on("add", async function (file, stats) {
29 if (filter && !filter(file)) {
30 return;
31 }
32
33 await compile(file, options);
34 log("文件新增:%s", blue(file));
35 log.flush();
36 });
37
38 watcher.on("unlink", async function (file, stats) {
39 if (filter && !filter(file)) {
40 return;
41 }
42
43 fs.removeSync(path.resolve(options.appSrc, file));
44 log("文件删除:%s", blue(file));
45 log.flush();
46 });
47
48 watcher.on("error", async function () {
49 // ignore
50 });
51};