Skip to content

Commit 71f7d29

Browse files
authored
Add support for glob matching when watching dir-dependency (#391)
This extends #383 to pass glob information on to chokidar, with the intention of reducing outputs when unrelated files are modified
1 parent 4be419d commit 71f7d29

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ function dependencies(results) {
281281
.map(depGraph.add)
282282
.forEach((dependency) => {
283283
if (dependency.type === 'dir-dependency') {
284-
messages.push(dependency.dir)
284+
messages.push(
285+
dependency.glob
286+
? path.join(dependency.dir, dependency.glob)
287+
: dependency.dir
288+
)
285289
} else {
286290
messages.push(dependency.file)
287291
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Editing this file should not trigger a rebuild.

test/watch.js

+117
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,120 @@ testCb('--watch watches directory dependencies', (t) => {
494494
// Timeout:
495495
setTimeout(() => t.end('test timeout'), 50000)
496496
})
497+
498+
testCb(
499+
'--watch applies glob on dir-dependency (and excludes non matching files)',
500+
(t) => {
501+
let cp
502+
let modifying = null // one of "unrelated.md", "a.css"
503+
504+
t.plan(1)
505+
506+
ENV('', [
507+
's.css',
508+
'base/level-1/b.css',
509+
'base/level-1/level-2/a.css',
510+
'base/level-1/level-2/unrelated.md',
511+
]).then((dir) => {
512+
fs.writeFile(
513+
path.join(dir, 'postcss.config.js'),
514+
`
515+
const fs = require('fs')
516+
module.exports = {
517+
plugins: [
518+
(root, result) => {
519+
result.messages.push({
520+
plugin: 'test',
521+
type: 'dir-dependency',
522+
dir: '${path.resolve(dir, 'base')}',
523+
glob: '**/*.css',
524+
parent: result.opts.from,
525+
})
526+
root.nodes = []
527+
root.append(fs.readFileSync('${path.resolve(
528+
dir,
529+
'base/level-1/level-2/a.css'
530+
)}', 'utf8'))
531+
return root
532+
}
533+
]
534+
}
535+
`
536+
)
537+
.then(() => {
538+
// Init watcher:
539+
const watcher = chokidar.watch('.', {
540+
cwd: dir,
541+
ignoreInitial: true,
542+
awaitWriteFinish: true,
543+
})
544+
545+
// On the first output:
546+
watcher.on('add', (p) => {
547+
if (p === 'output.css') {
548+
// Modify unwatched file, shouldn't trigger output
549+
modifyUnwatched()
550+
}
551+
})
552+
553+
// When the change is picked up:
554+
watcher.on('change', (p) => {
555+
if (p === 'output.css') {
556+
// Assert that change to output.css happened only after modifying the watched a.css
557+
t.is(
558+
modifying,
559+
'a.css',
560+
`Unexpected change to ${p} after modifying ${modifying}`
561+
)
562+
done()
563+
} else if (p === 'base/level-1/level-2/unrelated.md') {
564+
// Modify watched file next, should trigger output
565+
setTimeout(modifyWatched, 250)
566+
}
567+
})
568+
569+
// Start postcss-cli:
570+
watcher.on('ready', () => {
571+
cp = exec(
572+
`node ${path.resolve(
573+
'bin/postcss'
574+
)} "s.css" -o output.css --no-map -w`,
575+
{ cwd: dir }
576+
)
577+
cp.on('error', t.end)
578+
cp.on('exit', (code) => {
579+
if (code) t.end(code)
580+
})
581+
})
582+
583+
function modifyUnwatched() {
584+
modifying = 'unrelated.md'
585+
fs.writeFile(
586+
path.join(dir, 'base/level-1/level-2/unrelated.md'),
587+
'Some modification'
588+
).catch(done)
589+
}
590+
591+
function modifyWatched() {
592+
modifying = 'a.css'
593+
fs.writeFile(
594+
path.join(dir, 'base/level-1/level-2/a.css'),
595+
'a { color: hotpink }'
596+
).catch(done)
597+
}
598+
599+
function done(err) {
600+
try {
601+
cp.kill()
602+
} catch {}
603+
604+
t.end(err)
605+
}
606+
})
607+
.catch(t.end)
608+
})
609+
610+
// Timeout:
611+
setTimeout(() => t.end('test timeout'), 50000)
612+
}
613+
)

0 commit comments

Comments
 (0)