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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86'use strict';
var path = require('path');
var fs = require('fs');
var glob_parent = require('glob-parent');
var es = require('event-stream');
var vinyl = require('vinyl-file');
var pug_dependency = require('pug-dependency');
module.exports = function (globs, options) {
options = options || {};
var delay = options.delay || 100;
var base = path.resolve(glob_parent(globs));
var dependency = pug_dependency(globs, options);
var stream;
var files = {};
var paths = {};
var timer = null;
function flush() {
var abs_path;
timer = null;
for (abs_path in paths) {
if (!files[abs_path]) {
files[abs_path] = vinyl.readSync(abs_path, {base: base});
}
}
paths = {};
for (abs_path in files) {
stream.emit('data', files[abs_path]);
}
files = {};
}
function request_flush() {
if (!timer) {
timer = setTimeout(flush, delay);
}
}
function add_file(abs_path, file) {
files[abs_path] = file;
request_flush();
}
function add_path(abs_path) {
paths[abs_path] = true;
request_flush();
}
var mtime_cache = {};
stream = es.through(function (file) {
var abs_path = path.resolve(file.path);
try {
// Prevent unchanged files from triggering
var mtime = fs.statSync(abs_path).mtime.getTime();
var last_mtime = mtime_cache[abs_path];
mtime_cache[abs_path] = mtime;
if (last_mtime === mtime) {
return;
}
if (!last_mtime) {
// Initial scan, skip dependency graph
add_file(abs_path, file);
return;
}
} catch (e) {
// File might be gone.
}
add_file(abs_path, file);
dependency.file_changed(abs_path);
dependency.find_dependents(abs_path).map(add_path);
});
return stream;
};