fix(TsPathRewriter): auto-detect all ts_* folders in project for cross-module import rewriting

Previously only mapped folders from the current compilation glob patterns.
Now scans project directory for all ts_* folders to ensure imports like
'../ts_shared/' are rewritten to '../dist_ts_shared/' even when compiling
only the main ts/ folder.
This commit is contained in:
2026-01-12 17:53:06 +00:00
parent 1e40bd5882
commit c8a1582ec4
2 changed files with 35 additions and 1 deletions

View File

@@ -51,6 +51,39 @@ export class TsPathRewriter {
return new TsPathRewriter(mappings);
}
/**
* Create a TsPathRewriter by auto-detecting all ts_* folders in the project directory.
* This ensures that cross-module imports are rewritten even when compiling a single module.
*
* For example, if the project has ts/, ts_shared/, ts_web/ folders:
* - ts → dist_ts
* - ts_shared → dist_ts_shared
* - ts_web → dist_ts_web
*
* @param cwd - The project root directory
* @returns TsPathRewriter instance with all detected folder mappings
*/
public static async fromProjectDirectory(cwd: string): Promise<TsPathRewriter> {
const mappings: IFolderMapping[] = [];
try {
const entries = await FsHelpers.listDirectory(cwd);
for (const entry of entries) {
// Match folders starting with 'ts' (ts, ts_shared, ts_web, ts_core, etc.)
if (entry.isDirectory && /^ts(_|$)/.test(entry.name)) {
const sourceFolder = entry.name;
const destFolder = `dist_${sourceFolder}`;
mappings.push({ sourceFolder, destFolder });
}
}
} catch {
// Directory listing failed, return empty mappings
}
return new TsPathRewriter(mappings);
}
/**
* Get the current folder mappings
*/