Fix absolute tsx imports not being converted to .js extensions #167
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue where absolute imports (imports that don't start with
.or..) were not being converted to include.jsextensions, while relative imports worked correctly.The Problem
Previously, imports like this would not be converted:
While relative imports worked fine:
Root Cause
The issue was in
src/util/replaceModulePath.tswhere the logic incorrectly classified absolute imports within the project as "node modules" imports. Any non-relative import containing a/that wasn't a path alias was assumed to be fromnode_modules, causing the tool to look for files in the wrong location.The Solution
Modified the
createReplacementPathfunction to:Properly handle absolute imports: Added logic to detect absolute imports within the project (non-relative imports that aren't path aliases)
Support TypeScript's
baseUrl: Use the TypeScript compiler'sbaseUrlconfiguration to correctly resolve absolute imports relative to the configured base directoryMaintain fallback behavior: If a file isn't found as a project absolute import, fall back to checking
node_modulesto preserve existing functionalityChanges Made
baseUrlcompiler option resolutionExamples
The fix now correctly converts absolute imports in various configurations:
With
baseUrl: "src":With
baseUrl: ".":Node modules imports remain unchanged:
Fixes #126.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.