Skip to content

Commit 4a15691

Browse files
authored
Fix watching relative dirs in @swc/cli due to change in chokidar@v4 (#82)
When watching on `--out-dir`, `@swc/cli` would compile the sources when a relative path was passed, but watching would immediately exit instead of watch for changes. Chokidar version 4.x [removes globbing support](https://github.com/paulmillr/chokidar/releases/tag/4.0.0), which means that it does not recursively watch unresolved relative paths with either globbing or cwd. The documentation recommends resolving the glob(s) before passing to `watch`, and has some examples. Weighing the options, it appeared to be more ergonomic to use `globSources` to resolve the files to watch rather than create a new method of globbing or directly glob the files. This pull request makes a very small change to the parameters of `watchSources`, as I could not cause the error to occur on individual files and wanted to leave that signature alone. However, when watching on `--out-dir`, watching would immediately exit as a relative file did not exist and was not being resolved via globbing anymore. This restores the ability to pass relative paths and globs to the `swc` command of `@swc/cli` and successfully watch for changes of the source files.
1 parent d2bf625 commit 4a15691

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

.changeset/twelve-fishes-roll.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@swc/cli": patch
3+
---
4+
5+
Fix watching relative dirs in `@swc/cli` due to change in `chokidar@v4`

packages/cli/src/swc/dir.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,16 @@ async function watchCompilation(
256256
outFileExtension,
257257
quiet,
258258
sync,
259+
only,
260+
ignore,
259261
} = cliOptions;
260262

261-
const watcher = await watchSources(filenames, includeDotfiles);
263+
const watcher = await watchSources(
264+
filenames,
265+
includeDotfiles,
266+
only,
267+
ignore
268+
);
262269
watcher.on("ready", () => {
263270
if (callbacks?.onWatchReady) {
264271
callbacks.onWatchReady();

packages/cli/src/swc/sources.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,20 @@ export async function requireChokidar() {
106106
}
107107
}
108108

109-
export async function watchSources(sources: string[], includeDotfiles = false) {
109+
export async function watchSources(
110+
sources: string[],
111+
includeDotfiles = false,
112+
only: string[] = [],
113+
ignore: string[] = [],
114+
) {
110115
const chokidar = await requireChokidar();
111-
112-
return chokidar.watch(sources, {
116+
const sourceFiles = await globSources(
117+
filenames,
118+
only,
119+
ignore,
120+
includeDotfiles
121+
);
122+
return chokidar.watch(sourceFiles, {
113123
ignored: includeDotfiles
114124
? undefined
115125
: (filename: string) => basename(filename).startsWith("."),

0 commit comments

Comments
 (0)