Skip to content

Commit 36f1bbe

Browse files
committed
Expand dir-dependency globs manually
1 parent bf5c172 commit 36f1bbe

1 file changed

Lines changed: 42 additions & 26 deletions

File tree

index.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ import outputFile from './lib/outputFile.js'
2121
const reporter = postcssReporter()
2222
const depGraph = createDependencyGraph()
2323

24+
const expandGlob = async (patterns) => {
25+
if (!Array.isArray(patterns)) patterns = [patterns]
26+
27+
const paths = await glob(
28+
patterns.map((i) => slash(String(i))),
29+
{ dot: argv.includeDotfiles },
30+
)
31+
32+
return paths.map((i) => path.resolve(i))
33+
}
34+
2435
let input = argv._
2536
const { dir, output } = argv
2637

@@ -79,19 +90,15 @@ if (argv.watch && !(argv.output || argv.replace || argv.dir)) {
7990
}
8091

8192
if (input && input.length) {
82-
input = await glob(
83-
input.map((i) => slash(String(i))),
84-
{ dot: argv.includeDotfiles },
85-
)
93+
input = await expandGlob(input)
94+
8695
if (!input.length) {
8796
error('Input Error: You must pass a valid list of files to parse')
8897
}
8998

9099
if (input.length > 1 && !argv.dir && !argv.replace) {
91100
error('Input Error: Must use --dir or --replace with multiple input files')
92101
}
93-
94-
input = input.map((i) => path.resolve(i))
95102
} else {
96103
if (argv.replace || argv.dir) {
97104
error('Input Error: Cannot use --dir or --replace when reading from stdin')
@@ -110,7 +117,8 @@ try {
110117
if (argv.watch) {
111118
const printMessage = () =>
112119
printVerbose(pc.dim('\nWaiting for file changes...'))
113-
const watcher = chokidar.watch(input.concat(dependencies(results)), {
120+
const deps = await dependencies(results)
121+
const watcher = chokidar.watch(input.concat(deps), {
114122
usePolling: argv.poll,
115123
interval: argv.poll && typeof argv.poll === 'number' ? argv.poll : 100,
116124
awaitWriteFinish: {
@@ -137,7 +145,8 @@ try {
137145
if (!recompile.length) recompile = input
138146

139147
return files([...new Set(recompile)])
140-
.then((results) => watcher.add(dependencies(results)))
148+
.then((results) => dependencies(results))
149+
.then((deps) => watcher.add(deps))
141150
.then(printMessage)
142151
.catch((err) => {
143152
// Watch mode shouldn't exit on file processing error
@@ -260,27 +269,34 @@ async function css(css, file) {
260269
return result
261270
}
262271

263-
function dependencies(results) {
272+
async function dependencies(results) {
264273
if (!Array.isArray(results)) results = [results]
265274

266-
return results.flatMap((result) => {
267-
if (result.messages.length <= 0) return []
268-
269-
return result.messages
270-
.filter(
271-
(msg) => msg.type === 'dependency' || msg.type === 'dir-dependency',
275+
const depArrays = await Promise.all(
276+
results.map(async (result) => {
277+
if (result.messages.length <= 0) return []
278+
279+
return Promise.all(
280+
result.messages
281+
.filter(
282+
(msg) => msg.type === 'dependency' || msg.type === 'dir-dependency',
283+
)
284+
.map(depGraph.add)
285+
.map(async (dependency) => {
286+
if (dependency.type === 'dir-dependency') {
287+
return dependency.glob
288+
? await expandGlob(path.join(dependency.dir, dependency.glob))
289+
: dependency.dir
290+
}
291+
292+
return dependency.file
293+
}),
272294
)
273-
.map(depGraph.add)
274-
.map((dependency) => {
275-
if (dependency.type === 'dir-dependency') {
276-
return dependency.glob
277-
? path.join(dependency.dir, dependency.glob)
278-
: dependency.dir
279-
}
280-
281-
return dependency.file
282-
})
283-
})
295+
}),
296+
)
297+
298+
// depth of 2 is needed because glob dir-dependency can return an array of files
299+
return depArrays.flat(2)
284300
}
285301

286302
function printVerbose(message) {

0 commit comments

Comments
 (0)