Handle package json watch life time (#56488)

This commit is contained in:
Sheetal Nandi
2023-11-27 14:50:22 -08:00
committed by GitHub
parent 6e65f973f6
commit 1797837351
26 changed files with 298 additions and 163 deletions

View File

@@ -1,4 +1,5 @@
import {
closeFileWatcher,
Debug,
FileWatcher,
ModulePath,
@@ -13,11 +14,12 @@ import {
/** @internal */
export interface ModuleSpecifierResolutionCacheHost {
watchNodeModulesForPackageJsonChanges(directoryPath: string): FileWatcher;
toPath(fileName: string): Path;
}
/** @internal */
export function createModuleSpecifierCache(host: ModuleSpecifierResolutionCacheHost): ModuleSpecifierCache {
let containedNodeModulesWatchers: Map<string, FileWatcher> | undefined;
let containedNodeModulesWatchers: Map<Path, FileWatcher> | undefined;
let cache: Map<Path, ResolvedModuleSpecifierInfo> | undefined;
let currentKey: string | undefined;
const result: ModuleSpecifierCache = {
@@ -38,9 +40,10 @@ export function createModuleSpecifierCache(host: ModuleSpecifierResolutionCacheH
if (p.isInNodeModules) {
// No trailing slash
const nodeModulesPath = p.path.substring(0, p.path.indexOf(nodeModulesPathPart) + nodeModulesPathPart.length - 1);
if (!containedNodeModulesWatchers?.has(nodeModulesPath)) {
const key = host.toPath(nodeModulesPath);
if (!containedNodeModulesWatchers?.has(key)) {
(containedNodeModulesWatchers ||= new Map()).set(
nodeModulesPath,
key,
host.watchNodeModulesForPackageJsonChanges(nodeModulesPath),
);
}
@@ -69,7 +72,7 @@ export function createModuleSpecifierCache(host: ModuleSpecifierResolutionCacheH
}
},
clear() {
containedNodeModulesWatchers?.forEach(watcher => watcher.close());
containedNodeModulesWatchers?.forEach(closeFileWatcher);
cache?.clear();
containedNodeModulesWatchers?.clear();
currentKey = undefined;