Repository navigation
fix: Resolve tsconfig path aliases like TypeScript does in monorepos - #213
Merged
Merged
Conversation
Path aliases were resolved relative to the directory of the converted project's tsconfig.json. TypeScript resolves them relative to "baseUrl" or, without it, relative to the tsconfig.json that declares "paths". In a monorepo where an app's tsconfig extends a root config, aliased imports like "@mylib/utils/helper" were looked up inside the app folder, not found, and left without a file extension. Two related issues in the same code path are fixed as well: - An alias with several targets was joined into one comma-separated path. Each target is now tried in order. - Wildcard patterns matched too much: "@mylib/*" matched "@MyLib" and "@app/*" matched "@application/x". The resolved base path exposed this, because "@MyLib" was turned into "@mylib/index.js".
# Conflicts: # src/util/replaceModulePath.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #127
Path aliases were resolved relative to the folder of the converted project's
tsconfig.json. TypeScript resolves them relative tobaseUrlor, if that's missing, relative to thetsconfig.jsonthat declarespaths. In the Nx layout from the issue, an app's tsconfig extends the root config that holds the aliases. So@mylib/utils/helperwas looked up underapps/myapp/libs/..., not found, and left without an extension.The base folder now comes from
baseUrl, or from TypeScript'spathsBasePathwhen there's nobaseUrl.pathsBasePathis set by TypeScript's config parser but isn't in its public types, so it's read with anincheck instead of a cast.Two related bugs in the same code path showed up while fixing this:
"@gen/*": ["./a/*", "./b/*"], was turned into one comma-joined path. Each target is now tried in order.@mylib/*matched the plain@mylibimport, and@app/*would match@application/x. With the correct base folder this turned@mylibinto@mylib/index.js. A wildcard pattern now has to match everything before the*, including the slash.The
getNormalizedPathunit test also ran itsexpectdirectly insidedescribe, which vitest reports as an empty suite. It now usesitblocks and covers multiple targets.