Skip to content

Commit c3f8c2d

Browse files
committed
fix(cli): correct watch ignore logic
1 parent 690dec0 commit c3f8c2d

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

packages/cli/src/app.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ await yargs(hideBin(process.argv))
116116
const closeHandlers: (() => Promise<void>)[] = [];
117117
const resolvedDir = path.resolve(args.dir);
118118

119-
await bundleSource(resolvedDir, logger);
120-
121119
const handleClose = async (): Promise<void> => {
122120
await Promise.all(
123121
closeHandlers.map(async (close) => withTimeout(close(), 400)),

packages/cli/src/commands/watch.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { unlink } from 'fs/promises';
88
import { describe, it, expect, vi, beforeEach } from 'vitest';
99

1010
import { bundleFile } from './bundle.ts';
11-
import { watchDir, makeWatchEvents, filterOnlyJsFiles } from './watch.ts';
11+
import { watchDir, makeWatchEvents, shouldIgnore } from './watch.ts';
1212

1313
vi.mock('fs/promises', () => ({
1414
unlink: vi.fn(async () => new Promise<void>(() => undefined)),
@@ -46,21 +46,22 @@ describe('watchDir', () => {
4646
});
4747
});
4848

49-
describe('filterOnlyJsFiles', () => {
49+
describe('shouldIgnore', () => {
5050
const missingStats = undefined as unknown as Stats;
5151
const fileStats = { isFile: () => true } as unknown as Stats;
5252
const nonFileStats = { isFile: () => false } as unknown as Stats;
5353

5454
it.each`
55-
description | file | stats | expectation
56-
${'a file with missing stats'} | ${'test.js'} | ${missingStats} | ${false}
57-
${'a non-file'} | ${'test.js'} | ${nonFileStats} | ${false}
58-
${'a .ts file'} | ${'test.ts'} | ${fileStats} | ${false}
59-
${'a .bundle file'} | ${'test.bundle'} | ${fileStats} | ${false}
60-
${'a .txt file'} | ${'test.txt'} | ${nonFileStats} | ${false}
61-
${'a .js file'} | ${'test.js'} | ${fileStats} | ${true}
55+
description | file | stats | expectation
56+
${'a file with missing stats'} | ${'test.js'} | ${missingStats} | ${false}
57+
${'a non-file'} | ${'test.js'} | ${nonFileStats} | ${false}
58+
${'a .js file'} | ${'test.js'} | ${fileStats} | ${false}
59+
${'a .ts file'} | ${'test.ts'} | ${fileStats} | ${true}
60+
${'a .bundle file'} | ${'test.bundle'} | ${fileStats} | ${true}
61+
${'a .txt file'} | ${'test.txt'} | ${fileStats} | ${true}
62+
${'a .js file in node_modules'} | ${'node_modules/test.js'} | ${fileStats} | ${true}
6263
`('returns $expectation for $description', ({ file, expectation, stats }) => {
63-
expect(filterOnlyJsFiles(file, stats)).toBe(expectation);
64+
expect(shouldIgnore(file, stats)).toBe(expectation);
6465
});
6566
});
6667

packages/cli/src/commands/watch.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ export const makeWatchEvents = (
5656
error: (error: Error) => throwError(error),
5757
});
5858

59-
export const filterOnlyJsFiles: MatchFunction = (file, stats) =>
60-
(stats?.isFile() ?? false) && file.endsWith('.js');
59+
export const shouldIgnore: MatchFunction = (file, stats): boolean =>
60+
// Ignore files and directories in `node_modules`.
61+
file.includes('node_modules') ||
62+
// Watch non-files, but ignore files that are not JavaScript.
63+
((stats?.isFile() ?? false) && !file.endsWith('.js'));
6164

6265
/**
6366
* Start a watcher that bundles `.js` files in the target dir.
@@ -77,14 +80,8 @@ export function watchDir(dir: string, logger: Logger): WatchDirReturn {
7780
const { reject: throwError, promise: errorPromise } = makePromiseKit<never>();
7881

7982
let watcher = watch(resolvedDir, {
80-
ignoreInitial: true,
81-
ignored: [
82-
'**/node_modules/**',
83-
'**/*.test.js',
84-
'**/*.test.ts',
85-
'**/*.bundle',
86-
filterOnlyJsFiles,
87-
],
83+
ignoreInitial: false,
84+
ignored: shouldIgnore,
8885
});
8986

9087
const events = makeWatchEvents(watcher, readyResolve, throwError, logger);

0 commit comments

Comments
 (0)