Skip to content

Commit 4ffec21

Browse files
committed
chore: address review feedback (index exports cache)
1 parent 3bd9d97 commit 4ffec21

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

packages/workspace/src/generators/move-file/export-management/index-exports-cache.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Tree } from '@nx/devkit';
2-
import { astCache } from '../ast-cache';
32
import { treeReadCache } from '../tree-cache';
43

54
/**
@@ -10,8 +9,12 @@ export interface IndexExports {
109
reexports: Set<string>; // re-exported modules (file paths without extension)
1110
}
1211

12+
interface CachedIndexExports extends IndexExports {
13+
content: string; // content snapshot used to build this cache entry
14+
}
15+
1316
// Internal cache keyed by normalized index file path
14-
const indexExportsCache = new Map<string, IndexExports>();
17+
const indexExportsCache = new Map<string, CachedIndexExports>();
1518

1619
/** Clears all cached index export data. */
1720
export function clearIndexExportsCache(): void {
@@ -28,11 +31,11 @@ export function invalidateIndexExportsCacheEntry(indexPath: string): void {
2831
* Lightweight regex based extraction – sufficient for current export patterns.
2932
*/
3033
export function getIndexExports(tree: Tree, indexPath: string): IndexExports {
31-
const cached = indexExportsCache.get(indexPath);
32-
if (cached) return cached;
33-
3434
const content = treeReadCache.read(tree, indexPath, 'utf-8') || '';
3535

36+
const cached = indexExportsCache.get(indexPath);
37+
if (cached && cached.content === content) return cached;
38+
3639
const exports = new Set<string>();
3740
const reexports = new Set<string>();
3841

@@ -51,10 +54,12 @@ export function getIndexExports(tree: Tree, indexPath: string): IndexExports {
5154
// Capture exported local files: export * from './lib/file'; we store path without extension for comparison ease.
5255
for (const spec of reexports) {
5356
const withoutExt = spec.replace(/\.(ts|tsx|js|jsx)$/i, '');
54-
exports.add(withoutExt);
57+
// Ensure stored paths are normalized to start with './'
58+
const normalized = withoutExt.startsWith('./') || withoutExt.startsWith('../') ? withoutExt : `./${withoutExt}`;
59+
exports.add(normalized);
5560
}
5661

57-
const result: IndexExports = { exports, reexports };
62+
const result: CachedIndexExports = { exports, reexports, content };
5863
indexExportsCache.set(indexPath, result);
5964
return result;
6065
}

packages/workspace/src/generators/move-file/export-management/is-file-exported.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Tree } from '@nx/devkit';
22
import type { ProjectConfiguration } from '@nx/devkit';
33
import { getProjectEntryPointPaths } from '../project-analysis/get-project-entry-point-paths';
44
import { removeSourceFileExtension } from '../path-utils/remove-source-file-extension';
5-
import { escapeRegex } from '../security-utils/escape-regex';
5+
66
import { treeReadCache } from '../tree-cache';
77
import { getIndexExports } from './index-exports-cache';
88

@@ -32,7 +32,7 @@ export function isFileExported(
3232
const indexPaths = getProjectEntryPointPaths(tree, project);
3333

3434
const fileWithoutExt = removeSourceFileExtension(file);
35-
const escapedFile = escapeRegex(fileWithoutExt);
35+
3636

3737
return indexPaths.some((indexPath) => {
3838
if (!cachedTreeExists(tree, indexPath)) {

0 commit comments

Comments
 (0)