fix(TsPathRewriter): rewrite cross-module import paths after compilation
When compiling multiple ts_* source directories to dist_ts_* outputs, imports like '../ts_shared/helper.js' now correctly become '../dist_ts_shared/helper.js' in the compiled output. Uses targeted regex patterns that only match import/export/require statements, avoiding false positives in strings or comments.
This commit is contained in:
@@ -7,6 +7,7 @@ import * as smartpath from '@push.rocks/smartpath';
|
||||
import { TsConfig } from '../mod_config/index.js';
|
||||
import { FsHelpers } from '../mod_fs/index.js';
|
||||
import { performUnpack } from '../mod_unpack/index.js';
|
||||
import { TsPathRewriter } from '../mod_pathrewrite/index.js';
|
||||
|
||||
/**
|
||||
* Interface for error summary data
|
||||
@@ -307,6 +308,7 @@ export class TsCompiler {
|
||||
): Promise<ICompileResult> {
|
||||
const emittedFiles: string[] = [];
|
||||
const errorSummaries: IErrorSummary[] = [];
|
||||
const successfulOutputDirs: string[] = [];
|
||||
|
||||
const totalTasks = Object.keys(globPatterns).length;
|
||||
let currentTask = 0;
|
||||
@@ -365,6 +367,20 @@ export class TsCompiler {
|
||||
// Perform unpack if compilation succeeded
|
||||
if (result.errorSummary.totalErrors === 0) {
|
||||
await performUnpack(pattern, destDir, this.cwd);
|
||||
successfulOutputDirs.push(destDir);
|
||||
}
|
||||
}
|
||||
|
||||
// Rewrite import paths in all output directories to handle cross-module references
|
||||
// This must happen after ALL compilations so all destination folders exist
|
||||
if (successfulOutputDirs.length > 0) {
|
||||
const rewriter = TsPathRewriter.fromGlobPatterns(globPatterns);
|
||||
let totalRewritten = 0;
|
||||
for (const outputDir of successfulOutputDirs) {
|
||||
totalRewritten += await rewriter.rewriteDirectory(outputDir);
|
||||
}
|
||||
if (totalRewritten > 0 && !isQuiet && !isJson) {
|
||||
console.log(` 🔄 Rewrote import paths in ${totalRewritten} file${totalRewritten !== 1 ? 's' : ''}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user