-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev-watch.mjs
51 lines (43 loc) · 1.65 KB
/
dev-watch.mjs
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
import { subscribe } from '@parcel/watcher';
(() => {
let subscription;
/**
* initialize @parcel/watcher watch & init browserSync
* We are watching the files with extensions (.js, .ts, .html, .css, .scss) but we'll ignore folders like node_modules, dist & .git
* Note: the watcher often send duplicate events, however the use of Set of file changes & the use of a setTimeout delay gets rid of this problem.
*/
async function init() {
subscription = subscribe(process.cwd(), (err, events) => {
if (err) return onError(err);
console.log('Watcher events::', events);
// for (const event of events) {
// onFileChanged(event.path);
// }
}, {
ignore: [
'**/.git/**',
'**/dist/**',
'**/node_modules/**',
'!**/*.{ts,js,html,css,scss}' // which file extensions to watch
]
});
// also watch for any Signal termination to cleanly exit the watch command
process.once('SIGINT', () => destroy());
process.once('SIGTERM', () => destroy());
process.stdin.on('end', () => destroy());
process.stdin.on('exit', () => process.stdin.destroy());
}
/** Log to the terminal any watch errors */
function onError(err) {
console.error('File watcher error', err);
}
function onFileChanged(filepath) {
console.log('File changed', filepath);
}
async function destroy() {
console.log('Exiting the dev file watch...');
(await subscription).unsubscribe();
}
// start dev watch process
init();
})();