|
12 | 12 | import type {PackageJson} from 'metro-resolver/private/types'; |
13 | 13 |
|
14 | 14 | import {readFileSync} from 'fs'; |
15 | | -import {dirname} from 'path'; |
| 15 | +import {dirname, sep} from 'path'; |
16 | 16 |
|
17 | 17 | type GetClosestPackageFn = (absoluteFilePath: string) => ?{ |
18 | 18 | packageJsonPath: string, |
19 | 19 | packageRelativePath: string, |
20 | 20 | }; |
21 | 21 |
|
| 22 | +type PackageForModule = Readonly<{ |
| 23 | + packageJson: PackageJson, |
| 24 | + rootPath: string, |
| 25 | + packageRelativePath: string, |
| 26 | +}>; |
| 27 | + |
22 | 28 | export class PackageCache { |
23 | | - _getClosestPackage: GetClosestPackageFn; |
24 | | - _packageCache: { |
25 | | - [filePath: string]: { |
| 29 | + #getClosestPackage: GetClosestPackageFn; |
| 30 | + #packageCache: Map< |
| 31 | + string, |
| 32 | + { |
26 | 33 | rootPath: string, |
27 | 34 | packageJson: PackageJson, |
28 | 35 | }, |
29 | | - __proto__: null, |
30 | | - ... |
31 | | - }; |
32 | | - // Cache for "closest package.json" queries by module path. |
33 | | - _packagePathAndSubpathByModulePath: { |
34 | | - [filePath: string]: ?{ |
35 | | - packageJsonPath: string, |
36 | | - packageRelativePath: string, |
37 | | - }, |
38 | | - __proto__: null, |
39 | | - ... |
40 | | - }; |
41 | | - // The inverse of _packagePathByModulePath. |
42 | | - _modulePathsByPackagePath: { |
43 | | - [filePath: string]: Set<string>, |
44 | | - __proto__: null, |
45 | | - ... |
46 | | - }; |
| 36 | + >; |
| 37 | + // Single cache: module path → pre-built result object, or null (no allocation on hit) |
| 38 | + #resultByModulePath: Map<string, PackageForModule | null>; |
| 39 | + // Reverse index for invalidation: package.json path → set of module paths |
| 40 | + #modulePathsByPackagePath: Map<string, Set<string>>; |
| 41 | + // Module paths that resolved to no package.json (null), for invalidation |
| 42 | + #modulePathsWithNoPackage: Set<string>; |
47 | 43 |
|
48 | 44 | constructor(options: {getClosestPackage: GetClosestPackageFn, ...}) { |
49 | | - this._getClosestPackage = options.getClosestPackage; |
50 | | - this._packageCache = Object.create(null); |
51 | | - this._packagePathAndSubpathByModulePath = Object.create(null); |
52 | | - this._modulePathsByPackagePath = Object.create(null); |
| 45 | + this.#getClosestPackage = options.getClosestPackage; |
| 46 | + this.#packageCache = new Map(); |
| 47 | + this.#resultByModulePath = new Map(); |
| 48 | + this.#modulePathsByPackagePath = new Map(); |
| 49 | + this.#modulePathsWithNoPackage = new Set(); |
53 | 50 | } |
54 | 51 |
|
55 | | - getPackage(filePath: string): { |
| 52 | + getPackage(filePath: string): Readonly<{ |
56 | 53 | rootPath: string, |
57 | 54 | packageJson: PackageJson, |
58 | | - } { |
59 | | - if (!this._packageCache[filePath]) { |
60 | | - this._packageCache[filePath] = { |
| 55 | + }> { |
| 56 | + let cached = this.#packageCache.get(filePath); |
| 57 | + if (cached == null) { |
| 58 | + cached = { |
61 | 59 | rootPath: dirname(filePath), |
62 | 60 | packageJson: JSON.parse(readFileSync(filePath, 'utf8')), |
63 | 61 | }; |
| 62 | + this.#packageCache.set(filePath, cached); |
64 | 63 | } |
65 | | - return this._packageCache[filePath]; |
| 64 | + return cached; |
66 | 65 | } |
67 | 66 |
|
68 | | - getPackageForModule(absoluteModulePath: string): ?{ |
69 | | - packageJson: PackageJson, |
70 | | - rootPath: string, |
71 | | - packageRelativePath: string, |
72 | | - } { |
73 | | - let packagePathAndSubpath = |
74 | | - this._packagePathAndSubpathByModulePath[absoluteModulePath]; |
75 | | - if ( |
76 | | - packagePathAndSubpath && |
77 | | - this._packageCache[packagePathAndSubpath.packageJsonPath] |
78 | | - ) { |
79 | | - const {rootPath, packageJson} = |
80 | | - this._packageCache[packagePathAndSubpath.packageJsonPath]; |
81 | | - return { |
82 | | - packageJson, |
83 | | - rootPath, |
84 | | - packageRelativePath: packagePathAndSubpath.packageRelativePath, |
85 | | - }; |
| 67 | + getPackageForModule(absoluteModulePath: string): ?PackageForModule { |
| 68 | + const cached = this.#resultByModulePath.get(absoluteModulePath); |
| 69 | + |
| 70 | + // Distinguish between `null` (positively no closest package) and |
| 71 | + // `undefined` (no cached result yet) |
| 72 | + // eslint-disable-next-line lint/strictly-null |
| 73 | + if (cached !== undefined) { |
| 74 | + return cached; |
86 | 75 | } |
87 | 76 |
|
88 | | - packagePathAndSubpath = this._getClosestPackage(absoluteModulePath); |
89 | | - if (!packagePathAndSubpath) { |
| 77 | + const closest = this.#getClosestPackage(absoluteModulePath); |
| 78 | + if (closest == null) { |
| 79 | + this.#resultByModulePath.set(absoluteModulePath, null); |
| 80 | + this.#modulePathsWithNoPackage.add(absoluteModulePath); |
90 | 81 | return null; |
91 | 82 | } |
92 | 83 |
|
93 | | - const packagePath = packagePathAndSubpath.packageJsonPath; |
| 84 | + const packagePath = closest.packageJsonPath; |
94 | 85 |
|
95 | | - this._packagePathAndSubpathByModulePath[absoluteModulePath] = |
96 | | - packagePathAndSubpath; |
97 | | - const modulePaths = |
98 | | - this._modulePathsByPackagePath[packagePath] ?? new Set(); |
| 86 | + // Track module→package for invalidation |
| 87 | + let modulePaths = this.#modulePathsByPackagePath.get(packagePath); |
| 88 | + if (modulePaths == null) { |
| 89 | + modulePaths = new Set(); |
| 90 | + this.#modulePathsByPackagePath.set(packagePath, modulePaths); |
| 91 | + } |
99 | 92 | modulePaths.add(absoluteModulePath); |
100 | | - this._modulePathsByPackagePath[packagePath] = modulePaths; |
101 | 93 |
|
102 | 94 | const pkg = this.getPackage(packagePath); |
103 | | - |
104 | 95 | if (pkg == null) { |
105 | 96 | return null; |
106 | 97 | } |
107 | 98 |
|
108 | | - const {rootPath, packageJson} = pkg; |
109 | | - |
110 | | - return { |
111 | | - packageJson, |
112 | | - packageRelativePath: packagePathAndSubpath.packageRelativePath, |
113 | | - rootPath, |
| 99 | + // Cache the pre-built result object — no allocation on future hits |
| 100 | + const result: PackageForModule = { |
| 101 | + packageJson: pkg.packageJson, |
| 102 | + packageRelativePath: closest.packageRelativePath, |
| 103 | + rootPath: pkg.rootPath, |
114 | 104 | }; |
| 105 | + this.#resultByModulePath.set(absoluteModulePath, result); |
| 106 | + return result; |
115 | 107 | } |
116 | 108 |
|
117 | 109 | invalidate(filePath: string) { |
118 | | - if (this._packageCache[filePath]) { |
119 | | - delete this._packageCache[filePath]; |
120 | | - } |
121 | | - const packagePathAndSubpath = |
122 | | - this._packagePathAndSubpathByModulePath[filePath]; |
123 | | - if (packagePathAndSubpath) { |
124 | | - // filePath is a module inside a package. |
125 | | - const packagePath = packagePathAndSubpath.packageJsonPath; |
126 | | - delete this._packagePathAndSubpathByModulePath[filePath]; |
127 | | - // This change doesn't invalidate any cached "closest package.json" |
128 | | - // queries for the package's other modules. Clean up only this module. |
129 | | - const modulePaths = this._modulePathsByPackagePath[packagePath]; |
130 | | - if (modulePaths) { |
131 | | - modulePaths.delete(filePath); |
132 | | - if (modulePaths.size === 0) { |
133 | | - delete this._modulePathsByPackagePath[packagePath]; |
| 110 | + this.#packageCache.delete(filePath); |
| 111 | + |
| 112 | + // Clean up any cached result for this module path (including null). |
| 113 | + // Derive the package.json path from the cached result to clean up the |
| 114 | + // reverse index. |
| 115 | + const cachedResult = this.#resultByModulePath.get(filePath); |
| 116 | + this.#resultByModulePath.delete(filePath); |
| 117 | + this.#modulePathsWithNoPackage.delete(filePath); |
| 118 | + |
| 119 | + if (cachedResult != null) { |
| 120 | + const packagePath = cachedResult.rootPath + sep + 'package.json'; |
| 121 | + const modules = this.#modulePathsByPackagePath.get(packagePath); |
| 122 | + if (modules != null) { |
| 123 | + modules.delete(filePath); |
| 124 | + if (modules.size === 0) { |
| 125 | + this.#modulePathsByPackagePath.delete(packagePath); |
134 | 126 | } |
135 | 127 | } |
136 | 128 | } |
137 | | - if (this._modulePathsByPackagePath[filePath]) { |
138 | | - // filePath is a package. This change invalidates all cached "closest |
139 | | - // package.json" queries for modules inside this package. |
140 | | - const modulePaths = this._modulePathsByPackagePath[filePath]; |
| 129 | + |
| 130 | + // If filePath is a package.json, invalidate all module lookups pointing to it |
| 131 | + const modulePaths = this.#modulePathsByPackagePath.get(filePath); |
| 132 | + if (modulePaths != null) { |
141 | 133 | for (const modulePath of modulePaths) { |
142 | | - delete this._packagePathAndSubpathByModulePath[modulePath]; |
| 134 | + this.#resultByModulePath.delete(modulePath); |
| 135 | + } |
| 136 | + this.#modulePathsByPackagePath.delete(filePath); |
| 137 | + } |
| 138 | + |
| 139 | + // If a package.json was created, modified, or deleted, invalidate all |
| 140 | + // null-cached module results, since modules that previously had no |
| 141 | + // enclosing package.json may now resolve to this one. |
| 142 | + if (filePath.endsWith(sep + 'package.json')) { |
| 143 | + for (const modulePath of this.#modulePathsWithNoPackage) { |
| 144 | + this.#resultByModulePath.delete(modulePath); |
143 | 145 | } |
144 | | - modulePaths.clear(); |
145 | | - delete this._modulePathsByPackagePath[filePath]; |
| 146 | + this.#modulePathsWithNoPackage.clear(); |
146 | 147 | } |
147 | 148 | } |
148 | 149 | } |
0 commit comments