mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-23 18:48:40 -05:00
Handle package json watch life time (#56488)
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user