Skip to content

Commit 22a257e

Browse files
committed
fix(watch): support array of source files
closes #663
1 parent ac235bd commit 22a257e

File tree

2 files changed

+85
-43
lines changed

2 files changed

+85
-43
lines changed

lib/resources/tasks/watch.js

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,44 @@ import copyFiles from './copy-files';
1212
const debounceWaitTime = 100;
1313
let isBuilding = false;
1414
let pendingRefreshPaths = [];
15-
let watches = {};
1615
let watchCallback = () => { };
16+
let watches = [
17+
{ name: 'transpile', callback: transpile, source: project.transpiler.source },
18+
{ name: 'markup', callback: processMarkup, source: project.markupProcessor.source },
19+
{ name: 'CSS', callback: processCSS, source: project.cssProcessor.source }
20+
];
1721

18-
watches[project.transpiler.source] = { name: 'transpile', callback: transpile };
19-
watches[project.markupProcessor.source] = { name: 'markup', callback: processMarkup };
20-
watches[project.cssProcessor.source] = { name: 'CSS', callback: processCSS };
2122
if (typeof project.build.copyFiles === 'object') {
2223
for (let src of Object.keys(project.build.copyFiles)) {
23-
watches[src] = { name: 'file copy', callback: copyFiles };
24+
watches.push({ name: 'file copy', callback: copyFiles, source: src });
2425
}
2526
}
2627

2728
let watch = (callback) => {
2829
watchCallback = callback || watchCallback;
2930

30-
const watchPaths = Object.keys(watches);
31-
32-
for(let i = 0; i < watchPaths.length; i++) {
33-
gulpWatch(
34-
watchPaths[i],
35-
{
36-
read: false, // performance optimization: do not read actual file contents
37-
verbose: true
38-
},
39-
(vinyl) => processChange(vinyl));
31+
// watch every glob individually
32+
for(let watcher of watches) {
33+
if (Array.isArray(watcher.source)) {
34+
for(let glob of watcher.source) {
35+
watchPath(glob);
36+
}
37+
} else {
38+
watchPath(watcher.source);
39+
}
4040
}
4141
};
4242

43+
let watchPath = (p) => {
44+
gulpWatch(
45+
p,
46+
{
47+
read: false, // performance optimization: do not read actual file contents
48+
verbose: true
49+
},
50+
(vinyl) => processChange(vinyl));
51+
};
52+
4353
let processChange = (vinyl) => {
4454
if (vinyl.path && vinyl.cwd && vinyl.path.startsWith(vinyl.cwd)) {
4555
let pathToAdd = vinyl.path.substr(vinyl.cwd.length + 1);
@@ -60,11 +70,20 @@ let refresh = debounce(() => {
6070
let paths = pendingRefreshPaths.splice(0);
6171
let refreshTasks = [];
6272

63-
// Dynamically compose tasks
64-
for (let src of Object.keys(watches)) {
65-
if (paths.find((x) => minimatch(x, src))) {
66-
log(`Watcher: Adding ${watches[src].name} task to next build...`);
67-
refreshTasks.push(watches[src].callback);
73+
// determine which tasks need to be executed
74+
// based on the files that have changed
75+
for (let watcher of watches) {
76+
if (Array.isArray(watcher.source)) {
77+
for(let source of watcher.source) {
78+
if (paths.find(path => minimatch(path, source))) {
79+
refreshTasks.push(watcher);
80+
}
81+
}
82+
}
83+
else {
84+
if (paths.find(path => minimatch(path, watcher.source))) {
85+
refreshTasks.push(watcher);
86+
}
6887
}
6988
}
7089

@@ -74,9 +93,11 @@ let refresh = debounce(() => {
7493
return;
7594
}
7695

96+
log(`Watcher: Running ${refreshTasks.map(x => x.name).join(', ')} tasks on next build...`);
97+
7798
let toExecute = gulp.series(
7899
readProjectConfiguration,
79-
gulp.parallel(refreshTasks),
100+
gulp.parallel(refreshTasks.map(x => x.callback)),
80101
writeBundles,
81102
(done) => {
82103
isBuilding = false;

lib/resources/tasks/watch.ts

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,44 @@ import copyFiles from './copy-files';
1212
const debounceWaitTime = 100;
1313
let isBuilding = false;
1414
let pendingRefreshPaths = [];
15-
let watches = {};
1615
let watchCallback = () => { };
16+
let watches = [
17+
{ name: 'transpile', callback: transpile, source: project.transpiler.source },
18+
{ name: 'markup', callback: processMarkup, source: project.markupProcessor.source },
19+
{ name: 'CSS', callback: processCSS, source: project.cssProcessor.source }
20+
];
1721

18-
watches[project.transpiler.source] = { name: 'transpile', callback: transpile };
19-
watches[project.markupProcessor.source] = { name: 'markup', callback: processMarkup };
20-
watches[project.cssProcessor.source] = { name: 'CSS', callback: processCSS };
2122
if (typeof project.build.copyFiles === 'object') {
2223
for (let src of Object.keys(project.build.copyFiles)) {
23-
watches[src] = { name: 'file copy', callback: copyFiles };
24+
watches.push({ name: 'file copy', callback: copyFiles, source: src });
2425
}
2526
}
2627

27-
let watch = (callback?) => {
28+
let watch = (callback) => {
2829
watchCallback = callback || watchCallback;
2930

30-
const watchPaths = Object.keys(watches);
31-
32-
for(let i = 0; i < watchPaths.length; i++) {
33-
gulpWatch(
34-
watchPaths[i],
35-
{
36-
read: false, // performance optimization: do not read actual file contents
37-
verbose: true
38-
},
39-
(vinyl) => processChange(vinyl));
31+
// watch every glob individually
32+
for(let watcher of watches) {
33+
if (Array.isArray(watcher.source)) {
34+
for(let glob of watcher.source) {
35+
watchPath(glob);
36+
}
37+
} else {
38+
watchPath(watcher.source);
39+
}
4040
}
4141
};
4242

43+
let watchPath = (p) => {
44+
gulpWatch(
45+
p,
46+
{
47+
read: false, // performance optimization: do not read actual file contents
48+
verbose: true
49+
},
50+
(vinyl) => processChange(vinyl));
51+
};
52+
4353
let processChange = (vinyl) => {
4454
if (vinyl.path && vinyl.cwd && vinyl.path.startsWith(vinyl.cwd)) {
4555
let pathToAdd = vinyl.path.substr(vinyl.cwd.length + 1);
@@ -60,11 +70,20 @@ let refresh = debounce(() => {
6070
let paths = pendingRefreshPaths.splice(0);
6171
let refreshTasks = [];
6272

63-
// Dynamically compose tasks
64-
for (let src of Object.keys(watches)) {
65-
if (paths.find((x) => minimatch(x, src))) {
66-
log(`Watcher: Adding ${watches[src].name} task to next build...`);
67-
refreshTasks.push(watches[src].callback);
73+
// determine which tasks need to be executed
74+
// based on the files that have changed
75+
for (let watcher of watches) {
76+
if (Array.isArray(watcher.source)) {
77+
for(let source of watcher.source) {
78+
if (paths.find(path => minimatch(path, source))) {
79+
refreshTasks.push(watcher);
80+
}
81+
}
82+
}
83+
else {
84+
if (paths.find(path => minimatch(path, watcher.source))) {
85+
refreshTasks.push(watcher);
86+
}
6887
}
6988
}
7089

@@ -74,9 +93,11 @@ let refresh = debounce(() => {
7493
return;
7594
}
7695

96+
log(`Watcher: Running ${refreshTasks.map(x => x.name).join(', ')} tasks on next build...`);
97+
7798
let toExecute = gulp.series(
7899
readProjectConfiguration,
79-
gulp.parallel(refreshTasks),
100+
gulp.parallel(refreshTasks.map(x => x.callback)),
80101
writeBundles,
81102
(done) => {
82103
isBuilding = false;

0 commit comments

Comments
 (0)