Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ export async function* runEsBuildBuildAction(
logger.info('Watch mode enabled. Watching for file changes...');
}

const normalizedOutputBase = toPosixPath(outputOptions.base);
const normalizedCacheBase = toPosixPath(cacheOptions.basePath);
const ignored: string[] = [
// Ignore the output and cache paths to avoid infinite rebuild cycles
outputOptions.base,
cacheOptions.basePath,
normalizedOutputBase,
`${normalizedOutputBase}/**`,
normalizedCacheBase,
`${normalizedCacheBase}/**`,
`${toPosixPath(workspaceRoot)}/**/.*/**`,
];

Expand Down
13 changes: 12 additions & 1 deletion packages/angular/build/src/tools/esbuild/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type * as ParcelWatcher from '@parcel/watcher';
import type * as Chokidar from 'chokidar';
import * as fs from 'node:fs';
import * as path from 'node:path';
import picomatch from 'picomatch';
import { toPosixPath } from '../../utils/path';

export class ChangedFiles {
Expand Down Expand Up @@ -517,9 +518,19 @@ async function createChokidarWatcher(
const rootDirPosix = toPosixPathNormalized(rootDir);
const rootDirLookupKey = toLookupKey(rootDirPosix, isCaseSensitive);

const ignored = options?.ignored?.map((pattern) => {
if (/[*?[\]{}()]/.test(pattern)) {
const isMatch = picomatch(pattern, { dot: true });

return (filePath: string) => isMatch(toPosixPathNormalized(filePath));
}

return { path: toPosixPathNormalized(pattern), recursive: true };
});
Comment thread
clydin marked this conversation as resolved.

const watcher = chokidar.watch(rootDir, {
ignoreInitial: true,
ignored: options?.ignored,
ignored,
followSymlinks: options?.followSymlinks,
usePolling: !!options?.polling,
interval: options?.interval,
Expand Down
34 changes: 34 additions & 0 deletions packages/angular/build/src/tools/esbuild/watcher_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,5 +448,39 @@ describe('Watcher', () => {

await watcher.close();
}, 10000);

it('should ignore changes matching glob patterns in polling mode (chokidar)', async () => {
const ignoredDir = path.join(tempDir, 'dist');
fs.mkdirSync(ignoredDir);
const ignoredFile = path.join(ignoredDir, 'bundle.js');
const watchedFile = path.join(tempDir, 'src.ts');
fs.writeFileSync(ignoredFile, 'initial-dist');
fs.writeFileSync(watchedFile, 'initial-src');

const watcher = await createWatcher({
polling: true,
interval: 50,
cwd: tempDir,
ignored: [`${toPosixPathNormalized(ignoredDir)}/**`],
});

watcher.add(tempDir);
await setTimeout(100);

const iterator = watcher[Symbol.asyncIterator]();
const nextPromise = iterator.next();

// Trigger changes in ignored file and watched file
fs.writeFileSync(ignoredFile, 'updated-dist');
fs.writeFileSync(watchedFile, 'updated-src');

const result = await nextPromise;
expect(result.done).toBeFalsy();
const emitted = result.value?.all ?? [];
expect(emitted.some((f: string) => f.includes('src.ts'))).toBeTrue();
expect(emitted.some((f: string) => f.includes('bundle.js'))).toBeFalse();

await watcher.close();
}, 10000);
});
});