-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: infer aliases from tsconfig.json #494
Draft
kricsleo
wants to merge
15
commits into
unjs:main
Choose a base branch
from
kricsleo:feat/infer-alias-from-tsconfig
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−0
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8a7d246
feat: infer aliases from tsconfig.json
kricsleo 91d4f3c
chore: apply automated updates
autofix-ci[bot] 0b9a05d
update
kricsleo 4c8edd0
Merge branch 'feat/infer-alias-from-tsconfig' of github.com:kricsleo/…
kricsleo 4004f54
chore: apply automated updates
autofix-ci[bot] 1ae6730
update
kricsleo f5c133e
Merge branch 'feat/infer-alias-from-tsconfig' of github.com:kricsleo/…
kricsleo e241a15
chore: apply automated updates
autofix-ci[bot] 637009b
update
kricsleo 0d9dd11
Merge branch 'feat/infer-alias-from-tsconfig' of github.com:kricsleo/…
kricsleo 190a690
chore: apply automated updates
autofix-ci[bot] 54dab07
update
kricsleo 980cc02
Merge branch 'feat/infer-alias-from-tsconfig' of github.com:kricsleo/…
kricsleo 4efbcdb
chore: apply automated updates
autofix-ci[bot] d8f8fb8
update
kricsleo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
import { dirname, resolve } from "pathe"; | ||
import { | ||
sys, | ||
findConfigFile, | ||
readConfigFile, | ||
parseJsonConfigFileContent, | ||
} from "typescript"; | ||
import type { CompilerOptions } from "typescript"; | ||
import type { PreRenderedChunk } from "rollup"; | ||
import type { BuildContext } from "../../types"; | ||
|
||
|
@@ -15,6 +23,7 @@ export const DEFAULT_EXTENSIONS: string[] = [ | |
|
||
export function resolveAliases(ctx: BuildContext): Record<string, string> { | ||
const aliases: Record<string, string> = { | ||
...inferAliasesFromTsconfig(ctx), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be opt-in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, do you suggest adding a new configuration for this feature? |
||
[ctx.pkg.name!]: ctx.options.rootDir, | ||
...ctx.options.alias, | ||
}; | ||
|
@@ -40,6 +49,67 @@ export function resolveAliases(ctx: BuildContext): Record<string, string> { | |
return aliases; | ||
} | ||
|
||
function inferAliasesFromTsconfig( | ||
ctx: BuildContext, | ||
): Record<string, string> | undefined { | ||
const tsconfig = getTsconfig(ctx); | ||
|
||
if (!tsconfig.compilerOptions?.paths) { | ||
return; | ||
} | ||
|
||
const tsconfigDir = tsconfig.path | ||
? dirname(tsconfig.path) | ||
: ctx.options.rootDir; | ||
|
||
const resolvedBaseUrl = resolve( | ||
tsconfigDir, | ||
tsconfig.compilerOptions?.baseUrl || ".", | ||
); | ||
|
||
const aliases = Object.fromEntries( | ||
Object.entries(tsconfig.compilerOptions.paths).map( | ||
([pattern, substitutions]) => { | ||
const find = pattern.replace(/\/\*$/, ""); | ||
// Pick only the first path. | ||
const replacement = substitutions[0].replace(/\*$/, ""); | ||
const resolvedReplacement = resolve(resolvedBaseUrl, replacement); | ||
return [find, resolvedReplacement]; | ||
}, | ||
), | ||
); | ||
|
||
return aliases; | ||
} | ||
|
||
function getTsconfig(ctx: BuildContext): { | ||
path?: string; | ||
compilerOptions?: CompilerOptions; | ||
} { | ||
const { tsconfig: overridePath, compilerOptions: overrideCompilerOptions } = | ||
ctx.options.rollup.dts; | ||
|
||
const tsconfigPath = overridePath | ||
? resolve(ctx.options.rootDir, overridePath) | ||
: findConfigFile(ctx.options.rootDir, sys.fileExists); | ||
|
||
if (!tsconfigPath) { | ||
return { compilerOptions: overrideCompilerOptions }; | ||
} | ||
|
||
const { config: tsconfigRaw } = readConfigFile(tsconfigPath, sys.readFile); | ||
const { options: compilerOptions } = parseJsonConfigFileContent( | ||
tsconfigRaw, | ||
sys, | ||
dirname(tsconfigPath), | ||
); | ||
|
||
return { | ||
path: tsconfigPath, | ||
compilerOptions: { ...compilerOptions, ...overrideCompilerOptions }, | ||
}; | ||
} | ||
|
||
export function getChunkFilename( | ||
ctx: BuildContext, | ||
chunk: PreRenderedChunk, | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid loading
typescript
package as it adds runtime overhead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have considered this issue and come up with two alternative solutions:
Implement a lightweight
tsconfig.json
parsing logic that only parses thepaths
andbaseUrl
fields to infer the alias.Use a lightweight alternative library (e.g., https://github.com/privatenumber/get-tsconfig) (or implement one ourselves) to parse the complete
tsconfig.json
configuration, which can not only be used for alias inference but also for other configurations, such as automatically passing it to esbuild'stsconfigRaw
(which can resolve Decorators support #309), thereby eliminating the need to duplicate existing content fromtsconfig.json
in configuration files.I'd love to hear your feedback! 🤔