Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular-devkit/build-angular): use Webpack provided watcher typings instead of custom #21292

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ export interface BuilderWatcherFactory {
): { close(): void };
}

export interface WebpackWatcher {
close(): void;
pause(): void;
getFileTimeInfoEntries(): Map<string, { safeTime: number; timestamp: number }>;
getContextTimeInfoEntries(): Map<string, { safeTime: number; timestamp: number }>;
}

class TimeInfoMap extends Map<string, { safeTime: number; timestamp: number }> {
update(path: string, timestamp: number): void {
this.set(path, Object.freeze({ safeTime: timestamp, timestamp }));
Expand All @@ -42,41 +35,26 @@ class TimeInfoMap extends Map<string, { safeTime: number; timestamp: number }> {
}
}

type WatchCallback = (
error: Error | undefined,
files: Map<string, { safeTime: number; timestamp: number }>,
contexts: Map<string, { safeTime: number; timestamp: number }>,
changes: Set<string>,
removals: Set<string>,
) => void;

export interface WebpackWatchFileSystem {
watch(
files: Iterable<string>,
directories: Iterable<string>,
missing: Iterable<string>,
startTime: number,
options: {},
callback: WatchCallback,
callbackUndelayed: (file: string, time: number) => void,
): WebpackWatcher;
}
// Extract watch related types from the Webpack compiler type since they are not directly exported
type WebpackWatchFileSystem = Compiler['watchFileSystem'];
type WatchOptions = Parameters<WebpackWatchFileSystem['watch']>[4];
type WatchCallback = Parameters<WebpackWatchFileSystem['watch']>[5];

class BuilderWatchFileSystem implements WebpackWatchFileSystem {
constructor(
private readonly watcherFactory: BuilderWatcherFactory,
private readonly inputFileSystem: { purge?(path?: string): void },
private readonly inputFileSystem: Compiler['inputFileSystem'],
) {}

watch(
files: Iterable<string>,
directories: Iterable<string>,
missing: Iterable<string>,
startTime: number,
_options: {},
_options: WatchOptions,
callback: WatchCallback,
callbackUndelayed?: (file: string, time: number) => void,
): WebpackWatcher {
): ReturnType<WebpackWatchFileSystem['watch']> {
const watchedFiles = new Set(files);
const watchedDirectories = new Set(directories);
const watchedMissing = new Set(missing);
Expand Down Expand Up @@ -152,7 +130,7 @@ class BuilderWatchFileSystem implements WebpackWatchFileSystem {
export class BuilderWatchPlugin {
constructor(private readonly watcherFactory: BuilderWatcherFactory) {}

apply(compiler: Compiler & { watchFileSystem: unknown }): void {
apply(compiler: Compiler): void {
compiler.hooks.environment.tap('BuilderWatchPlugin', () => {
compiler.watchFileSystem = new BuilderWatchFileSystem(
this.watcherFactory,
Expand Down